-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnobg-configurable-widget-template.js
96 lines (86 loc) · 2.18 KB
/
nobg-configurable-widget-template.js
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: font;
/*
Script : nobg-configurable-widget-template.js
Author : [email protected]
Version : 1.0.0
Description :
Example script for the no-backkground module.
This allows the end user to set the background
of the widget at run-time instead of changing
the code.
*/
const nobg = importModule('no-background.js')
const widgetID = 'nobgwidget'
//------------------------------------------------
async function createWidget() {
const widget = new ListWidget();
widget.backgroundImage = await nobg.getSliceForWidget(widgetID)
// widget content code goes here
widget.addSpacer()
let text = widget.addText('Hello Scriptable')
text.font = new Font('AppleColorEmoji',14)
text.textColor = Color.white()
text.centerAlignText()
widget.addSpacer()
// end of widget content code
return widget
}
//---[ main ]-------------------------------------
if (config.runsInWidget) {
let widget = await createWidget()
Script.setWidget(widget)
Script.complete()
return
} else {
// show options
let response = await presentAlert(
'Options',
[
'Preview Widget',
'Set Background',
'Cancel'
])
switch(response) {
case 0:
let widget = await createWidget()
await previewWidget(widget)
break;
case 1:
await nobg.chooseBackgroundSlice(widgetID)
break;
default:
}
return
}
//------------------------------------------------
async function previewWidget(widget) {
let resp = await presentAlert('Preview Widget',
['Small','Medium','Large','Cancel'])
switch (resp) {
case 0:
await widget.presentSmall()
break;
case 1:
await widget.presentMedium()
break;
case 2:
await widget.presentLarge()
break;
default:
}
}
//------------------------------------------------
async function presentAlert(prompt,items,asSheet)
{
let alert = new Alert()
alert.message = prompt
for (const item of items) {
alert.addAction(item)
}
let resp = asSheet ?
await alert.presentSheet() :
await alert.presentAlert()
return resp
}