Invoking AppleScript from iOS Shortcuts

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.

4 Likes

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.

2 Likes

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.

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.

1 Like

Ahh, thanks for the help.

Check out this example of Shortcuts and AppleScript integration in dougscripts.com:

https://dougscripts.com/itunes/2018/12/scripting-to-a-mac-with-ios-shortcuts/

2 Likes

So with the most recent shortcuts version (I’m not sure when it was added), you can choose an input in the “Run Script Over SSH”.

Simple question…
If I have a text value “What’s up MacBook” in my shortcut
and I want to pass that over to the AppleScript
how do I call it in the AppleScript? I understand I can get it using the method above, but is there a simple way to do this?

My end goal is that I would like to pass a dictionary to the AppleScript using the “Run Script Over SSH” command in shortcuts.

Thanks in advance!

You can place data in a dictionary and pass that dictionary as the input to the Run script over SSH action. The dictionary is converted to JSON by Shortcuts which you’ll need to decode in your script to recover the key/value pairs within it.

Sorry, my problem currently is getting that input with AppleScript

so I have in shortcuts:

Text
<“This is a test”>

"Run Script Over SSH"
osascript ~/Desktop/SSHScript.scpt

  • host: xxxx.local
  • Port: 22
  • user: xxx
  • Authentication: Password
  • Password: xxxxxx
  • Input . <— which is “This is a test”

how do I get that from AppleScript? so if I have a simple program…

on run
set mytext to
display dialog mytext
end run

I can do it by appending the text magic variable to the "osascript ~/Desktop/SSHScript.scpt "
but its a list, so I can’t just retrieve all of the text as 1 input
I’m thinking what I need is that input field mentioned above.

See this section of my original post: AppleScript presents arguments specified to the osascript command as a single parameter containing a list of strings passed to the on run handler.

Oh gotcha, just to make sure, so in the case of running an AppleScript with that SSH command, the input field at the bottom is pointless?

Thanks for ur quick replies btw!

I believe the input field is the same as the input to the action (i.e. preceding action).

Note that you have to be concerned about is spaces in the data being passed to the script. Unix will split items into separate arguments based on the presence of space characters in the input value. Quoting a string prevents this problem. I’m not sure if the JSON data is properly quoted or not - you’ll have to try it to find out.

I have done in the past some test their I script in command-line to execute actions on Keynote.app and later use Shortcuts on iOS to use ‘Run Script Over SSH’. The test was faster and to use a client/server solution that some vendor approach is. But I got a very fast solution to pipe midi message to execute command on macOS. And many times when the api is open it become more integrated and properly much faster and it would be to inventing new api and hardware that only limit the use.

If you are curious ‘Open Stage Control’ let you build a interface to be access from ip
on remote client device. The client send midi message to the server and other app could listen to midi message and execute AS command. This process is very fast.

Oof I just read this, but yeah… it was the spaces and other characters… I created an encode dictionary shortcut that converts a dictionary (shortcut input) to a passable string, then the AppleScript has a decode function I can post later. Super basic and buggy, but it’s a start… if anyone has something better, please share!

Text (shortcut input )
Replace : with / in Text
Replace [ with { in UPDATED text
Replace ] with } in UPDATED text
Return UPDATED text