Smart Group's result sort by date

Hello,

I just learned about smart groups and how to use them in Trigger Macro by Name action. I have a smart group that displays macros used in the last 5 minutes. But the smart group result is shown in alphabetical order. Is it possible to sort the smart group result by date used? Thanks!

ChrisQ

Did you try using the mechanism described in this post?

Hi, @ChrisQ. Yes, this is a built-in feature of the Keyboard Maestro editor: View > Sort macro by > Date Used (⌃⌘E).

I've shared two related macros that you might find useful:

1 Like

I'm using Trigger Macro by Name action to retrieve the smart group's result, so the KM Editor is not used for sorting.

I have 3 macros and would like the results to be sorted like this:

  • Used 5 mins ago (smart group result sorted by Last Used)
  • Edited 3 days ago (smart group result sorted by last Modified)
  • Created 1 week ago (smart group result sorted by last Created)

Maybe this is a candidate for a feature request? @peternlewis

Then you are going to have to use AS or JXA to build your list, collecting {macroUUID,macroName,releventDate}, sort that list by releventDate, and present the result in a dialog.

Since you'll have to open the KM Editor (unless you want to parse the entire plist) you might as well write a macro that selects the appropriate Smart Group, sets the sort order, and selects the first macro in the list ready for you to arrow/type to any other. Something like:

Appreciate the solution @Nige_S. However, my main use case is the run the macro from smart group's result list. On occasions when I want to edit the macro, it opens KM Editor to that chosen macro. I try not to change the KM Editor's sort order for macros.

I saw this post N most recently run macros (Name, UUID, Stats)
Hoping @ComplexPoint could help me generate the Prompt with List for macros sorted by last (run, edited, created). :slight_smile:

OK, so not as difficult as I thought!

Here's a version that does your "Edited in the last 3 days". By default it executes the chosen macro, if you hold down ⌥ while selecting a macro it will open it for editing instead:

Modified in Last 3 Days.kmmacros (5.4 KB)

Image

Easy enough to make the other two versions. Just change

set macroList to every macro of smart group "All Macros" whose modification date > ((get current date) - (3 * days))

...to:

set macroList to every macro of smart group "All Macros" whose used date > ((get current date) - (5 * minutes))

...for "used in the last 5 minutes" or:

set macroList to every macro of smart group "All Macros" whose creation date > ((get current date) - (1 * weeks))

...for "created in the last week".

2 Likes

Yes -- and from the Editor you can select a macro and run it with either the "Run" button, File -> Run Macro, or with a shortcut (⌘R for me, you might have to set your own in System Settings or with a KM macro).

However, the AS version isn't too bad and -- importantly -- isn't too slow, even when pointed at "All Macros".

Hi @Nige_S , I'm getting this error in the KM engine when running the macro.

2024-11-28 17:26:54 Execute macro “Modified in Last 3 Days” from trigger Editor
2024-11-28 17:26:54 Action 16399067 failed: Execute an AppleScript failed with script error: text-script:34:76: script error: Invalid date and time date Thursday 1 January 1970 at 00:00:00. (-30720)
2024-11-28 17:26:54 Execute an AppleScript failed with script error: text-script:34:76: script error: Invalid date and time date Thursday 1 January 1970 at 00:00:00. (-30720). Macro “Modified in Last 3 Days” cancelled (while executing Execute AppleScript).

I had similar when setting it up.

Try editing the AppleScript action, replacing:

set baseDate to date "Thursday 1 January 1970 at 00:00:00"

...with:

set baseDate to date "1/1/1970"

KM will recompile the AS and, with that, recreate the date object -- that's what fixed it for me.

The creation date and modification date both work perfectly. They match exactly what KM Editor is showing when sorted by creation date or modification date.

However, the used date does not match what KM Editor is showing.
set macroList to every macro of smart group "All Macros" whose used date > ((get current date) - (5 * minutes))

This is great. It is what I was aiming for :slight_smile:

See, for basic listings, with macro UUID:

1 Like

My bad -- I forgot to say you also need to change line 6 so you get the correct sort order. That's the bit that goes:

		set outText to outText & ((modification date of eachMacro) - baseDate)...etc...

...so modification date becomes used date for "used in last...", and creation date for "created in last...".

If it's not the sort order -- what's the actual problem?

1 Like

Thank you @Nige_S. Everything is perfect now. Appreciate your time and effort. :slight_smile:

Hi, @ChrisQ. Glad @Nige_S was able to help you out.

Sorry about my non-helpful response above. I failed to carefully read your original question.:face_with_open_eyes_and_hand_over_mouth:

On a related front, however, you might find this macro useful: Edit a Recently Executed Macro

The macro can be used to edit or run a recently executed macro.

The Apple Script works for me but the whole macro does not.

set outText to ""
set baseDate to date "Tuesday, 11 July 2006 at 00:00:00"
tell application "Keyboard Maestro"
	set macroList to every macro of smart group "All Macros" whose modification date > ((get current date) - (3 * days))
	repeat with eachMacro in macroList
		set outText to outText & ((modification date of eachMacro) - baseDate) & tab & id of eachMacro & "__" & name of eachMacro & linefeed
	end repeat
end tell
return outText

But if the user wants to know the least used macros (and maybe their last use)?

How to change this line?

modification date > ((get baseDate?) - (3 * days?))

We can't troubleshoot unless you say in what way it doesn't work.

Then the user should go to the Editor, select the "All Macros" Smart Group (or a Group of interest), and sort by "Use Count". That information isn't directly available via AppleScript.

But you could take a look at some of the other recently-posted macros that list macro information -- one of those might do what you want by querying the database which stores that info. And keep an eye out for @DanThomas's forthcoming Macro Explorer, there's a usage count column in the screenshot.

Hey and good day Nige_S,

We can't troubleshoot unless you say in what way it doesn't work.

It was that 'nothing at all happened' when invoking the macro.

What I'm after:

There a quite a number of macros which I don't use, and the intention was to clean 'the bucket'.

This can probably be done in other ways, too.

Thany you for pointing to @DanThomas' project, looks good!

Then maybe you don't have any macros that match the criteria? If you don't have KM Engine Notifications turned on you won't know that the macro has errored because of an empty list.

I'd just use a Smart Group for a one-off like this. Sort by "Use Count", scroll to the bottom of the list (you can't reverse-sort), show the Macro Inspector, use the arrow keys to move through the macros and ⌘⌫ to delete the ones you don't want.

1 Like