Launching macros from applications sometimes works, but sometimes doesn't

I am looking for a way to write small apps for the sole purpose of launching individual KBM macros. The obvious way is to write a bit of Applescript then turn it into an app. Sometimes that works, but sometimes it doesn't. Specifically it fails if you tell KBM to simulate a keystroke.

My conclusion after a bunch of careful troubleshooting: An application needs root permission to trigger a keystroke action in a Keyboard Maestro macro. This is not true for all actions.

Consider two macros. "Test Display Text" consists of a Display Text action to put a few words on the screen. "Test Keystroke" consists of a Type the Keystroke action to simulate ⌘N. These two macros are launched by two simple Applescripts, display.scpt, and keystroke.scpt.

display.scpt:

tell application "Keyboard Maestro Engine"
   do script "Test Display Text"
end tell

keystroke.scpt:

tell application "Keyboard Maestro Engine"
   do script "Test Keystroke"
end tell

Both of these work fine when run inside Script Debugger or when run from the command line using osascript.

But now save the two scripts as apps, display.app and keyboard.app. When clicked on in Finder or run with open -a in a command line, display.app works great. But keystroke.app fails, consistently, regardless of how it is executed.

The only way that I could find to turn a script into an app that would successfully execute the "Test Keystroke" macro was to build the application using Platypus and turn on the "run with root privileges" option. The problem there, of course, is that every time you run it you have to type in the root password, which sort of defeats the purpose.

Note that this really is about root privilege and not something in Accessibility. Apps that have been properly registered in System Preferences --> Security & Privacy --> Privacy --> Accessibility fail. But apps that have not been registered succeed when run with root privilege.

Just running an app with sudo open -a keystroke.app fails.

I'm running the current version of Catalina. Watching the console output, trying to execute an app that tells KBM to simulate a keystroke generates a ton of errors, most of which are related to the code being unsigned.

Any suggestions or insight would be appreciated. Again, my hope is to find a way to launch KBM macros -- in particular those that simulate keystrokes -- by running apps.

OK, I think that I just found a practical solution. Apps generated using Automator seem to work. But I'll leave this posted here because I'm curious about what I might learn, and because there might be other people who bump into similar problems.

I did some experiments, and it seems to me that it also depends on the keystroke that actually is executed by the action in the macro.

Keystroke ⌘M (minimizes front window)

  • Double-click AppleScript.app --> Nope (probably because the app has no window to minimize)
  • Run the applet inside the AppleScript.app bundle --> Yes
  • Run exported Macro Trigger File --> Yes

Keystroke βŒ₯⌘esc (calls the Force Quit window):

  • None of the above methods works, not even running the app or the applet as sudo

A Speak Text action, which is also in the macro, always runs.


PS:

Forget the second example. Just found out that the Keystroke βŒ₯⌘esc action never runs, not even if the macro is triggered directly or from within KM :neutral_face:

1 Like

Added another test case, with a keystroke that launches a Service from the Services menu (the Service is available in all apps), and the result is like with ⌘M:

Macro Trigger file works, running the applet inside the AppleScript.app works, but running the AppleScript.app does not work (as sudo neither).

1 Like

I don’t think root permissions has anything to do with anything here.

There are two reasons the macro would fail to do anything:

  • The macro does not trigger
  • The macro triggers, but the typed keystroke does not do anything.

It is easy to determine which case is happening by using the Interactive Help, Something expected is not happening. It will find any issues with the macro, and tell you if the macro is triggered.

If the macro is not triggered, there are a number of possible causes:

  • The macro group could be inactive. If you are launching an applet, remember that that might deactivate the macro group.
  • The script might not have permission to send AppleEvents to Keyboard Maestro Engine.

Since you say running some macros work, and presuming those macros are in the same macro group, then presumably this is not the issue.

If the macro is triggered, but fails to type the keystroke, then there are a number of possibilities:

  • Accessibility could block it. I strongly doubt this - if the macro is triggered by the AppleScript, it will proceed to run, but after that the system does not know that it was instigated by AppleScript. So if typed keys work normally, this is probably not the cause.
  • The key could be being sent to the applet - since the applet just launched, the system may be sending the key to it. In that case, the key would be handled by the applet, not the previously front application.

There are other possibilities, but those would be my starting points.

I highly doubt running the AppleScript with rood or admin privs will help.

Hey Peter,

Sorry for the slow reply. I've been out of pocket this week.

You are clearly right about root privilege not being the real problem. The fact that applications made using Automator run without any special permissions indicates that is not the real root of the issue. I had already pursued the paths you suggested when I posted originally. What clearly is the case is that different ways of turning scripts into apps result in very different behaviors when those apps are used to command KM. For whatever reason the way Platypus generates an app that wants to run with root permission works while the other ways Platypus (or Script Debugger) generate apps produces apps that fail.

I'm including a link to the notes that I took while trying to characterize the problem. I ran through the entire sequence twice with the same results, which doesn't mean I didn't overlook anything. Perhaps the issue will replicate for you and perhaps not.

Again, the conclusion in the PDF is incorrect, since unlike apps generated with Script Debugger and Platypus, apps generated with Automator do work without root privilege. So from a practical standpoint I found a way to accomplish what I set out to.

The tests were run on a late 2019 MacBook Pro running Catalina 10.15.3 and current versions of Script Debugger and Platypus.

A further peculiarity with inconsistent behavior seemed strange enough that I documented it with screen capture video. The link is included in the PDF.

Strangeness. By the way, fantastic job on KM!

Cheers,
Jeff

Tom,

I also ran into the issue with different keystrokes behaving differently when triggered from an app. I first ran across that when triggering macros to control Final Cut Pro X. Some keystrokes (for example the keystroke to control the zoom of the video timeline) would produce the desired result, while others (such as the keystroke to go into and out of full screen mode) would not. Although the command-control-F full screen keystroke would work in some other apps.

From a practical standpoint I'd be interested in knowing if any of your examples (or other examples) fail when triggered using an app generated with Automator. I have tried a dozen or so examples using Automator and so far all have worked. I'm hoping that turns out to always be the case.