Can I edit a smart group search string via script?

I would like to edit a smart group's search string via script. Is this possible and if so how would I do it?
AppleScript would be preferred for this, but failing that I guess I could use click on found image.
TIA - Larry W.

Looking at Keyboard Maestro's AppleScript classes reveals this:

KM 0 2022-05-17_22-58-23

which implies you can edit a smart group's search strings via scripting.

In outline:

  • Get the smart group you want to change
  • Get the search string to what you want

So if I've got a single smart group called "Rotations" whose search string is "n:Rotat", a simple "Execute AppleScript" action of:

tell application "Keyboard Maestro"
	set theGroup to (item 1 of (get every smart group whose name contains "Rotat"))
	set search strings of theGroup to {"n:number"}
end tell

...will change the search string to "n:number".

That might get you started, otherwise:

Will it always be the same smart group, with the same name?
Do you want to replace all the search strings, just some, how will you choose? What's your replacement going to be, do you want to edit it freestyle or maybe pick from a list?
And probably more questions!

Well let's see.

What I want to end up with is a pallet that has all the combinations of hotkey modifiers (15). Clicking on one will then select a single Smart Group and change it's search string accordingly.

Currently I have 15 Smart Groups to show what macros use the various combinations of modifiers. It works well but takes up way too much space in the Group column.

Thanks, That should be all I need to get this done.

Gotcha. I'm guessing this is because groups can't be nested, so while you'll end up having more macros the Groups list will be less cluttered.

I think the cleanest way is to have a single macro that does the "search string setting", using Globals set by each of your 15 paletted macros (since we need Globals for AppleScript anyway, no point in a parameterised sub-routine).

I've gone a bit OTT and passed the Smart Group name as a Global as well, so you don't have to edit the script to change the Smart Group name -- just change the triggering macro's "Set Variable "Global_SGName" action to suit.

Note that a "h:⌘" search finds all macros with "⌘" in the hotkey trigger regardless of other modifiers used. So I've negatived-out the modifiers we aren't searching for and rolled it into one long search term, eg: h:⌘ -h:⌃ -h:⌥ -h:⇧, but you could also do these as individual items in the search term list.

Marking the just-pressed palette button is easy, unmarking the "old" button less so -- I can't find a way of passing a variable into the "Unmark" action, and I was left with explicitly iterating through all 15 to unmark everything then marking the single current item. Life's too short for that... Would love to hear if anyone has a better way!

As you see, there's a single "Set Smart Group Search" macro, and five example macros in the "Mods Palette" group. It all works on a "Trigger Mods SG" smart group, but you can change the first action in every "Mods Palette" macro if you want to target a different named group.

Sample Mods Palette Macro:

Set Smart Group Search.kmmacros (2.2 KB)
Mods Palette Macros.kmmacros (9.7 KB)

See how you get on!

So here's what I ended up doing:

I set up a smart group called "Display macros by use of modifiers".

I also set up a regular macro group called "Modifier Use Macros" which contains 16 macros. Each of these macros represent a unique combination of modifiers and they change the search string of the Smart Group accordingly. After they change the search string they select the Smart Group and that in turns shows me what macros are using a particular set of modifier keys. The 16th is for the Escape key.

I have a shortcut that displays a pallet of macros – the 16 macros in the "Modifier Use Macros" group.

Nice and simple and it all works great.

Thanks to @Nige_S for all the help.