This document describes how to use the library contained in this lib directory to create scripts that run:
- In CLI on macOS
- From the share sheet on iOS/iPadOS (Using Shortcuts)
- From the home screen on iOS/iPadOS (Using Shortcuts)
- In the Scriptable App on iOS/iPadOS
In order for scripts to be able to run in all environments, they cannot use:
require(...)(importing NodeJS modules)import ...(ES6-style imports)importModule(...)(Scriptable-style import)
Instead, use these four lines to import the correct set of library functions depending on environment:
// @ts-ignore
// eslint-disable-next-line
try { require; } catch(e) { require = importModule; }
const lib = require('./lib/lib.js');The lib variable will then contain the library, with functions to:
- Request complex input (date/times, choosing from a set, files/folders, etc.)
- Download/Upload Data
- Reading/writing to the filesystem (text or binary)
- Displaying notifications, text logs, rich HTML, or rendered Markdown
For a full list of functions and constants in the bridge library, see the API documentation.
- Download Scriptable on an iOS/iPadOS Device
- On a Mac: Copy this
libdirectory into theScriptableiCloud directory - On a Mac: Put this code in
~/.zshrc:
export PATH_SCRIPTABLE=~/Library/Mobile\ Documents/iCloud~dk~simonbs~Scriptable/Documents
for file in $PATH_SCRIPTABLE/*.js; do
alias $(basename $file | tr '[A-Z]' '[a-z]' | tr ' ' '-')="node \"$file\""
doneOnce done, all scripts will be aliased for use in the CLI in lower-kebab-case. For instance, if a script is called Test File Name.js, a user could run: test-file-name.js --help.
Also, at this point, all scripts can be run in the Scriptable app on iOS.
For each script whose getInput(...) call specifies share: true, create a shortcut. That shortcut should accept strings and urls, so that it displays on the share sheet. It should have the following steps:
- Dictionary (↓) → Run shortcut (Scriptable Harness[2]), Input: (↪)[3]
- (script) (Text) → (ScriptName)[1]
- (share) (Text) → (
$SHORTCUT-INPUT)
For each script that does not need to show up on a share sheet, create a shortcut. That shortcut should accept no input, and it should have the following steps:
- Dictionary (↓) → Run shortcut (Scriptable Harness[2]), Input: (↪)[3]
- (script) (Text) → (ScriptName)[1]
- (share) (Text) → ()
That shortcut can be added to the home screen.
Notes:
- "ScriptName" is the script to run. For example:
Test File Nameto run:Test File Name.js. - This requires having created the "Scriptable Harness" shortcut described in the "Helper Shortcuts" section below.
- ↪ always refers to the result of the previous item in the same scope, including "Repeat Results" when referenced right outside/after a "Repeat".
export PATH="{{PATH}}"
node "/Path/To/This/Repository/{{NAME}}.js" --shortcuts-args "$1" --shortcuts-mode "{{MODE}}"Notes:
- The
{{PATH}}text should be replaced by pasting after runningecho $PATH | pbcopyin a terminal. - The
{{NAME}}text should be replaced with the shortcuts$namevariable block. - The
{{MODE}}text should be replaced with the shortcuts$modevariable block.
- Set variable (input) to (
$SHORTCUT-INPUT) - Get (Value) for (name) in (
$input) → Set variable (name) to (↪) - Get (Value) for (args) in (
$input) → Set variable (args) to (↪) - Get (Value) for (mode) in (
$input) → Set variable (mode) to (↪) - Get (Value) for (inScriptable) in (
$input) → Set variable (inScriptable) to (↪ as boolean) - If (OS) (is) (macOS)
- Run Shell Script (
BASH SCRIPT IN SECTION ABOVE), Shell: (zsh), Input: ($args), Pass Input: (as arguments), Run as Administrator: (☐)
- Run Shell Script (
- Otherwise
- Get file from (iCloud → Scriptable) at path (
$name.js), Error if Not Found: (☑) →$MAGIC-SCRIPTABLE-FILE - If
$inScriptable- [Scriptable] Run Inline Script (
$MAGIC-SCRIPTABLE-FILE as text), Texts: [$args,$mode], Run In App: (☑), Show When Run: (☑)
- [Scriptable] Run Inline Script (
- Otherwise
- [Scriptable] Run Inline Script (
$MAGIC-SCRIPTABLE-FILE as text), Texts: [$args,$mode], Run In App: (☐), Show When Run: (☐)
- [Scriptable] Run Inline Script (
- Get file from (iCloud → Scriptable) at path (
- Set variable (input) to (
$SHORTCUT-INPUT) - Get (Value) for (type) in (
$input) → Replace (pathF) with (F) in (↪) → Text(↪s) → Set variable (type) to (↪) - Get (Value) for (bookmarkName) in (
$input) → Set variable (bookmarkName) to (↪) - Select File/Folder (
$type), Select Multiple: (☐) →$MAGIC-FILE - If (OS) (is) (macOS)
- Stop and output (
$MAGIC-FILE as File Path)
- Stop and output (
- Otherwise
- [Scriptable] Create bookmark named (
$bookmarkName) for ($MAGIC-FILE) - Stop and output
$bookmarkName
- [Scriptable] Create bookmark named (
- Set variable (inputDictionary) to (
$SHORTCUT-INPUT) - Get (Value) for (script) in (
$inputDictionary) → Set variable (script) to (↪) - Get (Value) for (share) in (
$inputDictionary) → Set variable (share-raw) to (↪) - Text (
$share-raw) → Set variable (share-input) to (↪) - Dictionary () → Set variable (output) to (↪)
- Get file from (iCloud → Scriptable → args) at path (
$script.json), Error If Not Found: (☐) →$MAGIC-FILE-ARGS - If (
$MAGIC-FILE-ARGS) (has any value)- Set variable (argJsonString) to
$MAGIC-FILE-ARGS
- Set variable (argJsonString) to
- Otherwise
- Dictionary (↓) → Run (Scriptable Harness Run JS), Input: (↪) → Set variable (argJsonString) to (↪)
- (name) (Text) → (
$script) - (args) (Text) → ({})
- (mode) (Text) → (shortcuts.getArgs)
- (inScriptable) (Boolean) → (False)
- (name) (Text) → (
- Dictionary (↓) → Run (Scriptable Harness Run JS), Input: (↪) → Set variable (argJsonString) to (↪)
- Get dictionary from (
$argJsonString) →$MAGIC-ARGS - Get (Value) for (inScriptable) in (
$MAGIC-ARGS) → Set variable (inScriptable) to (↪) - Get (Value) for (outputType) in (
$MAGIC-ARGS) → Text (↪) → Set variable (outputType) to (↪) - Get (Value) for (args) in (
$MAGIC-ARGS) - → Repeat with each item in (↪)
- // Parse Argument Properties
- Get (Value) for (name) in (
$REPEAT-ITEM) → Text (↪) → Set variable (name) to (↪) - Get (Value) for (type) in (
$REPEAT-ITEM) → Text (↪) → Set variable (type) to (↪) - Get (Value) for (help) in (
$REPEAT-ITEM) → Text (↪) → Set variable (help) to (↪) - Get (Value) for (bookmarkName) in (
$REPEAT-ITEM) → Text (↪) → Set variable (bookmarkName) to (↪) - Get (Value) for (share) in (
$REPEAT-ITEM) → Text (↪) → Set variable (share) to (↪) - // Argument is SHARE
- If (
$share) (is) (Yes)- Match (^\s+$) in (
$share-input) →$MAGIC-MATCH - If (All) are true: (
$MAGIC-MATCH as text) (does not have any value) and ($share-input) (is not) ()- Set (
$name) to ($share-input) in ($output) → Set variable (output) to (↪) - Text (share) → Set variable (type) to (↪)
- Set (
- Match (^\s+$) in (
- // Argument is PATHFILE or PATHFOLDER
- If Any are true: (
$type) is (pathFile) or ($type) is (pathFolder)- Show alert (
$help), Title: ($name), Show Cancel Button: (☐) - Dictionary (↓) → Run (Scriptable Harness Get File), Input: (↪)
- (type) (Text) → (
$type) - (bookmarkName) (Text) → (
$bookmarkName)
- (type) (Text) → (
- → Set (
$name) to (↪) in$output→ Set variable (output) to (↪)
- Show alert (
- // Argument is ENUM
- If (
$type) is (enum)- Dictionary () → Set variable (enumMap) to (↪)
- Get (Value) for (choices) in (
$REPEAT-ITEM) - → Repeat with each item in (↪)
- Get (Value) for (title) in (
$REPEAT-ITEM-2) →$MAGIC-enumTitle - Get (Value) for (code) in (
$REPEAT-ITEM-2) →$MAGIC-enumCode - Set (
$MAGIC-enumTitle) to ($MAGIC-enumCode) in$enumMap→ Set variable (enumMap) to (↪) - Text (
$MAGIC-enumTitle)
- Get (Value) for (title) in (
- → Choose from (↪), Prompt (
$name:$help) - → Get (Value) for (↪) in (
enumMap) - → Set (
$name) to (↪) in$output→ Set variable (output) to (↪)
- // Argument is STRING
- If (
$type) (is) (string)- Ask for (Text) with (
$name:$help) - → Set (
$name) to (↪) in ($output) → Set variable (output) to (↪)
- Ask for (Text) with (
- // Argument is BOOLEAN
- If (
$type) (is) (boolean)- Dictionary (↓) →
$MAGIC-BOOLEANS- (True) (Boolean) → (True)
- (False) (Boolean) → (False)
- → Choose from (↪ as Keys), Prompt: (
$name:$help) → Get (Value) for (↪) in ($MAGIC-BOOLEANS) - → Set (
$name) to (↪) in ($output) → Set variable (output) to (↪)
- Dictionary (↓) →
- // Argument is DATE
- If (
$type) (is) (date)- Ask for (Date and Time) with (
$name:$help), Default Date and Time: ($CURRENT-DATE) - → Format (↪), Date Format: (RFC 2822)
- → Set (
$name) to (↪) in ($output) → Set variable (output) to (↪)
- Ask for (Date and Time) with (
- Dictionary (↓) → Run (Scriptable Harness Run JS), Input: (↪) →
$MAGIC-JS-OUTPUT- (name) (Text) → (
$script) - (mode) (Text) → (shortcuts.setArgs)
- (inScriptable) (Boolean) → (
$inScriptable) - (args) (Text) → (
$output)
- (name) (Text) → (
- Show notification (
$MAGIC-JS-OUTPUT) - If (
$outputType) (is) (data)- Copy (
$MAGIC-JS-OUTPUT) to clipboard
- Copy (
Accepts: Text
- Text: "
$SHORTCUT-INPUT" - Set name of (
$PREVIOUS) to (Text) More(Don't Include File Extension: (On)) - Save (
$PREVIOUS) Service: (iCloud Drive) Ask Where to Save: (On)