Keyboard Maestro and macOS Shortcuts—Data Transfer Examples

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.

Macro Output-image


DOWNLOAD Macro File:
Get Current Weather Conditions.kmmacros (29 KB)

Macro-image


DOWNLOAD Shortcut:
Weather Conditions at Specified Location

Shortcut-image


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.

Macro Output-image


DOWNLOAD Macro File:
Get Current Weather Conditions—INSTANCE.kmmacros (29 KB)

Macro-image


DOWNLOAD Shortcut:
Weather Conditions at Specified Location—INSTANCE

Shortcut-image


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.

Macro Output-image


DOWNLOAD Macro File:
Get Current Weather Conditions—JSON.kmmacros (30 KB)

Macro-image


DOWNLOAD Shortcut:
Weather Conditions at Specified Location—JSON

Shortcut-image


Please comment if you use other methods of sharing data between macros and shortcuts.

9 Likes

I've updated the OP with Version 1.1 of the second macro.

I had an oops in v1.0 and forgot to cleanup by deleting the dictionaries created. This modification does not affect the displayed result, but I always like to delete dictionaries unless I intentionlly want the values to persist.

Here’s what has been added:

Keyboard Maestro Export

These all worked fine for me in Monterey 12.5. Thanks for the coherent and thorough explanation

1 Like

Ah, great; thanks for testing the macros/shortcuts and reporting back.

You're welcome, @rolian.

Morning Jim (at least where I am haha), thanks for this bit of information. The way the Wiki entry is worded makes it seem to me this is indeed possible... but I can't figure out how...

Shortcut Actions [Keyboard Maestro Wiki]

Wiki Entry Screenshot (click to expand/collapse)

This has me banging my head against the proverbial wall for some time now because I believe there is a way to pass non-global variables to a Shortcuts action... even the actions themselves to me give the appearance to be able to accept local/instance variables...

macOS Shortcuts Action Screenshot (click to expand/collapse)

...but I can't figure out how. Likely I am just missing something but is there indeed a proper way to accomplish passing a local or instance variable to a Shortcuts action?

Thanks again for this awesome write-up... I haven't finished reading it all (gotta wait for my coffee for that :sweat_smile:) but i'll keep plugging along.

-Chris

1 Like

Hi, Chris; it's morning also in my neck of the woods (IN, USA).

I agree, based on the wiki it seems that it should be possible:



But unless there is some special syntax that I've missed, I don't think it is possible using the Shortcuts application actions Get Variable and Set Variable (both provided by the Keyboard Maestro application). Bear in mind, however, I was using Ventura, Developer beta 3.

In contrast, I was able to pass a local variable via the Execute Shortcut via the With input from setting. See Pair 1.

Moreover, if you want to pass multiple local variables using that technique, see Pair 2.

1 Like

Howdy neighbor! I'm in Ohio. :grin:

Ok that makes sense because I was able to pass an instance variable via the shortcut input part but have not found a way to pass multiple local/instance variables like what we can do in AppleScript and Shell Scripts etc.

Tagging @peternlewis to see if he can chime in and confirm/deny our understanding on this when he has a moment.

In the meantime I'll take a look at Pair 2. Thanks again Jim, always nice to see your contributions here. :grin:

1 Like

Good move; it wouldn’t be the first time that he’s set me straight. :grinning:

P.S. I grew up in a sleepy suburb of Columbus, OH. With all the growth in that area, I’d now call it bustling.

1 Like

The various Keyboard Maestro shortcuts like Get Variable, Set Variable, Calculate and Process Tokens, all take optional instance values which reference a particular macro execution instance.

If you pass them in to the shortcut, you can then reference local or instance variables.

For example:

and:

Keyboard Maestro Input is the output value of the Get variable shortcut entry.

4 Likes

Peter, thanks for providing the example. That makes perfect sense now.

I will update my post above using this information. Unfortunately I may not get to it for a few days. I've updated the original post based on your information.

1 Like

Thank you Peter for the ELI5 explanation. I'm going to monkey around with some test macros so as to wrap my head around it now! :grin:

2 Likes

@cdthomer and @rolian, I've updated the examples above based on the information shared by @peternlewis.

3 Likes

Is there an easy way for Shortcuts to pass a variable to a KM macro?

I think I understand how to pass variable KM->Shortcuts but not the other way around.

Thanks.

Hi, @bw352. The three examples above (Pair A, Pair B, and Pair C) pass data both ways, but all are initiated using the respective Keyboard Maestro macro.

I suspect what you are asking here is how can a macOS shortcut initiate the process and, when initiated, pass some information to the Keyboard Maestro macro.

Thanks to the Shortcut app actions that Keyboard Maestro provides (in this case, the one that is required is Execute Macro), this is very straightforward. See below.


Pair D

In this example, a macOS shortcut runs and triggers a Keyboard Maestro macro. When the macro is triggered, text is passed from the shortcut to the macro. This is done using the Execute Macro action provided by the Keyboard Maestro application. The text is passed using the Parameter field in the action. The macro then retrieves the passed text using the Keyboard Maestro %TriggerValue% token.

Macro Output-image


DOWNLOAD Shortcut:
Weather Conditions at Specified Location—Pass to a KM Macro

Shortcut-image


DOWNLOAD Macro File:
Get Current Weather Conditions—From a Shortcut.kmmacros (9.2 KB)

Macro-image

1 Like