I'm currently using Ventura 13.0, Developer beta 3 (22A5295i). @rolian reported that the macros and shortcuts below behave as expected using Monterey 12.5.
2022-07-24 Update: Please note that this post and the comments in the example macros and shortcuts have been updated based on information provided by @peternlewis.
The latest episode of the Automators podcast is entitled Automation Interoperation. It's outstanding and I'm sure many of you would find interesting.
At 53:05 in the episode the hosts, Rosemary Orchard and David Sparks, laud Keyboard Maestro for its comprehensive support of macOS Shortcuts.
I've written many shortcuts (primarily is iOS) and Keyboard Maestro macros, but I hadn't taken the time to explore this new interoperation (available in KM v10.1+). However, the podcast episode and a recent post here, Getting Started with macOS Shortcuts, motivated me to give it a try. As I started testing, I was primarily interested in the methods to transfer data. During that testing, here's what I initially found:
-
With the KM macro Execute Shortcut action, text data can be transferred to a shortcut via the With input from setting and retrieved in the shortcut via the Shortcut Input.
-
If a shortcut ends with a Text action (or Stop and output –Text action), the text value is passed back to a KM macro and retrieved using the Execute Shortcut action, Save to variable setting.
-
When reading a macro variable in a shortcut using the Get Variable action (provided by the Keyboard Maestro application), the KM variable must be global unless the %ExecutingInstance% is provided by the macro and used in the shortcut action.
-
When writing a macro variable in a shortcut using the Set Variable action (provided by the Keyboard Maestro application), the KM variable must be global unless the %ExecutingInstance% is provided by the macro and used in the shortcut action.
-
The Set Variable action (provided by the Keyboard Maestro application) will create a global, local, or instance variable if it does not exist in the macro. If it does exist, the the action will overwrite the variable value.
Since I’m a big fan of Keyboard Maestro Dictionaries and since I’ve also used them extenstively within shortcuts, I was curious if they could be used in the aforementioned Execute Shortcut action with the With input from and Save to variable settings. To my delite, I discovered the following:
-
With the KM macro Execute Shortcut action, JSON data can be transferred to a shortcut via the With input from setting and retrieved in the shortcut via the Shortcut Input. In the shortcut, the JSON can be coerced into a dictionary using the Get dictionary from action.
-
If a shortcut ends with a Dictionary action (or Stop and output –Dictionary action), the equivalent JSON value is passed back to a KM macro and retrieved using the Execute Shortcut action, Save to variable setting. In the macro, the JSON can be coerced into a dictionary using the Set Dictionary to JSON action.
The advantage I see in this approach is that one can transfer all values from/to a macro to/from shortcut using the single Execute Shortcut action. Another advantage, that might be consequential in some situations, is that the results are returned more quickly (approximately 25% reduction).
Dictionaries are global and it is imperative that dictionary names be unique (to avoid potential collisions with other macros or even a concurrently running instance of this macro). See the third macro below for the approach I use.
If you are interested in experimenting with these data sharing features, download one or more of the following macro/shortcut pairs. The first and second pairs cover the first five bullets above. The third pair is for the sixth and seventh bullet. I've included several comments in each macro and shortcut.
Pair A
In this example values are passed to and retrieved from a shortcut using the Execute Shortcut action. In addition, macro global variables are read and written using shortcut actions provided by the Keyboard Maestro application.
See bullets one to five above and the embedded comments for more information.
DOWNLOAD Macro File:
Get Current Weather Conditions.kmmacros (29 KB)
DOWNLOAD Shortcut:
Weather Conditions at Specified Location
Pair B
This example is the same as Pair A except that local variables are read and written using shortcut actions provided by the Keyboard Maestro application.
See bullets one to five above and the embedded comments for more information.
DOWNLOAD Macro File:
Get Current Weather Conditions—INSTANCE.kmmacros (29 KB)
DOWNLOAD Shortcut:
Weather Conditions at Specified Location—INSTANCE
Pair C
In this example all values are passed to and retrieved from a shortcut using the Execute Shortcut action. This approach does not require the shortcut actions provided by the Keyboard Maestro application.
See bullets six to seven above and the embedded comments for more information.
DOWNLOAD Macro File:
Get Current Weather Conditions—JSON.kmmacros (30 KB)
DOWNLOAD Shortcut:
Weather Conditions at Specified Location—JSON
Please comment if you use other methods of sharing data between macros and shortcuts.