MACRO: Zip Smart Group(s), v5.0

DESCRIPTION

This macro provides a quick and simple method to back-up the macros listed in any Smart Group. All exported macros will be zipped into a single archive. The archive name includes:

  • Date & time
  • Smart Group name
  • macro count
  • zip extension

For example, for Smart Group Cre Mod 7d:

2025-01-22 0944_48 [Cre Mod 7d] (43 macros).zip


This macro uses the following Keyboard Maestro Editor native feature: File > Export > Export as Folder.... As mentioned above, this macro then subsequently zips the newly created folder.

One nice feature of Export as Folder... is that it maintains the Macro Group hierarchy when the macros are exported. Thus, even though this macro keys off Smart Groups, each macro exported is within a subfolder named to match the parent Macro Group.


Since the macro is quick and simple, it's an ideal tool to use during macro development.

For example, one could create a Smart Group named Cre Mod 4h with two match strings, cre:4h and mod:4h, then run this macro and select that Smart Group. In just a few seconds all macros created or modified within the last 4 hours, would be saved within a single zip file.

Since the macro provides an option to reveal the archive in the Finder after creation, it becomes very easy to manage the archive files.

ADDITIONAL FEATURES

Initial state restored. When the macro is triggered, the macro saves the state of the Keyboard Maestro Editor (including macro group and macro selections). When the macro completes, the state is restored.

Export names adjusted as needed. The Keyboard Maestro Export as Folder... command automatically replaces colons ( : ) and slashes ( / ) with dashes ( - ) in macro groups and macros to maintain compatibility with macOS file names. In addition, with this macro, if the first character is dot ( . ) or dash ( - ), it is replaced with an underscore. (If the first character was a dot, but not changed, the file would be invisible in the Finder; if the first character was a dash, but not changed, shell command syntax would be more complicated.) If local_DropboxSafe = 1, then additional replacements are made. See SETTINGS below.

Export confirmation. The Keyboard Maestro Search field is cleared before the macros are exported. When the export completes, before the export folder is zipped, the exported macros are counted and compared to the number of macros that are in the selected Smart Group. If counts do not match, a notification is generated and the information is also added to the summary dialog.

Prompts and feedback adjustable. If the macro is run from another macro, the smart group selection menu can be bypassed. (See USING ANOTHER MACRO TO TRIGGER for more information.) In addition, after the archive is created, the macro can be directed to: 1) do nothing, 2) display a summary dialog, or 3) reveal the archive in the Finder.

SETTINGS

> local_BackupRootPath
All backup zip files will be be written to this folder. If they do not exist, the subfolders in the path (below home: ~) will be created by the macro. The default is:

~/Documents/Automation/_Keyboard Maestro/Backups

If this path is not specified, the archive files will be written to: ~/Downloads

> local_SmartGroupPatterns
This variable can be optionally used to limit the Smart Groups displayed for backup. Each line is used as a Regular Expression pattern.

If nothing is specified (the default), the macro will provide an option to back up any of the Smart Groups.

> local_DefaultFeedback : [dialog] | [reveal] | [none]; default [reveal] : when this macro is triggered directly, the action that follows the creation of each zip file:

  • [dialog] : A dialog will appear after the Smart Group is archived to a zip file. If multiple Smart Groups are specified, then the final dialog will also include a Reveal button .

  • [reveal] : No dialogs will appear, however, the zip file created will be revealed in the Finder. If multiple Smart Groups are specified, then the last created zip file will be revealed.

  • [none] : Aside from the normal Export to Folder... notification, there will be no feedback.

In all cases, when the macro completes, the Keyboard Maestro Editor will be returned to its initial state including the selections within the Groups and Macros columns.

> local_DefaultFeedbackWhenCalled : [dialog] | [reveal] | [none]; default [reveal] : when this macro is triggered from another macro or via AppleScript, the action that follows the creation of each zip file:

  • [dialog] : A dialog will appear after the Smart Group is archived to a zip file. If multiple Smart Groups are specified, then the final dialog will also include a Reveal button .

  • [reveal] : No dialogs will appear, however, the zip file created will be revealed in the Finder. If multiple Smart Groups are specified, then the last created zip file will be revealed.

  • [none] : Aside from the normal Export to Folder... notification, there will be no feedback.

In all cases, when the macro completes, the Keyboard Maestro Editor will be returned to its initial state including the selections within the Groups and Macros columns.

> local_DropboxSafe : 0|1; default 0 : In exported macro groups and macros, leading dots ( . ), leading dashes ( - ), leading spaces, and problematic characters for Dropbox ( / \ < > : " | ?, emojis, and other non-ascii characters) are replaced with underscores ( _ ). In addition, trailing dots ( . ) and spaces are removed.


USING ANOTHER MACRO TO TRIGGER

This macro can be triggered using the Execute a Macro action in another macro; the Smart Group(s) are specified using the With Parameter, one Smart Group Name or UUID per line. In this mode, the Smart Groups Prompt With List will not be displayed. Since the setting local_SmartGroupPatterns does not apply in this mode, the macros in any configured Smart Group can be zipped.

In addition to the Execute a Macro action, the primary macro can be triggered using AppleScript within or outside Keyboard Maestro. Here's an example zipping two Smart Groups:

-- Zip Smart Group(s)

property theP : "4-Cre Mod 7d
F542D717-B0F7-4361-B44B-E16892686B67"

tell application "Keyboard Maestro Engine"
	do script "43C753A4-C71C-45DD-9F65-346DB1A8350A" with parameter theP
end tell

Note that in this example a Smart Group name is used for the first Smart Group, and a UUID is used for the second. (See property theP.)

Lastly, the first line of With Parameter can optionally be [dialog], [reveal], or [none]. See the local_DefaultFeedbackWhenCalled setting for more information.

OTHER BACKUP MACROS AVAILABLE IN THE FORUM

If you are looking for a full-featured back-up tool, I suggest you check out:

There may be others that I've missed.

This macro was inspired by @ronald's idea in this thread.

TESTED WITH

• Keyboard Maestro 11.0.3
• Sequoia 15.3 (24D60)/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) Macro can now be called using the Execute a Macro action and the Smart Group(s) can be specified using the With Parameter, one Smart Group Name or UUID per line.

b) Macro name changed from Zip a Smart Group to Zip Smart Group(s).

3.0
a) Added feedback dialog if an empty Smart Group is selected.

b) Reduced pause durations. For those using a very slow mac, it might be necessary to increase them.

c) Improved efficiency and readability of the JXA code that gathers the Smart Group information.

d) Fixed a dialog typographical error.

4.0
a) Moved the macro to a group that can be run regardless of the running state of the Keyboard Maestro Editor. When the macro completes, the Keyboard Maestro Editor will be returned to its initial state including the selections within the Groups and Macros columns.

b) After the macros are exported and before they are zipped into an archive, macro groups and macro names are changed by substituting any leading dots (.) to a dash (-). This change makes the folders/files visible in the Finder.

c) Added setting local_DropboxSafe. If 1, the zip file name and exported macro groups and macros will be renamed to avoid characters that are problematic for Dropbox.

d) If the macro is triggered directly, multiple Smart Groups can be selected and a dialog will appear for each Smart Group; the last dialog will include a Reveal button. If selected, the last zip file created will be revealed in the Finder. In contrast, if the macro is triggered from another macro or via AppleScript, by default no dialogs will appear and the zip file will be revealed.

e) Added setting local_DefaultFeedbackWhenCalled with options of [dialog], [reveal], or [none].

f) As with previous versions of this macro, if triggered from another macro or via AppleScript, one or more Smart Group names and/or UUID's can be specified using the With Parameter (one per line). With this macro update, the first line of With Parameter can optionally be [dialog], [reveal], or [none] to specify the post-zip feedback. If not included, the value of local_DefaultFeedbackWhenCalled will be used.

5.0

a) When local_DropboxSafe = 1, the macro now also replaces emoji characters, and other non-ascii characters, with underscores ( _ ). Therefore even if the Smart Group, Macro Group, and/or Macro Name contains non-ascii characters, the archive and included folders and files will be Dropbox compatible. Note that there is one minor anomaly with emoji transformation: if the macro group or macro name includes consecutive emoji characters, the transformation will be to one underscore, not one for each emoji.

b) Added setting local_DefaultFeedback with options of [dialog], [reveal], or [none]. The default is [dialog]. This setting is used when the macro is triggered directly.

c) The Keyboard Maestro Search field is now cleared before the macros are exported. When the export completes, before the export folder is zipped, the exported macros are counted and compared to the number of macros that are in the selected Smart Group. If counts do not match, a notification is generated and the information is also added to the summary dialog.

d) The folder enclosed in the zip archive now includes the name of the Smart Group. For example, with the zip file 2025-02-02 1514_22 [4-Cre Mod 7d] (51 macros), the archived folder would be 2025-02-02 1514_22 [4-Cre Mod 7d].

e) The JXA that collects the Smart Group information for the first Prompt With List is more memory efficient. The macro should now work properly even if the library contains hundreds of Smart Groups.

f) The JXA and shell scripts are now set to capture and report any errors that might occur. Also, the progress bars will be dismissed even if these actions were to fail.


LISTS AND DIALOGS

If the macro is triggered directly, a Prompt With List like the following will appear. (By default, all Smart Groups are listed. The list can be limited using an optional macro setting.) One or more (with ⇧+click or ⌘+click) Smart Group(s) can be selected.

By default, a dialog will appear for each Smart Group exported and zipped.


The last dialog will include and option to reveal the zip file.


If the macro is triggered from another macro using the Execute a Macro action and one or more Smart Groups are specified (by Name or UUID), the above Prompt With List is skipped and the backups immediately commence.

Keyboard Maestro Export

As depicted above, the first line of With Parameter can be used to specify the post-zip feedback. If not included, the value specified in a macro setting will be used.

DOWNLOADS

PRIMARY MACRO

Download: Zip Smart Group(s).kmmacros (175 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 (24D60)
  • Keyboard Maestro v11.0.3


EXAMPLE CALLING MACRO

Download: 𝘊𝘢𝘭𝘭 Zip Smart Group(s).kmmacros (6.7 KB)

Macro-Image


3 Likes

I've updated the OP with Version 2.0 of Zip Smart Group(s):

  • Macro can now be called using the Execute a Macro action and the Smart Group(s) can be specified using the With Parameter, one Smart Group Name or UUID per line.

  • Macro name changed from Zip a Smart Group to Zip Smart Group(s) .

3 Likes

I've updated the OP with Version 3.0 of Zip Smart Group(s):

  • Added feedback dialog if an empty Smart Group is selected.

  • Reduced pause durations. For those using a very slow mac, it might be necessary to increase them.

  • Improved efficiency and readability of the JXA code that gathers the Smart Group information.

  • Fixed a dialog typographical error.

In the MacroBackerUpper thread, @ronald had this request:

Since sets of macros like these can be be created with Smart Groups, I suggested that @ronald use MacroBackerUpper at a lower frequency, then use Zip Smart Group(s) to address the above requirements.


Using the Search Strings syntax, here are the Smart Group definitions for each set...

1. modified or changed created in the past x number of days

I suspect @ronald intended to specify created, not changed, thus for five days, this is simply:

mod:5d
cre:5d

2. all macros in selected macro group

In most cases, this is simply:

gr:"group_name"

But I've found in some cases it's a bit trickier. For example, if you have the following three Macro Groups:

⋮Notes
⋮Notes ⚪️
⋮Notes ☆

And if you want the macros in ⋮Notes (only), one might think that this definition would work:

gr:"⋮Notes"

But the above is a bit greedier than I expected: it includes macros from all three Macro Groups. To limit the Smart Group to the first Macro Group, this definition can be used:

gr:"⋮Notes" -gr:"⋮Notes ⚪️" -gr:"⋮Notes ☆"

1 Like

Hello @_jims ,

Your macro is superb ! I spent a few hours playing around with it.

Just a few points. Take my comments with a grain of salt: you are the expert and I am a dilettante.

1- sometimes it worked, other times not. The problem was that I chose a dropbox folder for backup, and dropbox forbids certain characters in the filename.
All works fine with non dropbox folders. I tested many times.
This is a known issue with KM export as folder. I had forgotten about it.
Should the documentation suggest a non dropbox folder ?

2- when the macro is finished running, I am in KM, in the smart group being backed up with all macros in that smart group actively selected. Based on past experience, I have found this to be dangerous (for me). One accidental delete and it's a disaster. Once again a similar situation has happened to me, so it's not just my imagination.
A simple solution is simply to insert a type "→ " action at that point. I do it manually, but it could be included in the macro.

3- I can't figure out why the macro ends in KM as above. It's as if the whole post backup action group does not run. I tried running the macro and pressing ⌘ at the same time, but it simply causes the macro to cancel).

Minor points about a brilliant piece of work. Thank you VERY much !

image

1 Like

Hi, @ronald. Thanks for the feedback. I'm going to send you a PM with an updated version for you to test.

1 Like

fantastic ! thank you very much

Hello Jim, you use the syntax cre: mod:1 wk . I thought that the syntax would be cre:1 wk mod: 1 wk
thank you

Where did I do that?

Were you referring to the name of a Smart Group or the Search Strings within it?

The name can be anything; the Search Strings within a Smart Group, however, must follow: Search Strings

1 Like

I now understand ! thank you

1 Like

just an idea based on an experience from last night in which I used cron to backup instead of doing it manually.

The idea is to display at the end of the backup a notification or text display window to confirm that the backup was done and listing the full path of all zip files created.

Otherwise, every morning (like me this morning) the user has to check the backup(s) of the night before. This is necessary. in my case, the backup had failed because of a mistake I made in the cron. My point is that it's tedious to have to check every morning.

thank you

Hi, @ronald. Thanks for the feedback and suggestions.

I’ve been working on an enhanced version of the macro that should address this issue and others.

Right now I have some other priorities and haven’t had time to finish the revisions and test the changes. I hope to have an update later this week.

1 Like

great ! thanks very much. Please take your time !

I've updated the OP with Version 4.0 of Zip Smart Group(s):

  • Moved the macro to a group that can be run regardless of the running state of the Keyboard Maestro Editor. When the macro completes, the Keyboard Maestro Editor will be returned to its initial state including the selections within the Groups and Macros columns.

  • After the macros are exported and before they are zipped into an archive, macro groups and macro names are changed by substituting any leading dots (.) to a dash (-). This change makes the folders/files visible in the Finder.

  • Added setting local_DropboxSafe. If 1, the zip file name and exported macro groups and macros will be renamed to avoid characters that are problematic for Dropbox.

  • If the macro is triggered directly, multiple Smart Groups can be selected and a dialog will appear for each Smart Group; the last dialog will include a Reveal button. If selected, the last zip file created will be revealed in the Finder. In contrast, if the macro is triggered from another macro or via AppleScript, by default no dialogs will appear and the zip file will be revealed.

  • Added setting local_DefaultFeedbackWhenCalled with options of [dialog], [reveal], or [none].

  • As with previous versions of this macro, if triggered from another macro or via AppleScript, one or more Smart Group names and/or UUID's can be specified using the With Parameter (one per line). With this macro update, the first line of With Parameter can optionally be [dialog], [reveal], or [none] to specify the post-zip feedback. If not included, the value of local_DefaultFeedbackWhenCalled will be used.

1 Like

Hi, @ronald. See Version 4.0.

If you run Zip Smart Group(s) from another macro and specify the Smart Group(s) (by Name or UUID, one per line), the main Prompt With List and dialogs will not appear (assuming you use the default Zip Smart Group(s) settings). Instead, the last zip file will be revealed (i.e., selected) in the Finder.

If you sort the enclosing folder by date, you will be able to quickly confirm that the other zips were created.

1 Like

I've updated the OP with Version 5.0 of Zip Smart Group(s):

  • When local_DropboxSafe = 1, the macro now also replaces emoji characters, and other non-ascii characters, with underscores ( _ ). Therefore even if the Smart Group, Macro Group, and/or Macro Name contains non-ascii characters, the archive and included folders and files will be Dropbox compatible. Note that there is one minor anomaly with emoji transformation: if the macro group or macro name includes consecutive emoji characters, the transformation will be to one underscore, not one for each emoji.

  • Added setting local_DefaultFeedback with options of [dialog], [reveal], or [none]. The default is [dialog]. This setting is used when the macro is triggered directly.

  • The Keyboard Maestro Search field is now cleared (using a method contributed by @Nige_S) before the macros are exported. When the export completes, before the export folder is zipped, the exported macros are counted and compared to the number of macros that are in the selected Smart Group. If counts do not match, a notification is generated and the information is also added to the summary dialog.

  • The folder enclosed in the zip archive now includes the name of the Smart Group. For example, with the zip file 2025-02-02 1514_22 [4-Cre Mod 7d] (51 macros), the archived folder would be 2025-02-02 1514_22 [4-Cre Mod 7d].

  • The JXA that collects the Smart Group information for the first Prompt With List is more memory efficient. The macro should now work properly even if the library contains hundreds of Smart Groups.

  • The JXA and shell scripts are now set to capture and report any errors that might occur. Also, the progress bars will be dismissed even if these actions were to fail.


I'd like to acknowledge @ronald: he suggested some of the above enhancements and he tested several drafts before this release.

1 Like

great. fantastic macro and thank you for mentioning me which was not necessary.

1 Like

when you have a minute, please see my post How to design a macro to ... cleanse all macros of Dropbox and Mac OS forbidden characters? - Questions & Suggestions - Keyboard Maestro Discourse