-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scriptable Intent.js
42 lines (38 loc) · 1.27 KB
/
Scriptable Intent.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: cog;
/**
* A script result object.
* @typedef ScriptResult
* @property {string} kind - One of 'Result' or a JS Error name.
* @property {string} value - Either the script output, or the Error message.
*/
/**
* Evaluate arbitrary JavaScript code from Shortcuts.
* Handles both native shortcut arguments (iOS 13+) and pasteboard input (iOS 12).
* @author Martin Kopischke <martin@kopischke.net>
* @version 2.1.2
* @returns {ScriptResult} The result of running the script (as JSON on Pasteboard).
*/
const processor = {}
if (args.shortcutParameter != null) {
// iOS 13+: use args and native output
processor.in = () => args.shortcutParameter
processor.out = Script.setShortcutOutput
} else {
// iOS 12: use Pasteboard (with JSON return data)
processor.in = Pasteboard.pasteString
processor.out = o => {
Pasteboard.copyString(JSON.stringify(o))
Script.complete()
}
}
var result
try {
let code = processor.in()
if (code == null || !code.trim().length) throw new EvalError('No code to evaluate.')
result = { kind: 'Result', value: Function(code)() }
} catch (e) {
result = { kind: e.name, value: e.message }
}
processor.out(result)