MACRO: Edit a Recently Executed Macro, v2.0

PURPOSE

This macro opens the most recently executed macro in the Keyboard Maestro Editor.

Alternatively, if triggered using the secondary hot key, the one that includes Shift (⇧), the 14** most recently executed macros are listed in a Prompt With List.

When the Prompt With List is displayed, the selected macro is opened in the Keyboard Maestro editor. Alternatively, if Shift (⇧) is held down when the macro is selected, the macro is run.

**The default value is 14 because this number of macros vertically fills the Prompt With List menu. The default value can be optionally changed using the local_SecondaryTrigger_NoEntries variable. If the value starts with the plus sign, e.g., +14, the setting, RegexPatterns_MacrosToExclude is ignored.

As noted above, the Prompt With List might exclude macros matching RegexPatterns_MacrosToExclude. In this case, if ⌥<return> is pressed when the Prompt With List is displayed, the Prompt With List reappears with the macros included. Each time ⌥<return> is pressed, the exclusion/inclusion state is toggled.


EXECUTING THIS MACRO FROM ANOTHER MACRO AND/OR BY USING APPLESCRIPT

If this macro is run from another macro using the Execute a Macro action (or via AppleScript), and With Parameter (WP) is specified, the macro runs as follows:

WP Behavior
1 the most recently executed macro is opened
null the local_SecondaryTrigger_NoEntries number of macros are included in the Prompt With List**
n the integer number of macros are included in the Prompt With List**
+n the integer number of macros are included in the Prompt With List ignoring the setting local_RegexPatterns_MacrosToExclude**

**The selected macro can be edited or run.


SETTINGS

local_SecondaryTrigger_NoEntries : refer to the PURPOSE section above.

local_RegexPatterns_MacrosToExclude : can be optionally configured to exclude macros. The value can be one or more text lines, each a regular expression pattern. For example:

^_
[Uu]ntitled

macros that begin with underscore (_) or include Untitled or untitled are excluded. Note that

^%ExecutingThisMacro%$
^%ExecutingMacro%$

are automatically excluded.


ACKNOWLEDGEMENTS

This macro uses JXA shared by @ComplexPoint (N most recently run macros (Name, UUID, Stats)) that extracts data from Keyboard Maestro Macro Stats.plist. Minor modifications were made to provide compatibility with Keyboard Maestro v10+.


TESTED WITH

• Keyboard Maestro 11.0.3
• Sonoma 14.6.1 (23G93)/MacBookPro18,2
• Sonoma 14.6.1 (23G93)/VariableMacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445


VERSION HISTORY

( expand / collapse )

1.0 - initial version

2.0
a) If started from the Keyboard Maestro editor, the setting local_SecondaryTrigger_NoEntries is used.

b) local_SecondaryTrigger_NoEntries can include an integer or a plus sign followed by integer. If the plus sign is included, the setting local_RegexPatterns_MacrosToExclude is ignored.

c) When the Prompt With List appears, depending on local_SecondaryTrigger_NoEntries or the %TriggerValue%, it might exclude macros matching local_RegexPatterns_MacrosToExclude. In this case, if ⌥<return> is pressed when the Prompt With List is displayed, the Prompt With List reappears with the macros included. Each time ⌥<return> is pressed, the exclusion/inclusion state is toggled.

d) Bug fix: If the recently run list included a macro that had been deleted, the macro generated an error. These entries are now properly ignored.



EXAMPLE PROMPT WITH LIST

When using the secondary trigger (or called with a With Parameter), a Prompt With List similar to the following will be displayed.

Depending on local_SecondaryTrigger_NoEntries or the %TriggerValue%, this list might exclude macros matching local_RegexPatterns_MacrosToExclude. In this case, if ⌥<return> is pressed, the Prompt With List reappears with the macros included. Each time ⌥<return> is pressed, the exclusion/inclusion state is toggled.


Edit a Recently Executed Macro Macros.kmmacros (64.3 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 15.0 (24A335)
  • Keyboard Maestro v11.0.3

5 Likes

Hello Jim :wave:

First of all - nice Macro… but I got an error from either the JXA Action that contains the code from Rob (Group: N most recently run Macros…) or either the other one (Group: Generate the PWL Entries).

Details below.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MACRO ERROR OCCURRED  on  2024-09-05 07:31:50
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This info was pulled from the KM Engine Log for the
Last Macro error that occurred.

MACRO:
------------------------------------------------------------------------------------------------------------
Edit a Recently Executed Macro
------------------------------------------------------------------------------------------------------------

Action Type:	Execute a JavaScript For Automation
Action Name:	Execute JavaScript For Automation

ACTION ERROR:	 script error: text-script: execution error: Error: Error: Objekt kann nicht gefunden werden. (-1728). 

KM Engine Log:	
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Execute a JavaScript For Automation failed with script error: text-script: execution error: Error: Error: Objekt kann nicht gefunden werden. (-1728). Macro “Edit a Recently Executed Macro” cancelled (while executing Execute JavaScript For Automation
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

Log File:  ~/Library/Logs/Keyboard Maestro/Engine.log

I am sorry that I only can provide this kind of information to you … Haven’t had the time to update Engine Log Tool yet.

Hoping the information is helpful anyway…

Edit: such a case is the reason why my Scropt Actions are always named … so that I am able to identify them very easy if a script goes wrong. Please consider doing this in the future. It saves a lot of time when multiple scripts are used.

Greetings from Germany :de:

Tobias

Stunning… Thank you

Could I show the triggers?

The reason is sometimes something stops working, I got to the recently executed macros to see if I've accidentally assigned a hotkey to something

1 Like

Hi, @Nr.5-need_input. I'm sorry that you have encountered a problem with this macro.

I suspect that you are not using Keyboard Maestro v11+. If that's the case, my ability to help is limited as I've upgraded my three macs.

As I stated elsewhere, I'm generally not targeting Keyboard Maestro pre-v11, but this macro was an exception because I had seen @adamg's request for a macro that would be v10.2 compatible.

As I was creating Edit a Recently Executed Macro, @adamg and I exchanged PM's. An earlier draft of this macro was working for @adamg's v10.2, but since that draft, I did make a few changes. My assumption was that none of the changes would be incompatible with v10.2.

If @adamg or some other v10.2 user downloads, tests, and reports back, we'll at least know if this is related to the Keyboard Maestro version or something else unique to your system.


In the meantime, @Nr.5-need_input, to troubleshoot, I suggest you insert a Display Text action after each JXA action and display local_MreJson and local_MrePwlList, respectively.

2024-09-14 Edit: @Nr.5-need_input, please refer to the OP of Edit a Recently Executed Macro. It has been upgraded to Version 2.0 which I suspect will address the error you mentioned above. Thanks for your feedback.

Hi, @olifrost. I'm glad you like the macro. Thanks for the feedback.

Theoretically, yes, but not without much more complexity that I don't plan to add to this macro. Here's why: Edit a Recently Executed Macro uses data from the Keyboard Maestro Macro Stats.plist file, thanks to the JXA code shared by @ComplexPoint. However, the plist does not contain trigger information.

When you suspect a mistaken trigger, you have a few troubleshooting options:

  • Create a Smart Group and and specify something like this: used:2m. (On a related note, you might find this macro useful: Search All Macros)

  • Search the Engine.log (Help > Open Logs Folder). For that task, you might find this macro useful: Engine.log Tool

Thanks! I do have a smart group like that, just looking to make a macro to get there. I’ll try your other two methods.

1 Like

I've updated the OP with Version 2.0 of Edit a Recently Executed Macro:

  • If started from the Keyboard Maestro editor, the setting local_SecondaryTrigger_NoEntries is used.

  • local_SecondaryTrigger_NoEntries can include an integer or a plus sign followed by integer. If the plus sign is included, the setting local_RegexPatterns_MacrosToExclude is ignored.

  • When the Prompt With List appears, depending on local_SecondaryTrigger_NoEntries or the %TriggerValue%, it might exclude macros matching local_RegexPatterns_MacrosToExclude. In this case, if ⌥<return> is pressed when the Prompt With List is displayed, the Prompt With List reappears with the macros included. Each time ⌥<return> is pressed, the exclusion/inclusion state is toggled.

  • Bug fix: If the recently run list included a macro that had been deleted, the macro generated an error. These entries are now properly ignored.