the example AppleScript I gave you shows the way, but because it's AppleScript it could be somewhat confusing as to what's going on. I have to say it took a month to "discover" how to do this stuff with KM.
Anyway, I've taken that example and added comments to it so you can better follow what's going on.
I also didn't say that to run this example the following must be true:
- The KM Editor must be open and visible
- One macro must be selected.
- At least one action must be selected.
If these conditions are not met then the script won't actually do anything.
In pseudocode, the script does this:
Exit if the KM Editor is not visible
Exit if zero or two or more macros are selected
Loop through each selected action in the selected macro
Get the action's properties into selAction
Tell selAction to provide its name and id
Display the action's name and id in a dialog
You should note that this example will not properly expose actions embedded within, for example, a KM
IF action - that requires some recursion which is too complex to go through for this example.
I hope this comes closer to answering your question.
Here's the example script for you. You can unzip it and run it directly inside Script Debugger (which is infinitely better than Apple's Script Editor) or embed it in an
Execute AppleScript action. It's better to run it in Script Debugger as then you can step through it and examine the values of everything as you go and that's basically how I discovered how to do this stuff.
Example.scpt.zip (491.8 KB)
Example script to show how to get information about actions
in a macro.
The KM Editor must be open and one macro must be selected for
this to run; otherwise it just exits.
You must also select at least one action as this example
loops over the actions that are selected. So if no action is
selected, nothing will happen.
In this example, the name of each selected action and its ID is
extracted and displayed in a dialog.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "System Events"
-- make sure the KM Editor is visible, if not then exit
if visible of application process "Keyboard Maestro" then
--display dialog "Visible"
--display dialog "Not Visible"
tell application "Keyboard Maestro"
-- Only one macro must be selected
-- If there's more than one then exit
set selMacros to selected macros
if ((count of selMacros) = 1) then
-- get the list of actions in the selected macro
set selActionList to get selection
-- loop through all the actions in the selected macro
repeat with selAction in selActionList
-- selAction contains all the parameters for the current action
-- make sure the current action is actually an action
if the class of selAction is action then
-- aID is the ID of the action
set aID to id of selAction
-- now get the name of the action and its ID and display tem
set actionName to its name
set actionID to its id
display dialog actionName & " " & actionID
-- keep looping over the actions
EDIT: I just noticed I'd left a line of code in
set aID to id of selAction
That's a superfluous line and isn't required in this example. However,
aID ends up containing the same value as
actionID obtained a few lines further on, so by accident you can see two different but equivalent ways of getting the value of a property of an action using AppleScript.