Questions about using osascript to call KM macros

When I call a KM macro from a macOS shell command line, like this:

osascript ~/filename.scpt "parameter1"

where ~/filename.scpt is a file that contains this:

tell application "Keyboard Maestro Engine" to do script "TryThis" with parameter "Whatever"

and where my macro contains this action:

image

The result I get in the shell is this:

T: Do Script V:Whatever B:AppleScript

That makes sense, I guess. So then I tried replacing the file contents with:

tell application "Keyboard Maestro Engine" to do script "TryThis" with parameter "$0"

...(hoping to pass the command line to my macro) but that gave me:

T: Do Script V:$0 B:AppleScript

...even though $0 normally gives you something more useful, like when you do this:

echo $0

... and you at least get this:

-zsh

Ideally what I would like is to pass the standard input in its entirety to the KM macro, but right now I can't even figure out how to send a variable to osascript, let alone standard input. For example, you can probably see what I'm trying to do here, can anyone fix this to make it work?...

var=$(echo "sending some text to an applescript"); osascript ~/filename.scpt $var

And if you can fix the above, can you also fix it so that STDIN is sent to the script? I might be wrong, but from what I can see, if you send STDIN to osascript, it will assume that STDIN is the program, not the data.

I'm not entirely sure what my end goal here is. It's mostly just pure research to understand what is possible. I'm trying to see how much data I can send to a KM macro using the Shortcuts app that calls a KM macro using osascirpt.

That's normal within the shell, because of course $0 is a shell variable. It's not part of Applescript syntax.

I don't have time to also experiment, but it's an interesting subject, and I am keeping a note of the following pages since they seem informative.

osascript how to pass in a variable
How to pass variable in bash script to osascript
Run AppleScript from bash script
How To Pass A Variable To Osascript In Bash

1 Like

As described in the man page for osascript, you can pass parameters like this:

on run argv
	set v to item 1 of argv
	tell application "Keyboard Maestro Engine" to do script "TryThis" with parameter v
end run
2 Likes

and equivalently, of course, you could display a "hello world" text passed as a command line argument to a JavaScript for Automation script file like:

function run(argv) {
    Application("Keyboard Maestro Engine")
        .doScript(
            `<dict>
                <key>Action</key>
                <string>DisplayWindow</string>
                <key>MacroActionType</key>
                <string>InsertText</string>
                <key>Text</key>
                <string>${argv[0]}</string >
            </dict > `
        );
}

with a command line like:

osascript -l JavaScript ~/Desktop/testArg.jxa "Hello world"

to get:


Or to see the full argument vector:

Expand disclosure triangle to view JS source
function run(argv) {
    Application("Keyboard Maestro Engine")
        .doScript(
            `<dict>
                <key>Action</key>
                <string>DisplayWindow</string>
                <key>MacroActionType</key>
                <string>InsertText</string>
                <key>Text</key>
                <string>${JSON.stringify(argv, null, 2)}</string >
            </dict >`
        );
}
2 Likes