Any way to log action ID that called a subroutine?

I have a subroutine that I call at least 50+ times in the MacroBackerUpper macro. If something goes wrong at that point, it's one call in the subroutine (the one that tries to access the database) that kills the macro, and gets reported in the log. Unfortunately, it's useless information, as I have no idea which action called the subroutine, as that's where the real problem lies.

I'd like to add a Log action to the subroutine (or to the macro that calls the subroutine, or some parameter I could pass and log, etc.) that records some information about how the subroutine was activated.

Unfortunately, the %ExecutingMacro% token won't help, because the subroutine is often called multiple times from the same macro. What I really need is the ActionID of the action that has the subroutine call, as that will put me in the spot that generated the error.

Is this possible in some way? I would assume so, but looking through the tokens table, I don't see anything usable.

Edit: The 'hacky' solution, of course, is to add a ton of 'log' calls, one before each call to the subroutine, with some identifying info. But that's a real PitA; I'd like to be able to put something in the subroutine itself that does the work, so I can only do it once.

I could also do this by adding a parameter to the subroutine call, then logging that parameter, but that also strikes me as inelegant. But it's probably the route I'll take if there's no other method.


I can't see a reasonable way to get the Action ID at run time so what I would probably do is this:

  1. Get the Action ID of the calling action manually while in the KM Editor.
  2. Paste that value as a parameter to the subroutine (as you have alluded to)

Getting the Action ID is pretty easy. Here's a little AppleScript that does it:

tell application "Keyboard Maestro"
	--» Get first selected macro.
	set firstSelectedMacro to first item of (get selected macros)
	--» Get selected item.
	set selectionList to selection
	set aid to get id of first item of selectionList
	return aid
end tell

Here's a macro that does it for you. Just select an action in the editor, trigger the macro and a window will pop up showing the selected action's ID. It would be more useful to put the ID into the clipboard, ready for pasting as a parameter into your subroutine call.

Download Macro(s): Actions - Get Action ID.kmmacros (2.5 KB)


Keyboard Maestro Export

  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.6.1
  • Keyboard Maestro v11.0.1

Thanks for that—I had a similar macro that I used to jump to the Action ID as seen in the log file. However, if I have to use a parameter anyway, I'll probably just use text, which will require less interpretation.

I'll set a debug flag in the subroutine, so it won't spam the log unless debug mode is enabled.

Thanks for the script, though—handy enough for other purposes I'll keep it around!

Edit: Yea, this will work:

2023-11-23 08:16:49 Log: MBU SQL processor task: Extract one example same-named macro within a group
2023-11-23 08:16:49 Log: MBU SQL processor task: Extract one example same-named group
2023-11-23 08:16:49 Log: MBU SQL processor task: Check for leading-dot-named macros and groups

(This isn't for my debugging, but rather when a user reports a problem, so I have some idea which SQL bit is messed up for them.)


1 Like