Invoking AppleScript from iOS Shortcuts
It may seem surprising but its actually really easy to invoke AppleScript from an iOS Shortcut script.
All of the examples I show below use the Run Script Over SSH
Shortcuts action in combination with the macOS’s osascript
command line tool to invoke AppleScript.
Invoke A Script
I’ll start with the simplest case: invoking a script which returns a string response.
Here’s the AppleScript code I’m going to execute:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- The value returned by the script's run handler is returned to Shortcuts
"Hello World"
The Shortcut that invokes this AppleScript script looks like this:
Invoke A Script And Pass A Parameter
The next challenge is passing parameter values into your AppleScript from iOS. Fortunately, the osascript
command allows for this by passing all parameters after the script path to the script’s on run
handler.
Here’s the AppleScript code I’m going to execute:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run args
-- The value returned by the script's run handler is returned to Shortcuts
return "Hello " & item 1 of args
end run
The Shortcut that invokes this AppleScript script looks like this:
Note how I insert the argument into the osascript
command using a Shortcuts magic variable. Be aware that if your parameter value contains spaces, the string will be passed as multiple arguments to your script. You can avoid this by enclosing the magic variable in quotes.
Invoke A Script Returning A Record
The final challenge is to return a complex result. In this example, I return an AppleScript record to Shortcuts by encoding it as a JSON string. Shortcuts provides facilities for converting a JSON string into a dictionary making it possible to extract property values from the AppleScript script’s result.
Here’s the AppleScript code I’m going to execute:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
-- An AppleScript data structure to convert to JSON
set theResponse to {|aString|:"Hello World", |anInteger|:1234, |aBoolean|:true, |aNull|:missing value, |anArray|:{"String 1", "String 2", "String 3"}}
-- Convert to a JSON string
set theJSONData to current application's NSJSONSerialization's dataWithJSONObject:theResponse options:(current application's NSJSONWritingPrettyPrinted) |error|:(missing value)
set theJSONString to ((current application's NSString's alloc)'s initWithData:theJSONData encoding:0) as string
return theJSONString
The Shortcut that invokes this AppleScript script looks like this:
In this script I invoke the AppleScript. Then, I convert the script’s result into a Dictionary and then extract one of the properties (aString
) from the dictionary.
You can experiment by changing the key name in the Get Dictionary Value
action to access different parts of the AppleScript record: aString
, anInteger
, aBoolean
, aNull
, and anArray
.