How to tell if an AppleScript is invoked from a KM macro?

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

I seem to remember seeing a topic on this. If memory serves, you can check something about the application that launched it, or something like that.

I can’t think of the right search terms, though, to find the topic. If you don’t find it, I’m sure @ccstone will chime in.

Hey Mitchell,

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()



1 Like

This basic question was addressed here:

The answer is there is no automatic, built-in way.
The only way is to set a KM Variable before the Execute Script Action.