How to Get the Selected Action's Information (Name, Id, Macro) in KM Editor Using AppleScript/JavaScript

Yes, it will be different for each machine since the macro import will generate a unique actionID again to prevent conflict with existing actionID .

By the way, did you try the KM10's Menu feature. It is really cool, I using for tracking task time

image

1 Like

I just wanted to make sure you knew that. In general, you can rely on macro and group UUIDs to be the same, unless a macro or group was duplicated.

By the way, did you try the KM10's Menu feature. It is really cool, I using for tracking task time

Not yet, but I will. :slightly_smiling_face:

You're welcome.

I should remind you, however, that if the action you're looking for is embedded in, for example, an action of one of the following MacroActionTypes: IfThenElse, PauseUntil, Switch (there may be others) then your script will not see it as you have to recursively examine each of those types.

In order to determine that, you need to examine the XML of the action (your actionItem in your script) and see if there's a MacroActionType property defined like this for example:

	<key>MacroActionType</key>
	<string>IfThenElse</string>

That's the only consistently reliable way I've found to do it, but then I am not an AppleScript expert so there may be alternatives.

I do have code to do all this but unfortunately it's protected by an IPA so I can't share it. However, you can see it in action in the Prettify facility implemented as part of my KM debugging aid, Checkpoint, which is available to all for free - just so you know it is possible to do.

tiffle,
yes, that did not actually solve the problem until I have to use recursion. Unfortunately I not familiar with Applescript to do recursion. Dan suggest converting the macro plist into jxa to perform operation as it is easier to use Javascript, so I pending for that solution.

So sorry - I got sidetracked by VSCode's ability to use JSDoc comments. See this topic for something that might be helpful for your issue. Then again it might not, but it's still cool: Get JXA Plist for a Macro UUID, With VSCode "Intellisense" Documentation Examples

1 Like

Dan,
Np, that's great. Thank for the hard work. I take a look at that.
thank

1 Like

The issue is still how to find a specific action. I forget - what will you do with it when you get it? Is it only to verify that it exists in the macro? And if so, you could find it easily by searching the XML for that macro. It's stored like this:

<key>ActionUID</key>
<integer>13025</integer>

And where did you get the action's ID to begin with?

Dan,

I use it to go to selected action. Yes, I just need to check if the ActionID exists in the macro, and from what I see the simple string search will do fine given that your code generates standard json. No need for recursion.

The json return has this string:

{
   "MacroAction": "Unmark",
   "MacroUID": "FE0CA6DC-2359-4FA9-A236-E2D1BBE5160E",
   "ActionColor": "Teal",
   "MacroActionType": "MarkMacro",
   "ActionUID": 18241
}

So I just need to search for "ActionUID": actionId , and that will check for existence of action. Recursion could be slower since action can be nested several deep down.

I modified Tiffle's original code to return the currently selected actionName and actionID

tell application "System Events"
   if visible of application process "Keyboard Maestro" then
      -- display dialog "Visible"
   else
      -- display dialog "Not Visible"
      return 0
   end if
end tell

tell application "Keyboard Maestro"
   try
      set selMacros to selected macros
      if ((count of selMacros) = 1) then
         set selActionList to get selection
         --- Loop Through All Actions ---
         repeat with selAction in selActionList
            if the class of selAction is action then
               set aID to id of selAction
               tell selAction
                  set actionName to its name -- Grammar Police
                  set actionId to its id -- Grammar Police
                  return actionName & "@@##" & actionId
                  -- display dialog actionName & actionId
               end tell
            end if
         end repeat
      end if
   end try
end tell

The original data is xml, and that's what I would search - no need to convert it to json if you're going to do a string match. Just use a regex like /<key>ActionUID<\/key>\s*<integer>(\d*)<\/integer>/

But you could convert it to json if you wanted to, as I show in the other topic

Dan,

I get my macro working now with your code, and your code works fast.

I'm using JSON as I want to reuse your existing sample as much as possible since I need the fastest way to leverage. Your amazing piece of work is the last missing piece I need to finish up my macro ...

Look like another late night for me :}

thanks

Cool - I'm glad it helps. I prefer to work in json also, and in fact, I have my Macro Repository backup all my macros in json.

One thing you might need to know about plists in JXA: They don't store <data> elements, like icons. At least I couldn't figure out a way to get it to work. And that's not an issue, unless you want to convert the plist back to XML, for instance to rewrite a .kmmacros file.

By the way, if anyone knows the proper way to handle <data> elements, let me know.

Personally, I sometimes need the elements, because I edit .kmmacros files. In one case, I strip out Triggers. In another case, I transfer the triggers from existing macros to new macros before I import them.

So I've developed a workaround so I don't lose the <data> elements. It's an integral of my Plist library, so most of the time I forget it's even there. I won't go into the details here unless you want to know. But suffice it to say that I have a workaround that's worked perfectly for 5 or 6 years.

1 Like