A Hotkey to Repeat the Last Macro Used?

thx @Yu_Cai

I will look into it in depth tomorrow and let you know if it worked

z

It is probably not something I would implement simply because of the risk of executing the wrong macro - macros can be triggered by many things, such as timers or remote events or whatever, and so even though you consider the last macro to have executed as being the one you just triggered from the palette, technically the last macro triggered might be a timer macro that starts a background backup. And then re-triggering that macro might cause problems.

In this respect, a technique like the one @JMichaelTX described, where it is explicitly one of a list of macros to choose from would ne a lot safer, even if it was somewhere more tedious. You might also want to record the time the macro was triggered, and then you could double check that the macro had been triggered recently.

Thx all

@peternlewis explanation makes sense of the potential issues of running potentially harmful macros so I understand why this isn't a good idea to implement

@JMichaelTX: as @peternlewis suggested I tried to implement you method on macros that are safe and could be included in the repeat last macro key, yet I cant get it to run.

I suspect the issue is my misunderstanding of the set a KM global variable (say "DND__Last_Run_Macro_UUID") part.

this is the two example macros I came up with:

and for the repeat macro

any clue what I messed up :slight_smile: ?

thx again

stay safe guys

Z

hmmm updating that the actual write to variable seems to be working!
I can see the global variable changing after the successful macro launch

the problem then seems to be in the relaunch last macro part

I get this error:

I think this is the relevant log parts


2020-03-26 11:57:22 text-script:103:122: 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 “rerun macro” cancelled (while executing Execute AppleScript).
2020-03-26 11:57:28 Execute macro “45)scrrenshot-clip” from trigger The Hot Key 4 is pressed
2020-03-26 11:59:33 Execute macro “01) maximize all in MBP mode” from trigger Any application launches

any clue?

This is likely exactly one of the issues I pointed out: The original macro is NOT active when you trigger the ReRun macro:

2020-03-26 11:57:22 text-script:103:122: 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)

I ran a test with that exact AppleScript and it worked fine for me.

1 Like

thx @JMichaelTX! Sorry that im slow with this but nit sure I understand that, you mean that in the KM editor the macro I just launched via the normal hotkey isn't selected? Not sure im following that? I tried both with it being selected and not (again not sure that’s what you mean by active) and still didn't work.
Perhaps you can upload your two macros that worked for you so I can try it here locally?

thx again

Z

This nothing to do with the KM Editor app (Keyboard Maestro.app).
To understand what being "Active" means, see Macro Activation .

Let's start by keeping it simple.
Create a Macro Group with no restrictions (it is "global", available in all apps).
Put both macros in that MG.
Also, make sure the Action to set the Macro UUID is the FIRST Action:
image

Here are my two test macros:

Macro Set -- Main Macro and ReRun Macro

Macro Set -- Main Macro and ReRun Macro.kmmacros (5.0 KB)

Main Macro

image

ReRun Macro

image

2 Likes

Hi again and sorry for the late response (world is to crazy these days..:))

im sorry for being slow but I cant cant get it to work or understand this :slight_smile: I downloaded the 2 test macros you posted. I ran the first test macro and got confirmation it worked

then I binded a key to the second macro (the rerun last macro) and try to run it and I get an error. Here is the last few log entries

020-04-05 12:07:58 Execute macro “01) maximize all in MBP mode” from trigger Application “Finder” activates
2020-04-05 12:08:05 Assertion Failed: (flags & kPasteboardModified) == 0,  file: …/Source/XCAF/XClipboardHistory.mm:839, value: 0
2020-04-05 12:08:38 Execute macro “01) maximize all in MBP mode” from trigger Application “Finder” activates
2020-04-05 12:09:30 Execute macro “01) maximize all in MBP mode” from trigger Application “Finder” activates
2020-04-05 12:09:48 Running application query took a while (5014 us)
2020-04-05 12:09:50 Execute macro “ReRun Last Macro [Example]” from trigger The Hot Key ⌃⌥⇧⌘R is pressed
2020-04-05 12:09:50 text-script:102:146: execution error: Keyboard Maestro Engine got an error: a script must be specified (-1)

2020-04-05 12:09:50 text-script:102:146: execution error: Keyboard Maestro Engine got an error: a script must be specified (-1)
 Macro “ReRun Last Macro [Example]” cancelled (while executing Execute AppleScript).
2020-04-05 12:10:21 Execute macro “ReRun Last Macro [Example]” from trigger The Hot Key ⌃⌥⇧⌘R is pressed
2020-04-05 12:10:21 text-script:102:146: execution error: Keyboard Maestro Engine got an error: a script must be specified (-1)

2020-04-05 12:10:21 text-script:102:146: execution error: Keyboard Maestro Engine got an error: a script must be specified (-1)
 Macro “ReRun Last Macro [Example]” cancelled (while executing Execute AppleScript).
2020-04-05 12:10:29 Assertion Failed: (flags & kPasteboardModified) == 0,  file: …/Source/XCAF/XClipboardHistory.mm:839, value: 0

any clue what im doing wrong?

If its to much I can just give up as its not super critical for my workflow just a nice addition so please feel free to ignore if you don't have time.. :slight_smile:

thx again

Z

Check that the DND__Last_Run_Macro_UUID variable is in the Keyboard Maestro Preferences.

You will get “a script must be specified” if you run the AppleScript command:

tell app "Keyboard Maestro Engine"
  do script ""
end tell

So my guess is the variable is empty.

From your log file, it appears that you did NOT actually run (trigger) the main macro.
In fact, from your Notification image:

it appears that you clicked on the "Try" button at the bottom of the KM Editor.
When you do that, it will NOT set the "DND__Last_Run_Macro_UUID" variable, as shown in this image from the KM Editor:
image

So you need to FIRST assign a trigger to the Main Macro, and use that trigger to run it.

ha! You were right @JMichaelTX!

I wasn’t aware that using the try option differs from the use of a bind
it now works!

the reason it didn’t work before is that I launched the macro via a pallette

so I assume it dosent work via a pallet niow is the same reason it dosent work with the try. I wonder why its that?

thx so much

Z

Hi Peter,
I understand the risk you are saying. But I'm wondering if you could store the last executed UUID somewhere either in a variable such as the ENV variable, or we can get the UUID by using AppleScript. I think it is a good thing to allow users to use it with the knowledge about the potential risk of triggering the wrong macro. I was think about using a work around by searching the KM engine log. But I'm not sure if there is a reliable way to get it. Assuming the macro was successfully triggered, is the macro name of the last triggered macro is always in the last line and follow this pattern?

2021-09-19 23:44:10 Execute macro “Macro Name” from trigger The Hot Key ⌘Tab is pressed

If so, I can get the macro name from the quotes.

Thanks!

The log is not a structured resource, and is subject to change at any time.

That said, a safe way to do what is asked for this question would be a macro that looked something like this:

  • Grab the last line of the Engine.log file
  • Switch on the line
    • If it contains "name of a macro 1"
      • Execute Macro "Name of a macro 1"
    • If it contains "name of a macro 2"
      • Execute Macro "Name of a macro 2"
    • Otherwise
      • Notify "No Acceptable Macro Found"

That way it will never execute an unexpected macro (like a timed "Backup my Mac" macro), and it fails in a safe way.

The UUID of the last triggered macro is not something that makes sense in the context of Keyboard Maestro triggering macros continuously in a variety of ways.

Fortunately, @DanThomas already wrote a macro some years ago that can show recently executed macros more easily and efficiently than parsing the log:

I also wrote a macro a few years ago myself back when KM 8 was still new that piggybacks off Dan's work to show a further simplified list prompt of recently executed macros and lets you jump back to the editor to quickly modify whichever one you select. I still use it to this day:

You could easily modify it to run a macro chosen from the list instead of editing it, or to choose between them by, say, holding down a modifier key when making a selection.

Thank you so much for reminding me about that macro. I need something like it right now, and I forgot all about it. :smile:

1 Like

Thank you very much, @gglick. I don't know how I missed that.

@ccstone also created macros to get the last N's executed macro from the Engine log, exactly what I was thinking.

Hi, Peter, this is great idea, used in combination with the macros @DanThomas, @gglick, and @ccstone have made.
I may add some special characters in the macro name for multiple macros. I can do something like:

  • if it contains (or maybe ends with) "special characters" (I'll probably do a RegEx search, match from the end of the macro list)
    • get the complete macro name and execute the macro.

Then I can freely rename those macros without worrying about the need to edit this macro as long as I keep those special characters untouched.

This has great usage for testing macros. Normally, I would assign a hotkey for test and then delete it after the test. But now, I only need to include the special characters in the test macros and run it once to add it to the last executed macro list. So, no need to assign a hotkey and then remove it.

1 Like

I've made a macro to execute the last run macro whose name ends with ==TM== (==TM== may be changed to anything else as long as we replace ==TM== with the target string in the macro as well.

Using this macro, we only need to add the target string (in this case, ==TM==) to the target macro name. We don't need to add the target UUID action (see below) to the macro.

image

If you haven't seen this, check it out: MACRO: Run Current Macro.

Obviously this has to be run from the Editor, but I use it constantly.

1 Like

Thanks, @DanThomas.

I searched but did not find it. I was searching for last executed macro. I think that's why I did not find it.

I just downloaded it and tested it. It's great! Your macro is definitely very useful for debugging macros, since we probably keep the Editor open for this kind of job. I'll like it very much and I'll start using it right away!

1 Like

This simple action lists the most recently used macros:
image
Almost always the first line returned in the list of macros is the one I want. Since it is pre-selected, hitting return runs it.
I have a macro assigned to hotkey F5 with only this action. So, F5 + Return does the job almost always. Can also modify "55m" on the fly if a lot of time has elapsed since last use.

P.S. My attempts to filter the log file for the most recent macro worked most of the time but when it didn't, it was really annoying.

3 Likes