-
Notifications
You must be signed in to change notification settings - Fork 1
/
transparentwidget-grtnkrd
289 lines (239 loc) · 6.5 KB
/
transparentwidget-grtnkrd
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
// created by tobias gretenkord
/*
Das Script ist frei zu verwenden.
*/
const filename = Script.name() + ".jpg"
const files = FileManager.local()
const path = files.joinPath(files.documentsDirectory(), filename)
if (config.runsInWidget) {
let widget = new ListWidget()
widget.backgroundImage = files.readImage(path)
Script.setWidget(widget)
Script.complete()
/*
* ===================================================================
*/
} else {
// Abfrage über bestehenden Screenshot.
var message
message = "Bevor du beginnst, mache einen Screenshot von deinem Wallpaper, indem du in den Switch-App-Icons-Wackelmodus gehst. Gehe nach rechts auf eine leere Seite und mache einen Screenshot."
let exitOptions = ["Screenshot vorhanden","Verlassen zum Screenshot machen"]
let shouldExit = await generateAlert(message,exitOptions)
if (shouldExit) return
// Screenshot und Auflösungs-Abfrage.
let img = await Photos.fromLibrary()
let height = img.size.height
let phone = phoneSizes()[height]
if (!phone) {
message = "Sie haben keinen Wackel-Modus-Screenshot gemacht oder das Foto ist nicht verwendbar."
await generateAlert(message,["OK, neu versuchen"])
return
}
// Extra setup needed for 2436-sized phones.
if (height == 2436) {
let cacheName = "grtnkrd-transparent-widget"
let cachePath = files.joinPath(files.libraryDirectory(), cacheName)
// Lade zwischengespeicherte Wallpaper-Größe.
if (files.fileExists(cachePath)) {
let typeString = files.readString(cachePath)
phone = phone[typeString]
// Falls kein Wallpaper zwischengespeichert
} else {
message = "Welches iPhone hast du?"
let types = ["iPhone 12 mini", "iPhone 11 Pro, XS, or X"]
let typeIndex = await generateAlert(message, types)
let type = (typeIndex == 0) ? "mini" : "x"
phone = phone[type]
files.writeString(cachePath, type)
}
}
// Anzeige-Abfrage.
message = "Welche Größe hat das Scriptable-Widget?"
let sizes = ["Small","Medium","Large"]
let size = await generateAlert(message,sizes)
let widgetSize = sizes[size]
message = "Welche Position hat das Scriptable Widget?"
message += (height == 1136 ? " (Note that your device only supports two rows of widgets, so the middle and bottom options are the same.)" : "")
// Crop Size
let crop = { w: "", h: "", x: "", y: "" }
if (widgetSize == "Small") {
crop.w = phone.small
crop.h = phone.small
let positions = ["oben links","oben rechts","mittig links","mittig rechts","unten links","unten rechts"]
let position = await generateAlert(message,positions)
let keys = positions[position].toLowerCase().split(' ')
crop.y = phone[keys[0]]
crop.x = phone[keys[1]]
} else if (widgetSize == "Medium") {
crop.w = phone.medium
crop.h = phone.small
crop.x = phone.left
let positions = ["Top","Middle","Bottom"]
let position = await generateAlert(message,positions)
let key = positions[position].toLowerCase()
crop.y = phone[key]
} else if(widgetSize == "Large") {
crop.w = phone.medium
crop.h = phone.large
crop.x = phone.left
let positions = ["Top","Bottom"]
let position = await generateAlert(message,positions)
crop.y = position ? phone.middle : phone.top
}
// Crop
let imgCrop = cropImage(img, new Rect(crop.x,crop.y,crop.w,crop.h))
message = "Dein Widget-Hintergrund ist ready, wollen Sie es für das Script nutzen oder extern speichern?"
const exportPhotoOptions = ["Script-Nutzung","Foto-Export","Dokumenten-Export"]
const exportPhoto = await generateAlert(message,exportPhotoOptions)
if (exportPhoto == 0) {
files.writeImage(path,imgCrop)
} else if (exportPhoto == 1) {
Photos.save(imgCrop)
} else if (exportPhoto == 2) {
await DocumentPicker.exportImage(imgCrop)
}
Script.complete()
}
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
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()
}
// verschiedene Pixel Größen
function phoneSizes() {
let phones = {
// 12 Pro Max
"2778": {
small: 510,
medium: 1092,
large: 1146,
left: 96,
right: 678,
top: 246,
middle: 882,
bottom: 1518
},
// 12 and 12 Pro
"2532": {
small: 474,
medium: 1014,
large: 1062,
left: 78,
right: 618,
top: 231,
middle: 819,
bottom: 1407
},
// 11 Pro Max, XS Max
"2688": {
small: 507,
medium: 1080,
large: 1137,
left: 81,
right: 654,
top: 228,
middle: 858,
bottom: 1488
},
// 11, XR
"1792": {
small: 338,
medium: 720,
large: 758,
left: 54,
right: 436,
top: 160,
middle: 580,
bottom: 1000
},
// 11 Pro, XS, X, 12 mini
"2436": {
x: {
small: 465,
medium: 987,
large: 1035,
left: 69,
right: 591,
top: 213,
middle: 783,
bottom: 1353,
},
mini: {
small: 465,
medium: 987,
large: 1035,
left: 69,
right: 591,
top: 231,
middle: 801,
bottom: 1371,
}
},
"2208": {
small: 471,
medium: 1044,
large: 1071,
left: 99,
right: 672,
top: 114,
middle: 696,
bottom: 1278
},
// SE2 / 6/6S/7/8
"1334": {
small: 296,
medium: 642,
large: 648,
left: 54,
right: 400,
top: 60,
middle: 412,
bottom: 764
},
// SE1
"1136": {
small: 282,
medium: 584,
large: 622,
left: 30,
right: 332,
top: 59,
middle: 399,
bottom: 399
},
// 11 & XR
"1624": {
small: 310,
medium: 658,
large: 690,
left: 46,
right: 394,
top: 142,
middle: 522,
bottom: 902
},
// Plus
"2001" : {
small: 444,
medium: 963,
large: 972,
left: 81,
right: 600,
top: 90,
middle: 618,
bottom: 1146
},
}
return phones
}