forked from Enjoyee/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnv.js
More file actions
496 lines (439 loc) · 16 KB
/
Env.js
File metadata and controls
496 lines (439 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Variables used by Scriptable.
/* -----------------------------------------------
这是一份模板,可以导入进行使用里面封装的方法变量,谨慎修改!!
----------------------------------------------- */
//------------------------------------------------
const fm = FileManager.local()
let path = ""
let exports = {}
//------------------------------------------------
let configs = {
previewSize: "Medium", // 预览大小【小:Small,中:Medium,大:Large】
changePicBg: true, // 是否需要更换背景
colorMode: false, // 是否是纯色背景
bgColor: undefined, // 小组件背景色
topPadding: 0, // 内容区边距
leftPadding: 0, // 内容区边距
bottomPadding: 0, // 内容区边距
rightPadding: 0, // 内容区边距
refreshInterval: 0 // 刷新间隔,单位分钟,非精准,会有3-5分钟差距
}
exports.configs = configs
//------------------------------------------------
// 标题样式定义
const textStyle = {
stack: undefined, // 加入到哪个内容栈显示
marginStart: 0, // 开始距离,水平方向的就是左边距离,垂直方向的就是顶部距离
marginEnd: 0, // 结束距离,水平方向的就是右边距离,垂直方向的就是底部距离
text: "", // 显示的文字
width: 0, // 宽
height: 0, // 长
lineLimit: 0, // 行数控制,0是全部展示
font: undefined, // 字体
textColor: undefined, // 文字颜色
}
exports.textStyle = textStyle
//------------------------------------------------
// 图片样式定义
const imgStyle = {
stack: undefined, // 加入到哪个内容栈显示
marginStart: 0, // 开始距离,水平方向的就是左边距离,垂直方向的就是顶部距离
marginEnd: 0, // 结束距离,水平方向的就是右边距离,垂直方向的就是底部距离
img: undefined, // 图片资源
width: 50, // 宽
height: 50, // 长
tintColor: undefined, // 图片渲染颜色
}
exports.imgStyle = textStyle
//------------------------------------------------
//------------------------------------------------
exports.run = async function (scriptName, widget) {
let appearance = (await exports.isUsingDarkAppearance()) ? 'dark' : 'light'
let appearanceStr = appearance == 'dark' ? '暗黑模式' : '白天模式'
let fileImgName = `${scriptName}-${appearance}.jpg`
path = fm.joinPath(fm.documentsDirectory(), fileImgName)
if (!exports.configs.changePicBg || exports.configs.colorMode || config.runsInWidget) {
// 结束并且进行预览
completeWidget(widget)
return
}
const okTips = `您的小部件【${appearanceStr}】背景已准备就绪,请退到桌面查看即可。`
let message = "图片模式支持相册照片&背景透明"
let options = ["图片选择", "透明背景"]
let isTransparentMode = await generateAlert(message, options)
if (!isTransparentMode) {
let img = await Photos.fromLibrary()
message = okTips
const resultOptions = ["好的"]
await generateAlert(message, resultOptions)
fm.writeImage(path, img)
} else {
message = "以下是【透明背景】生成步骤,如果你没有屏幕截图请退出,并返回主屏幕长按进入编辑模式。滑动到最右边的空白页截图,然后重新运行!"
let exitOptions = ["继续(已有截图)", "退出(没有截图)"]
let shouldExit = await generateAlert(message, exitOptions)
if (shouldExit) return
// Get screenshot and determine phone size.
let img = await Photos.fromLibrary()
let height = img.size.height
let phone = phoneSizes()[height]
if (!phone) {
message = "您似乎选择了非iPhone屏幕截图的图像,或者不支持您的iPhone。请使用其他图像再试一次!"
await generateAlert(message, ["好的!我现在去截图"])
return
}
// Prompt for widget size and position.
message = "您想要创建什么尺寸的小部件?"
let sizes = ["小号", "中号", "大号"]
let size = await generateAlert(message, sizes)
let widgetSize = sizes[size]
message = "您想它在什么位置?"
message += (height == 1136 ? " (请注意,您的设备仅支持两行小部件,因此中间和底部选项相同。)" : "")
// Determine image crop based on phone size.
let crop = { w: "", h: "", x: "", y: "" }
if (widgetSize == "小号") {
crop.w = phone.小号
crop.h = phone.小号
let positions = ["顶部 左边", "顶部 右边", "中间 左边", "中间 右边", "底部 左边", "底部 右边"]
let position = await generateAlert(message, positions)
// Convert the two words into two keys for the phone size dictionary.
let keys = positions[position].split(' ')
crop.y = phone[keys[0]]
crop.x = phone[keys[1]]
} else if (widgetSize == "中号") {
crop.w = phone.中号
crop.h = phone.小号
// 中号 and 大号 widgets have a fixed x-value.
crop.x = phone.左边
let positions = ["顶部", "中间", "底部"]
let position = await generateAlert(message, positions)
let key = positions[position].toLowerCase()
crop.y = phone[key]
} else if (widgetSize == "大号") {
crop.w = phone.中号
crop.h = phone.大号
crop.x = phone.左边
let positions = ["顶部", "底部"]
let position = await generateAlert(message, positions)
// 大号 widgets at the 底部 have the "中间" y-value.
crop.y = position ? phone.中间 : phone.顶部
}
// Crop image and finalize the widget.
let imgCrop = cropImage(img, new Rect(crop.x, crop.y, crop.w, crop.h))
message = okTips
const resultOptions = ["好的"]
await generateAlert(message, resultOptions)
fm.writeImage(path, imgCrop)
}
// 结束并且进行预览
completeWidget(widget)
}
//------------------------------------------------
function completeWidget(widget) {
// 刷新间隔
const refreshInterval = exports.configs.refreshInterval
if (refreshInterval > 0) {
widget.refreshAfterDate = new Date(new Date() + 1000*60*refreshInterval)
}
// 背景
if (exports.configs.colorMode) {
widget.backgroundColor = exports.configs.bgColor
} else {
widget.backgroundImage = fm.readImage(path)
}
// 设置边距(上,左,下,右)
widget.setPadding(exports.configs.topPadding, exports.configs.leftPadding, exports.configs.bottomPadding, exports.configs.rightPadding)
// 设置组件
Script.setWidget(widget)
// 完成脚本
Script.complete()
// 预览
if (exports.configs.previewSize == "Large") {
widget.presentLarge()
} else if (exports.configs.previewSize == "Medium") {
widget.presentMedium()
} else {
widget.presentSmall()
}
}
//------------------------------------------------
// Generate an alert with the provided array of options.
async function generateAlert(message, options) {
let alert = new Alert()
alert.message = message
for (const option of options) {
alert.addAction(option)
}
let response = await alert.presentAlert()
return response
}
//------------------------------------------------
// Crop an image into the specified rect.
function cropImage(img, rect) {
let draw = new DrawContext()
draw.size = new Size(rect.width, rect.height)
draw.drawImageAtPoint(img, new Point(-rect.x, -rect.y))
return draw.getImage()
}
//------------------------------------------------
// Pixel sizes and positions for widgets on all supported phones.
function phoneSizes() {
let phones = {
"2340": { // 12mini
"小号": 436,
"中号": 936,
"大号": 980,
"左边": 72,
"右边": 570,
"顶部": 212,
"中间": 756,
"底部": 1300,
},
"2532": { // 12/12 Pro
"小号": 472,
"中号": 1012,
"大号": 1058,
"左边": 78,
"右边": 618,
"顶部": 230,
"中间": 818,
"底部": 1408,
},
"2778": { // 12 Pro Max
"小号": 518,
"中号": 1114,
"大号": 1162,
"左边": 86,
"右边": 678,
"顶部": 252,
"中间": 898,
"底部": 1544,
},
"2688": {
"小号": 507,
"中号": 1080,
"大号": 1137,
"左边": 81,
"右边": 654,
"顶部": 228,
"中间": 858,
"底部": 1488
},
"1792": {
"小号": 338,
"中号": 720,
"大号": 758,
"左边": 54,
"右边": 436,
"顶部": 160,
"中间": 580,
"底部": 1000
},
"2436": {
"小号": 465,
"中号": 987,
"大号": 1035,
"左边": 69,
"右边": 591,
"顶部": 213,
"中间": 783,
"底部": 1353
},
"2208": {
"小号": 471,
"中号": 1044,
"大号": 1071,
"左边": 99,
"右边": 672,
"顶部": 114,
"中间": 696,
"底部": 1278
},
"1334": {
"小号": 296,
"中号": 642,
"大号": 648,
"左边": 54,
"右边": 400,
"顶部": 60,
"中间": 412,
"底部": 764
},
"1136": {
"小号": 282,
"中号": 584,
"大号": 622,
"左边": 30,
"右边": 332,
"顶部": 59,
"中间": 399,
"底部": 399
}
}
return phones
}
//------------------------------------------------
exports.isUsingDarkAppearance = async function () {
const wv = new WebView()
let js = "(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)"
let r = await wv.evaluateJavaScript(js)
return r
}
//================================================
//================================================
//================================================
//================================================
//================================================
exports.getImage = async function (url) {
const request = new Request(url)
const data = await request.loadImage()
return data
}
//------------------------------------------------
exports.getJson = async function (url) {
const request = new Request(url)
const defaultHeaders = {
"Accept": "*/*",
"Content-Type": "application/json"
}
request.url = url
request.method = 'GET'
request.headers = defaultHeaders
const data = await request.loadJSON()
return data
}
//------------------------------------------------
exports.alignVerticallyCenterStack = function (alignmentStack) {
let returnStack = alignmentStack.addStack()
returnStack.layoutVertically()
returnStack.centerAlignContent()
return returnStack
}
//------------------------------------------------
exports.alignHorizontallyCenterStack = function (alignmentStack) {
let returnStack = alignmentStack.addStack()
returnStack.layoutHorizontally()
returnStack.centerAlignContent()
return returnStack
}
//------------------------------------------------
exports.alignBottomStack = function (alignmentStack, marginBottom) {
let contentStack = alignmentStack.addStack()
contentStack.layoutVertically()
contentStack.addSpacer()
let returnStack = contentStack.addStack()
// 添加下边距
if (marginBottom != undefined && marginBottom != 0) {
contentStack.addSpacer(marginBottom)
}
return returnStack
}
//------------------------------------------------
exports.alignTopStack = function (alignmentStack, marginTop) {
let contentStack = alignmentStack.addStack()
contentStack.layoutVertically()
// 添加左边距
if (marginTop != undefined && marginTop != 0) {
contentStack.addSpacer(marginTop)
}
let returnStack = contentStack.addStack()
returnStack.layoutVertically()
contentStack.addSpacer()
return returnStack
}
//------------------------------------------------
exports.alignLeftStack = function (alignmentStack, marginLeft) {
let contentStack = alignmentStack.addStack()
contentStack.layoutHorizontally()
let returnStack = contentStack.addStack()
returnStack.layoutHorizontally()
// 添加左边距
if (marginLeft != undefined && marginLeft != 0) {
returnStack.addSpacer(marginLeft)
}
contentStack.addSpacer()
return returnStack
}
//------------------------------------------------
exports.alignRightStack = function (alignmentStack, marginRight) {
let contentStack = alignmentStack.addStack()
contentStack.layoutHorizontally()
contentStack.addSpacer()
let returnStack = contentStack.addStack()
// 添加右边距
if (marginRight != undefined && marginRight != 0) {
contentStack.addSpacer(marginRight)
}
return returnStack
}
//------------------------------------------------
exports.addStyleImg = function () {
const imgStyle = exports.imgStyle
const contentStack = imgStyle.stack
if (contentStack == undefined) {
return
}
const marginStart = textStyle.marginStart
if (marginStart != undefined && marginStart != 0) {
contentStack.addSpacer(marginStart)
}
const imgSpan = contentStack.addImage(imgStyle.img)
imgSpan.imageSize = new Size(imgStyle.width, imgStyle.height)
const tintColor = imgStyle.tintColor
if (tintColor != undefined) {
imgSpan.tintColor = tintColor
}
const marginEnd = textStyle.marginEnd
if (marginStart != undefined && marginStart != 0) {
contentStack.addSpacer(marginEnd)
}
// 重置样式
resetImgStyle()
}
//------------------------------------------------
exports.addStyleText = function () {
const textStyle = exports.textStyle
const contentStack = textStyle.stack
if (contentStack == undefined) {
return
}
const marginStart = textStyle.marginStart
if (marginStart != undefined && marginStart != 0) {
contentStack.addSpacer(marginStart)
}
const textSpan = contentStack.addText(`${textStyle.text}`)
contentStack.size = new Size(textStyle.width, textStyle.height)
textSpan.lineLimit = textStyle.lineLimit
textSpan.font = textStyle.font
textSpan.textColor = textStyle.textColor
const marginEnd = textStyle.marginEnd
if (marginStart != undefined && marginStart != 0) {
contentStack.addSpacer(marginEnd)
}
// 重置样式
resetTextStyle()
}
//------------------------------------------------
function resetImgStyle() {
exports.imgStyle.stack = undefined // 加入到哪个内容栈显示
exports.textStyle.marginStart = 0
exports.textStyle.marginEnd = 0
exports.imgStyle.img = undefined // 图片资源
exports.imgStyle.width = 0 // 宽
exports.imgStyle.height = 0 // 长
exports.imgStyle.tintColor = undefined // 图片渲染颜色
}
//------------------------------------------------
function resetTextStyle() {
exports.textStyle.stack = undefined // 加入到哪个内容栈显示
exports.textStyle.marginStart = 0
exports.textStyle.marginEnd = 0
exports.textStyle.text = "" // 显示的文字
exports.textStyle.width = 0 // 宽
exports.textStyle.height = 0 // 长
exports.textStyle.lineLimit = 0 // 行数控制,0是全部展示
exports.textStyle.font = undefined // 字体
exports.textStyle.textColor = undefined // 文字颜色
}
//------------------------------------------------
module.exports = exports
//------------------------------------------------