Call KBM Macro from the Shell with `osascript`?

I'd seen this question before and frankly, I didn't quite understand the answer, but I went ahead and fixed it myself in a pretty simple way. I just used "osascript -e 'script-UUID' " and it worked fine.

Suddenly later that session, I ran it from the command line and got
syntax error: Expected end of line but found unknown token. (-2741)

Then I used osascript -e "tell application 'Keyboard Maestro Engine'\n do script '34007B18-D3C6-4 2F8-BEC6-9FBFCB11EF7B' \nend tell"
~

Now, given that someone supposedly answered this issue before and got something working, could you please give me some example of how I would call a KBM macro from BASH ????

THANKS!!

Hey @gmark,

I expect the easiest way is to use the Keyboard Maestro URL-scheme.

#!/usr/bin/env bash
open kmtrigger://macro=AFB1EE80-3A76-4E58-A0E8-4C742CBC5C5F

The last parameter is the UUID of the macro.

If/when you run AppleScript from Bash I recommend making it readable. One-liners may seem streamlined, but when you have to maintain them they can be very hard to read.

#!/usr/bin/env bash

read -r -d '' asCmdStr <<'EOF'
   tell application "Keyboard Maestro Engine"
      do script "AFB1EE80-3A76-4E58-A0E8-4C742CBC5C5F"
   end tell
EOF

osascript -e "$asCmdStr"

-Chris

Absolutely.

I'm not sure where the cult of the one-liner started,

( misinterpretation of phrases like it's just a one liner ? )

but it's never kind to give people the impression that
some kind of slightly mysterious credit attaches to
expressing things in a single line ...

(If Lincoln had done that to the Gettysburg address we would never have heard of it, and even if he had somehow managed not to lose or tangle the very long piece of string or sellotape on which it was written, he would have run out of breath before getting half way.)

1 Like

I found using APL back at IBM that time was wasted in interpreters reading each line from the file before execution, and making a finished program into a one-liner made it faster. I'm not sure how important that is these days with faster drives, memory and machines and with BASH.

1 Like

I just think less computation means less electricity consumption. Therefore, I try to reduce the amount of computation if necessary, including using shorter variable names. But I don't know whether or how it has any effects at all.

For KM, I'm also debating whether or how much comments to put in a macro, because it increases the size of the macro file. :joy:

Not significantly.

You probably burn more power with the time spent streamlining than you save in the long run.

-Chris

Oh, really?!
I have no clue. I need to educate myself on computing and energy consumption. :sweat_smile: