Six Colors
Six Colors

by Jason Snell & Dan Moren

This Week's Sponsor

End users aren't your enemy! Kolide gets users to fix their own device compliance problems–and unsecure devices can't log in. Click here to learn how.

By Jason Snell

Building cross-platform shortcuts

Note: This story has not been updated since 2021.

As seen on my Mac: Two cross-platform shortcuts.

Shortcuts has arrived on the Mac with macOS Monterey, and while the new Shortcuts app is pretty weird, it works—and it brings most of the functionality from Shortcuts on iOS along with it.

As I’ve written about before, building Shortcuts is frequently much easier than building similar workflows on macOS using AppleScript and Automator. Now that Shortcuts is on the Mac, there’s no need to replicate shortcuts from iOS using those technologies! Instead, the same shortcut can run on both platforms. Problem solved.

Err… well… okay. Maybe not solved. While you might have rightfully assumed that not everything that works in Shortcuts on the Mac will work in Shortcuts on iOS—AppleScripts, shell scripts, and the like simply don’t exist on that platform—it might surprise you to learn that the reverse is also true. The Mac’s version of the Share menu is not nearly as sophisticated as the one on iOS, and the Share menu is a major launching point for iOS shortcuts. Safari’s automation interface is, similarly, not nearly as friendly on the Mac as on iOS.

But will that stop us? No! This week I’ve converted several of my shortcuts to “universal” versions that run on both macOS and iOS. Here are some tips and tricks that I’ve learned along the way.

If this is a Mac…

The most important trick in building cross-platform shortcuts is to wall off platform-specific actions inside an If block.

Shortcuts helpfully lets you control-click on the text area to the right of If and choose Device Details from the resulting pop-up. Click on Device Details, then scroll down and choose Device Model in the Get area of the resulting pop-up window. Then set the rest of the conditional: is Mac.

You’ll see that Shortcuts automatically creates two more blocks: Otherwise and End If. All your Mac-specific commands go between If and Otherwise, and all your iOS-specific ones go between Otherwise and End If.

Here’s a nice tip: The output of the End If block is the output of the final block in the conditional. So if you want to pick up a single piece of output from your If statement, you can select the output of the statement as a Smart Variable and it’ll be the right one on each platform. (For more complex conditionals, you’ll need to explicitly set variables or build a list and output that, then parse it.)

Problems with input

Since the Mac doesn’t have a proper Shortcuts-savvy share sheet, you’ll need to get input into your shortcut by some other means on the Mac side. For my Safari-based shortcuts that extract information from selected text in the browser window, I took a chip from John Voorhees of MacStories and shifted to Services. On the Mac, you can kick off a shortcut on selected text by control-clicking on the selection and choosing a shortcut from the Services submenu. To enable your shortcut for Services, click on the Preferences icon in the Shortcuts sidebar of your shortcut, then check the Services Menu box.

In this case, I also needed to change what input my shortcut received, by clicking the area to the right of the Receive block, at the very top of the shortcut. I made sure my shortcut would receive text, rich text, Safari web pages, Articles, and URLs.

Then there’s processing the input you’re given, which can be one of the most frustrating (but powerful!) parts of making Shortcuts. In this case of my Safari-based shortcuts, I need to create an If block that receives the page selection in Safari on iOS, but uses the rich text input from the Services menu on the Mac. It looks like this:

To make sure the input is properly received by the Make Markdown action, I have to click on Shortcut Input and explicitly set it to receive Rich Text. Then it works.

Problems with Safari

On iOS, when an item in Safari is shared with Shortcuts, it comes with a rich cloud of data, including the selected text, the URL of the page, the title of the page, and a lot more. On the Mac, since you’re using Services to grab that text, you get none of the rest of it! Fortunately, there is a Mac-specific function, Get current URL from Safari, that will do the equivalent.

It’s even trickier for getting the name of the current page in Safari. For that, we’ve got to use a Run AppleScript block with a very simple AppleScript script:

on run
    tell application "Safari" to return name of front document
end run

The name of the front document is the output of that block because it’s what is sent back by the return statement.

Problems with output

On iOS, one of my shortcuts opens the final result in a text editor. On the Mac, I’d like to open it automatically in MarsEdit, the blog editing app. To do this, I’ll need to use AppleScript, but I’m then prompted with a challenge: I need to pass three different pieces of information to the AppleScript script for it to do the job — the page title, the page URL, and the body text itself.

Fortunately, Shortcuts and AppleScript do speak the same language—sort of. The trick is to create a List inside the shortcut that contains the three pieces of information:

When this list is passed as input to the Run AppleScript command, it’s passed as an AppleScript list, which is easily referenced as individual items:

on run {input, parameters}

    tell application "MarsEdit"
        make new document
        tell document 1
            set title to (item 1 of input)
            set body to (item 2 of input)
            set custom field values to {{server field name:"link", value:(item 3 of input)}}            
            set category names to "Link"
        end tell
    end tell
end run

This is actually not any more complex than what is happening on the other side of the platform-sensing If statement, where all the text is being URL encoded, a filename is being formatted based on the page title, and the whole thing is being bundled up in an x-callback-url destined for 1Writer.

Does it work?

In the end, yes, you can make individual shortcuts that will execute properly on both macOS and iOS, so you can keep them in sync. In most cases, most of any given shortcut is identical across platforms—Apple has done a pretty good job of keeping behavior in sync. But when there’s something that’s simply not available on one platform or the other, you can usually find a workaround and stick it inside an If block.

If I have one complaint about the current state of cross-platform shortcuts, it’s that they’re not properly editable on either platform. AppleScript text isn’t even displayed on iOS—obviously it can’t be run, but shouldn’t I be able to see it?!—and if a shortcut uses an action from an iOS app that’s not installed on the Mac, those elements will also be opaque.

More work to do. But in the meantime, a lot of work that I did on iOS is coming over to my Mac with limited modifications, and that’s exciting. Building user automations is hard enough once; we shouldn’t need to do all of it twice.

[Here they are: Make linked list item and Make cross-promo post.]

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.

Search Six Colors