MACRO: Get Recently Run Macros with Last Executed Time

Okay @noisneil, I'm sure this could be improved, but here's something I hacked together:

  1. First I modified @ccstone's macro above to create a subroutine.

    DOWNLOAD Subroutine File:
    sub—KM -- Last N Macros Run v1.00.kmmacros (26 KB)

    Subroutine-image

  2. Then I created this macro to call the subroutine and filter unwanted macros. Note that you'll want to modify the contents of local_Macros to Exclude

    DOWNLOAD Macro File:
    Last N Macros Run (Filtered).kmmacros (32 KB)
    Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

    Macro-image


@noisneil, there's one known limitation. If the macro name includes a close parenthesis the If action that includes matches will not successfully match.

For example, I have a macro named: 01)Toggle Smart Sort. If I put that exact string in local_Macros to Exclude then the filtering fails. (Note: I suspect that this is a Regular Expression issue, but even when I attempted to use the Filter ActionEncode for Regular Expression, the filtering failed. Maybe @ccstone, @drdrang or some other RegEx Ninja could explain.)

The good news is that the macro can be filtered if a substring of the macro name is entered in local_Macros to Exclude (in this example: Toggle Smart Sort).

1 Like

Do you need a regex? I haven't fully followed the logic of the macros, but can you not use "contains" (which is also case-insensitive) instead of "matches"?

Unless it's because you want to include regex pattens in your exclusions list, in which case @peternlewis's post here might help.

Thanks @_jims. Very nicely put together and that filtering works perfectly! None of my macro names start with parentheses but quite a few have them near the end of their titles. All I've had to do is cut the name short and it still finds them. :+1:t3:

The ideal scenario would probably be to do the filtering on the shell script side (not that I'd have a clue how of course), just because there are so many instances of those little macros, that I have to request a silly number of results. Here's what I get from requesting 500 results, which takes 12sec to process:

Screenshot

Nice thought regarding contains but I couldn't get that to work.

I was hoping to use RegEx specifications due to the flexibility. Unfortunately I wasn't thinking straight when I posted the macro earlier.

The problem is easily resolved by escaping the special character. In addition, I added a prefix (⇢ ) and suffix ($) to each macro in local_Macros to Exclude to prevent unintended matches.

Keyboard Maestro Export

Okay @noisneil, now that I look at the approach I used above, I'm embarrassed. I need to get more sleep. :crazy_face:

This is much more efficient:

DOWNLOAD Macro File:
Last N Macros Run (Filtered—Method 2).kmmacros (27 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

1 Like

Sooo much quicker! The only downside is that, now that it's looking for a new line directly after the macro name, I can't shorten the name to exclude the trigger. As some of these macros are triggered when any application activates, the trigger could be anything.

The previous (less efficient) version is more forgiving in that regard.

It should work if you add a .* suffix. For example:

Speakers - nAc Volume Profile \(per App\).*
1 Like

Brilliant. I changed the regex to .*⇢ %Variable%local_Exclude%.*\n and it works nicely. Thanks @_jims!

I tried to automatically escape special characters, but it doesn't seem to do anything. Any idea what's wrong with it? Be handy to know for other applications.

Edit:

This action works:

Escape Regex Special Characters.kmactions (643 Bytes)

@noisneil, here's another spin on this topic.

This macro leverages @ccstone's excellent macro (recast as a subroutine) to generate a list of the most recently executed macros (filtered) presented in a Prompt With List (most recent on top).

When a macro is selected, it will be executed.

If the Shift (⇧) is down when it is selected, the macro will be found using Edit>Find>Find in All Macros….



DOWNLOAD Macro File:
Execute or Find Recent Macro.kmmacros (32 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image


Required subroutine:

DOWNLOAD Subroutine File:
sub—KM -- Last N Macros Run v1.00.kmmacros (26 KB)

Subroutine-image

2 Likes

@_jims, Love the idea of this, but at the moment I'm getting an empty list.

@_jims

The output of this action is blank, and I think it's the point of failure:

Any ideas why the script might not be working on my system?

Hi @noisneil. Sorry for the delayed reply.

Try downloading the main macro again (above). I think I may have mistakenly uploaded an incorrect version. Also, in this updated version I replaced tac with tail -r in the shell script. tac may not be available on your system.

After testing again, if you have any issues, please let me know.

1 Like

Yep, that was it! Really nice. Thanks! :clap:t3::clap:t3:

1 Like

Hi @ccstone. Do you know of there is a similar macro that will also list subroutines (executed by triggered macros)?

Hey Jim,

As far as I can see subroutines are not included in the Keyboard Maestro Engine Log when they are run, so there's not a convenient method.

I suppose you could get the macro list from the log and then:

  • Parse the XML of each macro for subroutines: <string>ExecuteSubroutine</string>
  • Pick up the UUID for same.
  • Then run an AppleScript to discover the last execution time of the subroutine and compare that to the execution time of the macro.

I think it could be done, but it would be a lot of work.

-Chris

1 Like

Perhaps easier for @_jims to add a "Log" action to any subroutines to be tracked, then leverage your original script. I'll confess I haven't looked -- is there a particular format to log so the line gets picked up?

2 Likes

Good idea.

And yes – there's a particular format.

1 Like

@ccstone and @Nige_S, thanks for the suggestions.

@peternlewis, for subroutine executions, please consider adding a Preferences Set by Command Line, maybe to:

defaults write com.stairways.keyboardmaestro.engine

Thanks, all!

Only triggers are shown in the log - executed macros (via Execute a Macro or Execute a Subroutine actions) are not shown in the log file.

If you want to log them, add a Log action to the start of the macro.

3 Likes

Hi Dan,

I'm looking for a way to get the last executed date just for a single macro (maybe by UUID would be the best option). My knowledge of Javascript is close to nothing (I would even say it's nothing, to be honest).

Would it be easy for you to share a customized macro to just do that?
If not, I understand that these things take time and energy, and I will find a workaround.

Thank you!