Create Keyboard Shortcut (macro) to Copy Selected Macro Shell Script (osascript) to Clipboard?

Hello,

I often copy the osascript (shell script) of a macro to the clipboard, and wonder if there would be a way to make this process more automatic, using a Keyboard Shortcut triggered macro to do so.

thank you very much

osascript -e 'tell application "Keyboard Maestro Engine" to do script "3B3B6C61-AE1F-4F87-832D-3842661072B1"'

image

Seems easy. All you need to do is get the macro itself to paste the value of %ExecutingThisMacroUUID% into the system clipboard.

You may not like my solution, but it's fairly easy. Just add a hotkey to your macro that puts the value of that token into the system clipboard. Like this:

Although this works, you may not like my solution. I can guess why, but there are things we can do ti fix your concerns with this solution.

One way to fix this is to add an IF action at the top of your macro that checks if the hotkey was pressed, and if so, then performs the action above, but if not, then performs whatever other code you want that macro to perform. But there are other ways besides this to make it work smoothly. If you tell me what you don't like about my solution, there's a good chance I can make it work more smoothly.

1 Like

Works perfectly. I just added it to my KBM specific palette to extract macro title, URL trigger, Edit and now Shell script.

Thank you VERY much

Where do you then use that snippet? I'm thinking that it would be more efficient to run a macro from the command line using the command line tool (assuming you're on KM v11), and from AppleScript using a direct AppleScript call.

osascript was the way to do it, and will be portable to older KM versions, but has the added resource cost of instantiating an AS environment within a shell instance. (Does that actually matter in these days of personal supercomputers? Probably not -- but the Command Line Tool doesn't get enough love IMO...)

All you have to do is change what you wrap the macro UUID with. Command line tool:

/Applications/Keyboard\ Maestro.app/Contents/MacOS/keyboardmaestro 5123DD72-CCB7-49A8-8835-C77D839EAE3C

or AS:

tell application "Keyboard Maestro Engine" to do script "5123DD72-CCB7-49A8-8835-C77D839EAE3C"
2 Likes

I often use the osascript (shell script) when I want to run a macro from Better Touch Tool , to trigger a KM macro with a BTT :globe_with_meridians: hotkey shortcut, key sequence, a gesture or a so-called trackpad drawing.
In this context, I am not sure if your answer applies. I am confused.
thank you for your reply.

Very much so.

At the moment you are doing the equivalent of

Hey BTT! Start a shell instance for me
   Hey BTT's shell instance! Start an AppleScript instance for me
      Hey BTT's shell instance's AppleScript instance! Run this command please
      Hey BTT's shell instance's AppleScript instance! What was the result of that command?
   Hey BTT's shell instance! What was returned by the AppleScript instance?
Hey BTT! What was returned by the shell instance?

...but as BTT can directly execute both shell scripts and AppleScripts you can

Hey BTT! Start a shell instance for me
   Hey BTT's shell instance! Run this command please
   Hey BTT's shell instance! What was the result of that command?
Hey BTT! What was returned by the shell instance?

...or you can

Hey BTT! Start an AppleScript instance for me
   Hey BTT's AppleScript instance! Run this command please
   Hey BTT's AppleScript instance! What was the result of that command?
Hey BTT! What was returned by the shell instance?

Wrapping things in extra layers is fun when playing pass-the-parcel, but isn't necessary here. :wink:

If this one part in a multiline shell script, use the shell form. One part of a multi-command AS, use the AS form. If this is a single, standalone, command then use either (although I think that the shell version is marginally more performant).

2 Likes

OK I understand. Thank you very much.

1 Like