-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSticky widget.js
73 lines (66 loc) · 1.77 KB
/
Sticky widget.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: sticky-note;
async function editData(data) {
let editor = new Alert()
editor.title = "Sticky"
editor.addTextField(data)
editor.addCancelAction("Cancel")
editor.addAction("Save")
let action = await editor.present()
if (action < 0) {
return data
} else {
return editor.textFieldValue(0)
}
}
function saveData(data) {
let fm = FileManager.iCloud()
let path = fm.joinPath(fm.documentsDirectory(), "sticky.txt")
fm.writeString(path, data)
}
function loadData() {
try {
let fm = FileManager.iCloud()
let path = fm.joinPath(fm.documentsDirectory(), "sticky.txt")
return fm.readString(path)
} catch(error) {
return "Type a note..."
}
}
function createWidget(note) {
let widget = new ListWidget()
widget.setPadding(16, 16, 16, 8)
let dark = Device.isUsingDarkAppearance()
let fgColor = Color.black()
if (dark) {
fgColor = new Color("#FFCF00")
let bgColor = Color.black()
widget.backgroundColor = bgColor
} else {
let startColor = new Color("#F8DE5F")
let endColor = new Color("#FFCF00")
let gradient = new LinearGradient()
gradient.colors = [startColor, endColor]
gradient.locations = [0.0, 1]
widget.backgroundGradient = gradient
}
let noteText = widget.addText(note)
noteText.textColor = fgColor
noteText.font = Font.mediumRoundedSystemFont(24)
noteText.textOpacity = 0.8
noteText.minimumScaleFactor = 0.25
return widget
}
if (config.runsInWidget) {
let note = loadData()
let widget = createWidget(note)
Script.setWidget(widget)
Script.complete()
}
if (config.runsInApp) {
let note = loadData()
note = await editData(note)
saveData(note)
Script.complete()
}