MACRO: Get Selected Macro UUID (and name) (without the clipboard)

Get Selected Macro UUID

Get Selected Macro UUID.v1.0.kmmacros (68.5 KB)

This macro is useful by itself, but it can also be used as a learning tool.

This macro returns the UUID (unique identifier) of the macro that is currently selected in the Keyboard Maestro editor. It does this via AppleScript, using Keyboard Maestro's automation calls.

It does NOT use the clipboard, as so many other macros do (including many of my own - at least, until now).

The included demo macro shows how to call this macro, and it also shows how to get the Name of the macro from its UUID, when the UUID is stored in a variable.

The macro:

The demo:

As with all my macros, feel free to ruthlessly steal from these as needed. After all, I stole - um, I mean - I learned most of this from others!

8 Likes

@DanThomas, I was searching for this and found it. It's very helpful. Thank you very much.
Just one question: In the AppleScript, you have an IF statement:

	if (count of macroUUIDs) is 0 then
		return ""
	end if

and say in the comment:

● If no macro is selected, this returns an empty variable.
● If one macro is selected, its UUID is returned.
● If more than one macro is selected, the first UUID is returned.

My question is: is it necessary?
Presumably, we put this macro at work only when Keyboard Maestro Editor is in the front. When we are working with the Keyboard Maestro Editor, it seems to me, we will always have a macro/macro group selected. Is there a case when a macro/macro group is not selected?

I tested with a Macro Group without any macro in it, it still returns the UUID of the macro group.

So, I guess one of the result is: if no macro is selected, the selected Macro Group UUID will be returned.

Thanks!

It's quite possible you're correct, and I wouldn't blame you for removing it.

But I'll give you a word of advice from my long career as a professional developer: It's ALWAYS a good idea to check for boundary conditions. Probably the single most common programming mistake is not checking for things like this.

LOL, I just finished typing a novel-length explanation of whether it would ever matter in this situation or not, but who cares? If you're using this code, it's not very likely to be mission critical since it's dealing with the KM Editor, so like I said, who cares? :stuck_out_tongue:

Absolutely!

Most likely 99.9% of the time it is not needed. But then there's the 0.1% :wink:

IAC, that simple IF test is extremely fast to execute, and there is no detriment to leaving it in the script.

That makes sense.
Thanks!

Hi, used scripts works perfect to get UUId
want to copy result UUid to clipboard
tried before & after end tell in the following script

set the clipboard to macroUUids

also

set tmp to macroUUids as text
set the clipboard to tmp

None works, plz suggest

tell application "Keyboard Maestro"
	set macroUUIds to selectedMacros
	if (count of macroUUIds) is 0 then
		return ""
	end if
	return item 1 of macroUUIds
end tell

This works for me running Keyboard Maestro 9.1 on macOS 10.14.6 (Mojave):

tell application "Keyboard Maestro"
  set {macroUUID} to selectedMacros
  try
    set macroClass to class of macro id macroUUID
  on error
    set macroUUID to "[ERROR] No Macro is Selected"
  end try
end tell

set the clipboard to macroUUID
return macroUUID

2 Likes

Works great, Thank you