Detecting whether a script is running in a KM context

What is the best way for a script to detect whether it is running in a KM action ?

JS

.pathTo(this) 

and AS

path to me 

provide more or less enough information for workable approaches (the JS result varies with the more immediate context of the this) but I wonder if I am overlooking something more direct and less fallible ?

I haven’t found anything better than that, which sucks, but that’s life.

I guess one other variant is something like

sa.doShellScript('env');

or

do shell script "env"

to see whether there are any KMVAR_ variables in the environment - but still not all that simple and clear cut.

set currentApp to name of current application
set currentProcess to first paragraph of (do shell script "pgrep -ia" & space & currentApp)
set parentProcess to do shell script "ps -o ppid=" & space & currentProcess
set parentProcessName to do shell script "ps -o comm= -p" & space & parentProcess

Very ugly, but when run from KM it returns

/Applications/Keyboard Maestro.app/Contents/Resources/Keyboard Maestro Engine.app/Contents/MacOS/Keyboard Maestro Engine

2 Likes

Here is a version that gives a bit more info…

set currentApp to name of current application
set currentProcess to first paragraph of (do shell script "pgrep -ia" & space & currentApp)
set parentProcess to do shell script "ps -o ppid=" & space & currentProcess
set parentProcessName to do shell script "ps -o comm= -p" & space & parentProcess
set frontApp to application (path to frontmost application as text)
set separator to "------------------------------------------------"
tell frontApp to display alert "Script Context:" message separator & linefeed & "▶ Current app:" & linefeed & currentApp & linefeed & separator & linefeed & "▶ Parent process:" & linefeed & parentProcessName & linefeed & separator & linefeed & "▶ Myself:" & linefeed & (path to me) & linefeed & separator & linefeed & "▶ Frontmost app:" & linefeed & frontApp & linefeed & separator


…and a nice output. For example, when run from KM:

2 Likes

Thanks !

On the JSA side, for reasons which I’m not sure that I’ve understood yet, this works from Script Editor:

Application.currentApplication().properties().name;

but fails in osascript-run contexts like Keyboard Maestro and the Script plugin for Atom.

(I guess that has some diagnostic value in itself)

I would imagine the easiest way is to set a specific Keyboard Maestro variable or environment variable variable (before running the script, leaving it permanently set) and then detect that.

1 Like

and perhaps, for sharing with users who may not have created a KMContext variable, something like:

AppleScript

set xs to (system attribute) as list
repeat with x in xs
    if length of x > 5 and text 1 thru 6 of x = "KMVAR_" then return true
end repeat
return false

JavaScript for Automation (Sierra onwards)

let a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true, a);

sa.systemAttribute()
    .findIndex(x => x.slice(0, 6) === 'KMVAR_') !== -1;

JavaScript for Automation (Yosemite up to and including Sierra)

var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true, a);

sa.systemAttribute()
    .findIndex(function (x) {
        return x.slice(0, 6) === 'KMVAR_';
    }) !== -1;
1 Like