MACRO: Toggle Smart Sort, v2.0

PURPOSE

A macro to intelligently sort macro groups was created and shared by @cdthomer:
Automatically Sort Macros According to Containing Group. @cdthomer was inspired by @82Taylor's post: Request: Ability to Sort Different KM Folders Differently.

This macro group, __ Smart Sort ⌃⌘/, adds a few bells and whistles to that macro:

  • When a Smart Group is selected and the macro is in Smart Sort mode, the macros will be sorted intelligently. For example, if a Smart Group named Modified Today is selected, the macros will be sorted by Date Modified. This is the main function of this macro group and it preforms very similarly to @cdthomer’s macro mentioned above.

  • The primary macro for user interaction is Toggle Smart Sort. Smart Sort can be enabled/disabled using ⌃⌘/ or ⌃⌥⌘/.

  • The default configuration for Smart Sort is likely suitable from most users, however, it can be adjusted using the SETTINGS below.

  • When Smart Sort is disabled using ⌃⌘/, a palette that facilitates manual sorting will appear. If it is subsequently dismissed, it can be displayed again by holding the (Option key) when clicking a group.

  • When Smart Sort is enabled using ⌃⌘/, the sorting palette will be hidden.

  • When toggling Smart Sort using ⌃⌥⌘/, the sorting palette behavior is reversed, i.e., it appears when Smart Sort is enabled, otherwise it is hidden.

  • When Smart Sort is enabled, the intelligent sorting can be temporarily suspended by holding the (Option key) when clicking a Smart Group. The sorting palette will also appear.

  • The group is named __ Smart Sort ⌃⌘/ so that it appears near the top of the group list below the Smart Groups.

SETTINGS

The intelligent sorting in Smart Sort mode can be adjusted by changing the default values for five variables. Note that these settings are lowercase Regex strings that are used in a Switch/Case action with a matches condition.

local_SortByCreated
create

local_SortByModified
modif[iy]|date

local_SortByUsed
use|execute|run

local_SortBySize
size|large|big

local_SortByTrigger
trigger|hot\s?key

The Sort Palette initial position can be adjusted. This is the position of the top-left corner of the palette relative to the Keyboard Maestro Editor window (both in percentages).

local_Palette X : 0 = far left; 100 = far right; 40 default

local_Palette Y : 0 = top; 100 = bottom; 10 default

IMPLEMENTATION NOTE

All persistent values are saved to a single KM Dictionary named jsTSS_dict, where js are my initials and TSS refers to the name of this macro. For more information about this approach to saving values, refer to Dialog with Dynamic Popup List.

ACKNOWLEDGEMENTS

  1. Toggle Smart Sort is largely based off Automatically Sort Macros According to Containing Group. The author, @cdthomer, kindly tested and provided feedback for this macro.

  2. The JXA script used to position the group palette was adapted from: Controlling position of Global or Group Palette by @ComplexPoint.

TESTED WITH

• Keyboard Maestro 11.0.3
• Sequoia 15.3.1 (24D70)/MacBookPro18,2
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

( expand / collapse )

1.0 - initial version

2.0
a) Regex pattern checks are now case insensitive.

b) Moved documentation from a header comment to the Keyboard Maestro forum. This has two advantages: i) reduces the size of the macro, and ii) streamlines maintenance of the macro.

c) For conditional steps that contain simple conditions, converted If Then Else actions to Switch/Case actions. When it can be used, the latter action has two advantages: i) it occupies less vertical space in the KM editor, and ii) if the logic does not have a false branch, then the action can be configured to have only a true section.


__ Smart Sort ⌃⌘: Macros.kmmacros (192.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.3.1 (24D70)
  • Keyboard Maestro v11.0.3

5 Likes

That's really cool. I like how you put that together.

2 Likes

It took me a bit to figure out because some names were not right for me and I wasn't sure if I was suppose type in changes to the macro or just rename the group. The GIF was helpful though it was hard to see what it was changing it by since we only see name and trigger in the Keyboard Maestro editor. Maybe someday @peternlewis will add more columns to sort by and you can click again to reverse order them.

This is very nice, thank you for making it and sharing it! I have spent several hours with these macros and getting my head a little bit wrapped around the logic here. It didn't always seem to update for me which threw me off. I had to restart Keyboard Maestro a few times, even though it was activated maybe a bit much going on in the background for it to keep up.

I might end up just using your macro that calls on explicitly. I really do like the idea of having sorting based on Macro Name. Too bad that is not a smart group option then the problem would be fixed for me.

Thanks for giving us options. You have done some amazing work here and a lot to be learned from your macros.

This doesn't seem to be the case for me it will only stay active if the Smart sorting is enabled no matter if I press Comtrol (Command+Control (⌘⌃)) + / or Power Claw (Command+Option+Control (⌘⌥⌃)) + /

Perhaps I am doing something wrong, or misinterpreting things.

There's a comment in the macro that states the following:

So, yes, the values of these settings should be adjusted to accommodate one's group naming conventions.


I've not seen that behavior.


I assume you are referring to: Sort Macros Conditionally


Hum, are you requesting a new Keyboard Maestro feature? If not, I'm confused.


The stated features work for me.

If you plan to alternatively use Sort Macros Conditionally then it's not worth troubleshooting and I suggest you delete this macro group to avoid potential conflicts.

I messed with that but I got a bit bewildered with how everything was interacting with each other and I did change some of those, but then other things broke and thought I probably needed to edit something deeper. That's what you don't know to get you that seems to be a lot for me :laughing: .

Probably just something we are going on on my system.

Yes, that was my bad for not referencing it. That is what I'm talking about.

Yes, requesting a Keyboard Maestro feature to allow smart groups to also have the option to auto sort.

Thanks for checking that and sounds good and thanks for the clarification to not have both happening, might me something I would do and get myself in a mess.

I think I will use your idea to jump to macro groups and here
https://forum.keyboardmaestro.com/t/39606/3

And then just put an auto sort at the bottom.

Select Group and Sort by Date Used

01)Select Macro Group or Smart Group by Name - (1) "**-All Macros - Active Macros in Active Groups Only - Used".kmmacros (39 KB)

I think I would use this more if it didn't default to auto sort by name if the macro doesn't meet one of the five criteria of a macro group name. Probably simple to turn off somewhere in the macro, but my head spins a bit reading through the macro and it's magical powers.

I like this idea @ccstone had of setting variables for users via an HTML prompt.

Setting a Variable Via an HTML Prompt

Setting a Variable Via an HTML Prompt.kmmacros (33 KB)

Hi @skillet.

I strongly recommend that you delete this macro group and focus your efforts on a similar but similar macro: Sort Macros Conditionally

If/when you get a good understanding and presumably like that macro, then maybe come back to Toggle Smart Sort if you are interested in the additional features.

With both macros, the configuration is rather simple. There are five variables to potentially adjust that you'll find below a comment very similar to this:

Keyboard Maestro Export

Read the comment carefully and note the link to regex patterns. You'll need a basic understanding of regex before you can change the five variables to match the naming conventions you use for Smart Groups.

Let's start with a simple example. Create a new Smart Group named Recently Modified with a Search String of mod:5d.

If you have that new Smart Group selected and trigger Sort Macros Conditionally, the macro will change the sort order to Date Modified and the most recently modified macro in the new Smart Group will appear at the top. The sort order change would occur because Recently Modified matches the [modif[iy] regex pattern.

After that macro trigger, the sort order would not change from Date Modified unless you change it yourself or you have some other Smart Group or Macro Group selected whose name does not match modif[iy] or date AND you trigger Sort Macros Conditionally again.

Once you have that working, then consider adjusting some of the patterns in the five variables, assuming the current patterns don't work well with your current naming conventions.

The key is to understand the relationship between the regex patterns in the five variables and your Smart Group naming conventions.

1 Like

Okay thank you, I have put this one on the shelf and just focusing on the other macro group.

Thank you for the detailed explanation, I am going through it one by one and the simpler macro will make things easier to undertand. Even with my own macros I sometimes have a headpin going back and reading them because they built one layer of logic after another based on various things I ran into.

I have gone through a couple of RegEx courses in the past few months so thankfully I have a better understanding there and have several apps that help me on that front.

image

RegEx

That's great thanks for explaining that.

Thanks, I will focus on that relationship and see what I can learn!

1 Like

I've updated Toggle Smart Sort to Version 2.0 :

  • Regex pattern checks are now case insensitive.

  • Moved documentation from a header comment to the Keyboard Maestro forum. This has two advantages: i) reduces the size of the macro, and ii) streamlines maintenance of the macro.

  • For conditional steps that contain simple conditions, converted If Then Else actions to Switch/Case actions. When it can be used, the latter action has two advantages: i) it occupies less vertical space in the KM editor, and ii) if the logic does not have a false branch, then the action can be configured to have only a true section.

1 Like

Thank you, I have the update as part of my macros now, nice work!