Invoking AppleScript from iOS Shortcuts

how-to
shortcuts

(Mark Alldritt) #1

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.


(CK) #2

If you keep your scripts in iCloud, you can even use the iOS file picker to pick an AppleScript to execute, which your system (assuming it has the same iCloud account privileges as your iDevice) can access.


#3

I tried to get up to speed on these, but in the WWDC video all they were talking about was how to ADD Shortcut support to your iOS app.

I’m still trying to figure out if these are somehow developed on the iOS device (does iOS even have AppleScript? If so, where do you write these scripts?) or in Xcode and transferred, or what. So far, all I’ve been able to do is see some shortcuts in Settings app and not be able to delete them from the Shortcuts app.


(Mark Alldritt) #4

AppleScript exists only on the Mac. My examples above show how to cause Shortcuts to run an AppleScript on a Mac and obtain a result. The AppleScript is written and runs on the Mac. You can use Script Debugger or Apple’s Script Editor to create the Script on the Mac.


#5

Ahh, thanks for the help.