What can I add to an AppleScript to detect whether or not it was invoked from KM? In fact, is there anything a script or a handler can do to detect how it was called?
An indirect way of doing this is:
Wrap a handler around the entire script
Give the handler a parameter caller
In an Execute an AppleScript action, have a text macro that invokes the handler with a parameter like “Keyboard Maestro”
Add a statement to the script outside the handler that invokes the handler with none or me or whatever
Basically no – AppleScript has no tracing faculties.
However – there is a hack if you're running a text-script.
path to me
Will return the path to the temp-file Keyboard Maestro used to run it – something similar to this:
alias DiskName:private:var:folders:z0:3_8bxajpflzdclf77xz5rxth0000gn:T:Keyboard-Maestro-Script-1DA009FF-2154-4823-9A07-2E371C9B1F98
If you need a stack-trace you'll have to build into your scripts/handlers yourself.
--------------------------------------------------------------------------------
script test
property scriptName : "test"
end script
test's scriptName
--------------------------------------------------------------------------------
on scriptName()
local scriptName
set scriptName to "My Test Script"
return scriptName
end scriptName
my scriptName()
--------------------------------------------------------------------------------