A Hotkey to Repeat the Last Macro Used?

First we need to know the name of your Last Used Macro.
Let’s see the Keyboard Maestro Engine log.
/Users/admin/Library/Logs/Keyboard Maestro/Engine.log
We analyze the output of the log.
I find these possible cases in the log file:
case 1: Execute macro “

2020-03-25 10:57:55 Execute macro “nH Display Last Used KM Macro v0” from trigger Editor
2020-03-25 10:57:55 Execute macro “nH insert Display Text block v0” from trigger Do Script

Note: This is the most common line in the log. If the macro run correctly, we will see the macro name in the line. We can use “search and replace (with RegEx)” macro to extract from this line.
The RegEx should be : (^.+)(Execute macro “)(.+)(” from)(.+$)

case 2: line(text-script:) + “\r”

2020-03-25 10:58:48 text-script:199:226: execution error: Keyboard Maestro Engine got an error: do script found no macros with a matching name (macros must be enabled, and in macro groups that are enabled and currently active). (-1)
(blank line)

Note: Here’s two lines. One line include “text-script:”. Other line is a blank line. We can’t see macro name in this line.

case 3: line(text-script:) + line( Macro “)

2020-03-25 10:58:48 text-script:199:226: execution error: Keyboard Maestro Engine got an error: do script found no macros with a matching name (macros must be enabled, and in macro groups that are enabled and currently active). (-1)
Macro “nH Display Last Used KM Macro v0” cancelled (while executing Execute AppleScript).

Note: Here’s two lines. One line include “text-script:”. Other line has the macro name.
The RegEx should be : ( Macro “)(.+)(” cancelled)(.+$)

Even though, we cannot get the macro name from case 2, but I found that case 2 and case 3 always appear at the same time. So maybe they are just from one execution. This need to be tested in practical use.

case 4: Action timeout

2020-03-25 11:15:51 Action timeout exceeded. Macro “nH Convert Selected Text to Title Case v0 fromKMD” cancelled (while executing Copy).

Note: we see “Macro “” and “” cancelled” in both Case 3 and case 4. So the RegEx for case 3 is also for case 4:
The RegEx should be : ( Macro “)(.+)(” cancelled)(.+$)

case 5: bypass KMM: switch window macro

2020-03-25 11:00:14 Execute macro “Activate Application Switcher” from trigger The Hot Key ⌘Tab is pressed

Note: If you use KM to switch application window, you need to bypass this macro. It’s annoying and not necessary generally.
Use if then to check if the line matches the RegEx.
The RegEx should be : Activate Application Switcher

case 6: bypass KMM:

2020-03-25 10:42:13 Execute macro “nH Execute Last Used KM Macro v0” from trigger Do Script

Note: If your last used macro is itself—excute last used macr0, what would happen? It become a loop. You run excute last used macro to excute last used macro to excute last used macro…
So we need to bypass this macro. Use if then to check if the line matches the RegEx.
The RegEx should be : nH Execute Last Used KM Macro v0 (NOTE: this is the macro name on my Mac machine, change it to the name you use)

That’s the Problem 1: how to get last used macro name from KM engine log.

Focus on Problem 2: how to get the lines into KM. We can use Tail -n command in “execute a shell script” macro.
Inspired by the KM macro: Display Last KM Macro Error @JMichaelTX
Then use “For each” deal with each line in the lines. But now we have another problem. The original lines in log file is things like:

Line1 earlier earlier
Line2 earlier
Line3 new
Line4 newer
Line5 newest

In “For each” macro we analyze each line from first to last, but the first line is not the newest line. We need to reverse the line order.
We can use “Prepend variable with text” macro in for each line loop.
For the first line Line1, we run prepend (Line 1& linefeed) to a variable. This variable becomes:

Line1

For Line2, we continue with prepending. We get:

Line2
Line1

We continue with prepending. We get:

Line5 newest
Line4 newer
Line3 new
Line2
Line1

After we get the reversed lines, we use second For Each loop to check lines one by one. We break the loop as long as we find a macro’s name.

But we also has Problem 3—how to run KM macro by macro name.
Please see the screenshot in Script Debugger 7.
Screen Shot 2020-03-25 at 12.14.37
We use do script in AppleScript to do this work.
So we got the final KM macro to exctute the last used macro.
As for now, it works OK on my machine(MacOS10.12.6; KM Version 9.0.4). I only use hotkey to execute KM macro. So I cannot see other kind of lines in KME log file. You can add your case and RegEx.
You can test it on your machine. Please let me know if it helps.
nH Execute Last Used KM Macro v0.1_Backup_20200325_1241_v0.kmmacros (23.2 KB)

1 Like