Define the application to send keystrokes to via a variable or per macro

Hello,

TL;DR:
What I would like to do is define the send-to application for keystrokes via a variable, or some method to specify a "default" application for a group or a specific macro, or frankly any other method to easily specify and change which application to send my keystrokes to.


I am using the "Send to" feature to send keystrokes to a background application.

This works fine when I manually select the application I wish to send the keystrokes to for each keystroke I wish to send.

However, I have many keystrokes to send, and worse, I want to use the same macro with multiple applications - the only thing that has changed is the target application I wish to send the keystrokes to.

My current process involves making a copy of the macro, and then manually updating each "send keystrokes" command to the new application. This is time consuming and prone to human error.

What I would like to do is define the send-to application for keystrokes via a variable, or some method to specify a "default" application for a group or a specific macro, or frankly any other method to easily specify and change which application to send my keystrokes to.

Is anyone aware of a way to do this?

Thank you!

Dang, I've used KM for years and didn't even know that feature existed! Shows how feature-rich KM is.

To answer your question, there isn't a simple way to do this. But I could envision a JXA or AppleScript method where you got the XML for the macro, altered the pid (the process id), and ran the altered XML.

If this is something you're interested in, I could spend a little time to see what t would take. But only if you really want it. Sometimes people ask for things, and after I spend some time on the task, they end up ghosting me or just no longer needing it. And that sucks. :grinning_face_with_smiling_eyes:

Thanks for the response, Dan.

Yes, I do need a solution, and won't ghost on you. :slight_smile: However, before you spend too much effort on an AppleScript, I hadn't ever considered editing the macro directly as an XML.

Opening it up as text, I see this targeting section:

> <key>ActionUID</key>
> <integer>5654</integer>
> <key>IsDisclosed</key>
> <false/>
> <key>KeyCode</key>
> <integer>3</integer>
> <key>MacroActionType</key>
> <string>SimulateKeystroke</string>
> <key>Modifiers</key>
> <integer>6656</integer>
> <key>ReleaseAll</key>
> <false/>
> <key>TargetApplication</key>
> <dict>
> 	<key>BundleIdentifier</key>
> 	<string>org.blenderfoundation.blender</string>
> 	<key>Match</key>
> 	<string>Path</string>
> 	<key>Name</key>
> 	<string>Blender copy 2</string>
> 	<key>NewFile</key>
> 	<string>/Applications/Blender copy 2.app</string>
> </dict>
> <key>TargetingType</key>
> <string>Specific</string>

I am identifying the target app using the "Path" method. Is this as simple as just changing "Blender copy 2.app" to "Blender copy 3.app" and re-importing the macro?

If so, this simple find and replace method would be sufficient for my needs, no AppleScript required.

Thanks!

Attached is a simple example of what one my macros looks like (each one usually has 10-20 "send keystroke" commands, I just left one here for simplicity).

Blender 2.kmmacros (2.9 KB)

Yes, generally speaking, it's that simple. Obviously you have to be careful not to change the wrong thing, but I think in the case of an application path, that's probably not an issue.

And by the way, if you do change the wrong thing, KM just won't import it. Unfortunately it won't tell you why it didn't import it, but that's how it works.

There's a way to do it at runtime by reading a macro's XML, modifying it on the fly, and having the KM Engine run the modified XML, but it's a lot more work.

Thanks a lot. I had to update all my macros to make sure they all were using "by path" for identifying the application, most were but there were a few sneaky ones in there. One I got that sorted it worked fine to edit the raw XML - a simple search and replace now allows me to switch which application my macros target. Very helpful.

Thanks for setting me on the right path! This will save me a ton of time, and I can now add "editing the XML directly" to my warchest of Keyboard Maestro tools.

Cheers!

Glad to help. If you haven't seen this already, it may be useful:

Thank you, I had not seen that and it's a perfect compliment to my new macro-editing workflow. Thanks again!

1 Like