Use Shortcuts (iOS) to Trigger Macro Remotely [Featured "Choose from Macro List"]

Edit (09/13/2021, 04:49 PM ET):

  • Improvement: Change kmmacrolist.txt to kmmacrolist.json and use JSON instead of Plaintext.

Using Shortcuts to trigger KM macro remotely is not a new thing. (See e.g., this post).

However, what I'm going to share is to let Shortcuts show a list of macros for us to choose from.
This is how it looks like:

2021-09-13 17-15-20.2021-09-13 17_16_41

I. Setup

To accomplish this task, we need do to at least three things:

1. Create a Macro List File.

For instance, I created a file named kmmacrolist.json and placed it in my dropbox folder: ~/Dropbox/Scripting/KM/Shortcuts/kmmacrolist.json.

In this file, I have a list of KM macros. Each line is a Description of the macro, followed by a comma and a space, and then lastly its UUID, something like this:
image

Note:

  • The Description can be any memorable text. It does not have to be the name of the macro, since we are going to trigger it with the UUID.
    • I'm using UUID instead of name because most of the macro names are prefixed by a number, e.g., 03) Macro Name. It is unnecessary to show these prefixed numbers in the list of the Shortcuts.

2. Create a Shortcut in iOS Device

Click to Download the Shortcuts.

See screenshot below. The Shortcut does the following:

  1. Get the KM Macro List file (kmmacrolist.json) from my Dropbox folder.
  2. Get the content of the file as Dictionary.
  3. Ask us to choose a macro from the dictionary list.
  4. Save the UUID of the chosen macro to a file named "execMacro.txt" in the Dropbox folder.
    • A KM macro monitors this folder and triggers the macro (See Point 3 below).

3. Create a KM Macro to Execute the Chosen Macro

Below is the macro for download.

  • Basically, it monitors my ~/Dropbox/Scripting/KM/Shortcuts folder. When a new file named execMacro.txt is added, it reads it's context (the UUID) and then triggers the macro. Afterwards, it deletes the file.

Shortcuts Macro Trigger.kmmacros (3.4 KB)

Note:

  • In the AppleScript, I trigger the macro with parameter Shortcuts. This allows me to do something with the macro being triggered. For instance, if I trigger the macro to check Airpods battery level from my iPad, I'll use Pushover to send the batter level result to my iPad only. Otherwise, I will just show on my Mac.

II. Benefit:

The benefit of this method are as follows:

  • All UUIDs are saved in our private Dropbox folder (you may use the iCloud folder). It is relatively safer than the Remote Trigger, which may be triggered by anyone.
  • Supposing our Dropbox (or iCloud) syncing is fast enough, it will be more reliable than the Remote Trigger, which is said to be not 100% reliable.
  • We only put a limited number of macros to be triggered remotely. It could arguably be quicker to locate the target macro than the Keyboard Maestro Control app. As you can see in my demo video above, I get the Pushover notification very quickly, which is the result of the macro being triggered.
  • We can easily update the macro list at anytime. For instance, we can make a macro to append the macroName and macroUUID of any selected macro to the kmmacrolist.txt file.
    • I'm also sharing this macro as well:

Add selected macro to kmmacrolist.json.kmmacros (8.5 KB)

III. More Tips:

  • Do not make the macro list too long, because
    • It will have negative speed affect on the Shortcuts (processing more lines).
    • It's not easy to choose a macro from a long list.
  • Instead, consider making multiple macro list files and make a separate Shortcuts for each macro list file. The only difference between these Shortcuts will be the path of the "Get File" action. This setup will then serve as a "pseudo macro group".

(PS: I personally do not have much use of it, but I just came up with this idea this afternoon and hope it can help others who might have good use of it.)

5 Likes

Hey @Martin is this using macOS Monterey? or iPad?

iPad.
Otherwise, no need for "remote trigger" :stuck_out_tongue_winking_eye:

1 Like

Update in the OP:

I have updated the OP to use kmmacrolist.json instead of kmmacrolist.txt in the Shortcuts. It simplifies the Shortcuts and has better appearance in the "Choose from Dictionary" action.

I also changed the KM macro to add selected macro to the kmmacrolist.json file.

1 Like

Hi @martin, can you tell me where I can find the pushover plugin you are using?

I use this one:

However, a single device (iPad in your case) cannot be selected:


here.

Hi Alex,

I'm using the same plug-in. But I edited the plug-in files. :wink:
Let me show you what I did:

  1. Edit the ==Keyboard Maestro Action.plist== file. Insert the below information to the file:
		<dict>
			<key>Label</key>
			<string>Device</string>
			<key>Type</key>
			<string>TokenString</string>
			<key>Default</key>
			<string>All</string>
		</dict>

image

  1. Edit the AppleScript file (==PushoverNotification.scpt==):

insert:

set Device to do shell script "echo $KMPARAM_Device"

img_shadow_soft

and

if (Device is not "All") or (Device is not "") then
	set curl to curl & "--form-string \"device=" & Device & "\" "
end if

img_shadow_soft

I have not tested fully, because I'm using only one device (iPad). But I think, with the above AppleScript, if the Device field in KM is set to All or blank, then the message will be pushed to all devices. Otherwise, set the field to a specified device that we set in our the Pushover account. See:

  • device - your user's device name to send the message directly to that device, rather than all of the user's devices (multiple devices may be separated by a comma)

from
https://pushover.net/api

1 Like

@martin thank you for the quick and detailed explanation :+1:
I will try it out for myself.

@martin in your gif I noticed that you get the KM icon displayed in the pushover notification.

Is that the way you want it? There is the possibility to use individual icons/images here.

2021_09_14_Support_1

2021-09-14_00-59-33

With the amount of notifications on my  devices, these are very helpful.

1 Like

Thanks for the great tip, Alex.
I set the icon by default in my Pushover Account:

I just signed up for Pushover today. I'm still in the trial period.

Attaching a customized picture looks great in the notification center. Within the Poshover app, however, the icon looks huge:

Is it how it is supposed to be like?

My png file is in 50x50 size:

image

@martin this is normal. You don't have to take into account the size of the images, because they are automatically adjusted when Pushover runs. The bigger, the better the quality of the display in the notification.

Here is my AirPos Pro icon:

At this link you can download it and try it out:

In the Pushover App it will look like this:

Video

1 Like

Looks great!
Thanks!

1 Like

Looks great!
What are the cases that the remote triggers are useful?

Thanks.

1 Like

I personally do not have much need of remote trigger right now. As @appleianer has mentioned in a few places on the forum, when connected to the same local network, it is more efficient to use a Shell command to trigger KM macros. The Shortcuts trigger is useful when we are not close to the Mac.

I can imagine it would be useful to those who have a work computer running in the company. They can then trigger KM macros on the work computer from home.

Another case is when someone has a Mac running as a server. They may want to do something with the server remotely. Such as enabling or disabling a certain service.

We can simply use it to check if the Mac is still running. If it's not running, the macro will not be triggered and it will not return anything. We may use other ways to first wake up Mac remotely.

I don't have need of any of these usages right now and have not tried any of them.

2 Likes

Hi @henryone, in the following posts times 2 examples, how I execute KM actions on my iMac via Siri Shortcuts. Thereby the scan is triggered via SSH action and the snapshot with the following alarm and the locking of the keyboard via iCloud Drive....

Please click on video in the posts. You will then come to Droplr and can see the videos in a better resolution

What I do on a daily basis is use Hey Siri to wake my Macs up from sleep and then use a Keyboard Maestro macro to unlock them.

1 Like