How to create a shortcut that Edits the Last Used Macro?

Hi - I'd like to create a shortcut that when used opens Keyboard Maestro to the last used macro. I've tried adapting the tips here with no luck:

I know in Applescript I can use:

tell application "Keyboard Maestro"
	editMacro "<insert id here>"
end tell

The part I'm stuck on is how to retrieve the ID of the last used macro. Any help is much appreciated, thanks!

As I described in the other thread, this will do the trick:

image

Edit Last Edited Macro.kmmacros (2.2 KB)

1 Like

Thanks for sharing!

That brings up the last Edited macro, but I'm looking for the last Triggered macro (which isn't necessarily the last one edited).

Any thoughts on that? Thx

I have one or two thoughts. First, the editor itself doesn't know when the Engine runs a macro. The Engine would have to have a record of each macro run. Fortunately, there is at least one such record, and that is the Engine log file. I think it would be possible, perhaps even easy, for a macro to look for the "last triggered macro." There are two problems with that idea. First, macro names are not "unique" which means that if you had two macros of the same name, there's no way to know which one was the one that ran. Second, any macro that wants to find the "last triggered macro" would actually have to look for the "second last triggered macro" because the macro doing the looking itself is a triggered macro! Perhaps now you can see why this is tricky.

However there's probably an easy solution. You could manually insert a "record this macro" statement at the beginning of all your macros (except for the macro which runs the last triggered macro.) I think this would get you what you want. You could solve the "unique" problem by saving the UUID of the macro instead of its name. It should be quite simple, inserting a statement like this at the top of each of your macros that you want to track:

If you were willing to do this, I think it would be very simple to create a macro that opened the "last triggered macro."

A small change to the macro provided by DanThomas should work. Instead of going to the Menu Item titled Last Edited, going to the Menu Item called Last Used will open (of course) the last used macro.

1 Like

That's great information! But I suppose that means the Editor has access to some Engine data source besides the Engine log file, because the Engine log file doesn't provide a UUID for the last macro. Any idea what that might be?

Sounds accurate, but awareness the Engine Log file exists is basically the extent of my knowledge about it.

That is correct.

Well, it could ask the engine directly, of course. But you could also find this out in the "Keyboard Maestro Macro Stats.plist" file in the Application Support folder. That's what I use here, although I haven't released this yet:

 
The "Last Used" column is actually "days since last used", but internally I store the actual last used date/time.

2 Likes

Some subtleties here, perhaps:

  1. The last triggered macro is presumably always this one, so we may often be looking for the penultimate trigger event
  2. The entry to the Keyboard Maestro Macro Stats.plist file is presumably written when a macro concludes (rather than starts), so if two are running we may need to distinguish between most recently started and the most recently finished. (which may be the same).

For a macro which lists the N most recent trigger events recorded in Keyboard Maestro Macro Stats.plist, with the names and UUIDs of the triggered macros, see:

1 Like

A draft:

Edit MRU KM Macro (draft).kmmacros (10 KB)

Thanks for guidance @ComplexPoint!

Running the Macro you shared resulted in this error:

Execute a JavaScript For Automation failed with script error: text-script:0:15: script error: Error on line 1: SyntaxError: Return statements are only valid inside functions. (-2700)

Any thoughts on how to fix? Thanks

In the small chevron to the left of of the text box of the Execute JavaScript action, you may need to reset the value back to Modern Syntax

Perhaps that setting has accidentally become unchecked ?

Screenshot 2024-08-28 at 5.26.16 PM


Unless you are using a pre-11 version of Keyboard Maestro ?

On a related note, I cobbled together a macro that presents a list of the ten most recently executed macros and, if one is selected, Keyboard Maestro opens and the macro is selected for editing.

The list is presented using a slightly modified version of @DanThomas's macro: Get Recently Run Macros with Last Executed Time

Download: Edit a Recently Executed Macro.kmmacros (23 KB)

Macro-Image


Macro-Notes
  • 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 14.5 (23F79)
  • Keyboard Maestro v11.0.3

EDIT: Now that I see @ComplexPoint's N most recently run macros (Name, UUID, Stats), I could create a more robust version if the Prompt With List entries were in the following form:

MacroUUID__MacroName**

**To be bulletproof, my improved version should be able to accommodate macro names that include consecutive underscores.


2024-09-04 EDIT: I've shared an improved version of the macro: Edit a Recently Executed Macro

1 Like

Ohh - yea I'm on Version 10.2 :slightly_frowning_face:

Thanks though for the tip…I'll keep poking around to see if I can get it to work

Ohh - yea I'm on Version 10.2 :slightly_frowning_face:

The first thing to try then is just to remove the opening return keyword from the code in the Execute JavaScript for Automation action.


i.e. leaving, in the first line, just:

(() => {

( Ver 11 is a good upgrade )

1 Like

Thanks for your patience on this!

I did the suggested change and the new error is:

Execute a JavaScript For Automation failed with script error: text-script: execution error: Error: ReferenceError: Can't find variable: kmvar (-2700)

Thanks! This macro is a great workaround in the meantime…

Would ideally love to skip the prompt and just open the last triggered macro. I'm fiddling with this now, if I'm able to get it to work I'll post the macro here so future folks can find it, thanks again!

Hi, @adamg. I've shared a macro that does just that: Edit a Recently Executed Macro

1 Like

This is amazing - thanks so much @_jims !!

On my machine (I'm also on Keyboard Maestro v10.2) I needed to add a pause at the beginning…otherwise this macro is seamless!

Also thanks to everyone who helped contribute to this thread!

Sidebar: This is hands-down the best forum on internet! :rocket:

1 Like