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 ?
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 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:
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.