Automatically Sort Macros According to Containing Group

Howdy folks, this post by @82Taylor inspired me to make this macro.

It's designed to automatically change the sort option when switching between macro groups. For instance, I have several smart groups such as "created in the last week", "used in the last two minutes" etc. In those groups, I like to have the macros sorted by "creation date" and "use date" respectively. This macro detects when Keyboard Maestro's window title changes, and sorts the macros according to the group I'm currently in. The IF action ensures it only runs when the group column is clicked on. That way, when you click on a macro inside that group column (and the window's title changes again), it does not trigger and resort the macros again.

End-user modifications would include setting the string to whatever your groups is called for each "contains" switch, and then selecting the appropriate sort option in the Select Menu Item action below.

It's fairly straightforward, but if anything is unclear, or if you have suggestions as to how it can be improved, don't hesitate to reach out here or in a PM.

-Chris

CURRENT VERSION: 1.3.1 (Monday, February 07, 2022)

v1.3.1: Monday, February 07, 2022
Added a condition to only trigger if the front window is the Editor.
This prevents it from triggering if you have the Macro Inspector or other such window open.

Previous release notes (click to expand/collapse)

v1.3.0: Saturday, December 25, 2021
Switched to local variables so as not to clutter up end-user's variables list.

v1.2.0: (unknown date)
I added an AppleScript to determine if the macro's title field is currently focused and if it is to abort the macro. This will prevent the macro from needlessly running when all you're doing is changing a macro's title.
While it still triggers and then runs the AppleScript, it does significantly cut down on lag while editing a title.
As always, I am sure this can be improved on, and if anybody has suggestions of a better way of doing this, please feel free to let me know. :grin:

v1.1.0: (unknown date)
Added an IF action to ensure the macro executes only when selecting a macro group.

v1.0.0: Saturday, November 27, 2021
Initial macro

06)[AS-KM] Macros- sort according to focused group.kmmacros (33 KB)

Macro screenshot (click to expand/collapse)

5 Likes

Hey thanks!

Added your Menu condition and it works perfectly – hadn't thought of a solution for that yet!

Cheers!

1 Like

Thank you! I edited my post to give more credit where more credit is due haha. I don't know if I would have thought of this if it weren't for your post. Glad the IF action worked for you! :grin:

1 Like

Updated to version 1.2, with the inclusion of an AppleScript to prevent the macro from fully executing if you're in a macro's title field.

See post #1.

Howdy folks, version 1.3.1 is out (apparently I forgot to post 1.3.0 but oh well :laughing:)

See post #1.

1 Like

@cdthomer, this is great and opens up some other creative possibilities. Gears are turning...

Thanks for sharing!

1 Like

Chris,
I have tried that macro but I wonder where does this script come from ?

1 Like

Whoops! I use compiled scripts on my personal versions (but have the AppleScript itself in the action)... try switching script file to text script and see if it pops up for you. Either way I'll update the macro in post 1 in a minute. Sorry!

EDIT: @macdevign_mac post 1 is updated with the corrected issue. If switching from script file to text script didn't work for you just download again. Thanks for alerting me to this! :laughing:

Chris,
I see. Thank

1 Like

Hey, Chris.

I thought this may have been discussed before, but I don't see anything in this thread. I recently re-enabled this macro and it appears to be working, but I have lots of log entries with this error:

Action 422123 failed: Execute an AppleScript failed with script error: text-script:1436:1440: execution error: System Events got an error: Can’t get text field 1 of scroll area 3 of splitter group 1 of group 6 of window 1 of application process "Keyboard Maestro" whose subrole = "AXStandardWindow" and title contains "Keyboard Maestro Editor" whose role = "AXTextField" and description = "Macro Name". Invalid index. (-1719)

Are you seeing that? (I'm on Sonoma 14.3.1)

Hey Evan,

I’m not seeing that, but I am still on Ventura. I don’t trust Sonoma due to all the bugs I have heard about it and probably will not upgrade to it at all.

That being said, the error message is claiming that I can’t find the window, which is ridiculous, because the window has to exist for the macro to be triggered (at least the way I built it), and if you say it’s working, then clearly the AppleScript is doing it’s job, and apparently giving a false error message. I don’t believe Peter changed the underlying structure of the editor, to where the macro title text field has a different name according to AppleScript, but I supposed that’s possible.

Here’s an updated cancel macro action set... try replacing yours with this one. I don’t think it will make a difference as the fundamental code is the same, but it has failure notifications turned off, so it will at the least not bug you with annoying notifications if it still doesn’t work.

Actions Screenshot (click to expand/collapse)

Cancel macro actions.kmactions (2.9 KB)