dougscripts.com

December 1 2018 - 11:54 am

Scripting to a Mac with iOS Shortcuts

I’m not particularly bowled over by Shortcuts on iOS. I just don’t use my iPhone and iPads rigorously enough that I’m inspired to automate many tasks. But I get that it’s a thing.

What I find very interesting however is that you can use Shortcuts to launch AppleScripts on a targeted Mac. So, while the “Music” actions on iOS are somewhat limited, there’s actually some potential usefulness in calling scripts (via Siri, even) from your iOS device to control iTunes on a Mac.

A nice basic tutorial by the great folks at Late Night Software explains how set this up. Essentially, you use the Run Script over SSH action to enter an osascript command to launch a script on a specified Mac. If you’ve ever done anything with launchd then you will recognize some similarities.

There are some limitations. First, AppleScript does not run on iOS. So anything you want to do with this technique will necessarily be on one particular Mac. (While I can imagine that it is possible to run other SSH commands from that Mac to other Macs, making all of that swing is beyond the scope of this article.)

Second, I am working strictly with scripting iTunes on a Mac. Many other apps on your Mac can be controlled with AppleScript, too.

Third, there is limited opportunity for user-interaction and feedback. (Depending on the use case, it is possible to manage some simple back-and-forth user-interaction in Shortcuts. But, again: beyond, scope, article.)

So, considering those parameters, there aren’t many iTunes track and playlist management things I’d want to do remotely; I’d prefer to be operating on track info while sitting in front of iTunes. But we can still use Shortcuts to perform some serviceable tasks with iTunes remotely, things that can’t be done conveniently with the Remote app on iOS.

Below, I describe how to create some Shortcuts that can launch scripts on your Mac. One will save the currently playing track to a “Favorites” playlist, another to change the AirPlay configuration and, lastly, one that pauses iTunes for five minutes. These may give you ideas for your own Shortcuts.

I’ll presume you’re already familiar with Shortcuts to a basic degree (like I am). So, using Late Night Software’s example, here’s how my Shortcut will look when I set it up on my iPad:

Tap “Create Shortcut” from the Shortcuts Library and drag the Run Script Over SSH action to it. It’s best to locate it with the Search field. Enter the Host name for the computer you want to target. You can find this name in the Sharing panel of that computer’s System Preferences. Don’t change the Port number (unless you know better). Enter the User name and Password for the account to target.

In the bottom section, that will say “Script”, enter “osascript”, a space and the path to the script that will be fired (we’ll get to those details in a moment).

You can do some other customizing in the Settings panel (tap the Preferences button in the top right of the screen):

I’ve made this Shortcut able to be activated by Siri. You can see there are also options for launching it in other ways. Do as you will here.

The script that this Shortcut will launch over SSH with the osascript command is called “save_fave.scpt” and this script is saved in the home folder on my iMac. When run, it will copy the currently playing iTunes track to a discrete playlist named “My Favorites”, creating the playlist if necessary. Here’s the script:

— change the name from "My Favorites" here if you like

property favePlaylist : "My Favorites"

tell application "iTunes"

if player state is playing then

set dbid to database ID of current track

if not (exists playlist favePlaylist) then

make new user playlist with properties {name:favePlaylist}

end if

if not (exists (some track of playlist favePlaylist whose database ID is dbid)) then

try

duplicate current track to playlist favePlaylist

end try

end if

end if

end tell

Click the AppleScript icon above to (eventually) open this in Script Editor or copy and paste the text above to a new Script Editor document. Save the script named “save_fave” with Format set to “Script” in your home folder.

Technically, you can name and save the script pretty much wherever you want, as long as you correctly enter its path in the Shortcut. Also, don’t use any spaces in the file path unless you escape them.

By the way, I have seen examples of this where actual AppleScript is typed into the “Script” panel. While this is, uhm, handy, I would discourage writing osascripts with the “-e” flag. Escaping quotes and other nonsense is just too messy.

So, everything is now set up. While a track that you like is playing on iTunes, tell Siri on your iOS device to “Save Fave” and the script will run. The first time the script runs, you may want to be nearby the Mac as it may ask your permission to launch the script. This will not happen later, unless you modify the script.

Using the basic template I used for the Save Fave Shortcut, I’ll create another, this time to Swap AirPlay configurations on my iMac:

tell application "iTunes"

set currentDevices to current AirPlay devices

set currentNames to {}

repeat with aDevice in currentDevices

set end of currentNames to (get aDevice’s name)

end repeat

if currentNames contains "Kitchen Express" then

set current AirPlay devices to {AirPlay device "Doug’s iMac", AirPlay device "Southern Express}

else

set current AirPlay devices to {AirPlay device "Dining Express", AirPlay device "Kitchen Express"}

end if

end tell

How I’d use this: I’m playing iTunes and sending audio over AirPlay to sets of Airport Expresses hooked up to speakers. In one case, I like the music only in the Kitchen and Dining room. But in another, I like the music over my Mac’s audio system in my office and an Airport Express in the Living Room (“Southern Express”). When I run this script, if it detects that the “Kitchen Express” is one of the current AirPlay devices, it will switch to the other set; and visa versa. Suppose I just tell Siri to “Swap Airplay”. Obviously, to use this yourself, you will have to hard-code the names of your AirPlay devices.

Finally, here’s a script where you can tell Siri to “Take Five”— pause iTunes on the Mac for five minutes and then play again:

tell application "iTunes"

if player state in playing then

pause

delay 300

play

end if

end tell

One note: my paranoid security buddies have not yet weighed-in on any evil-doing this sort of arrangement could induce; one can imagine some kind of code injection or something is possible. On the other hand, SSH is pretty secure. But, if you think doing something like this may expose you to evil-doers, then refrain.

Site contents © 2001 - 2018 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not interrupt the one doing it.