Scripting nested actions is tricky.
A recent thread full of helpful and very kind posts resulted in a pair of handlers for for making the task easier.
The macro at the bottom of the post demos primarily the handler that the flattens actions and secondarily the helper handler that queries the flat actions.
The main macro enables the user to change the colors of actions and groups of existing colored actions of the currently selected macro.
Changing actions colors a few at a time is likely what works for most people.
This macro may be helpful occasionally for changing colors on multiple actions on different levels, or according to kind, or on groups of colored actions.
Its main methods could be adapted to go to any action (not just top-level actions) from a prompt list or query/set other action properties such and note, xml or name.
Usage:
-
Enable the macro group and the macro.
-
Run the macro from a trigger of your choosing.
-
From the prompt list that appears, select one or more of the actions and existing groups of colored actions of the currently selected macro.
-
Choose a color to apply to the selection from a macro palette of available colors.
-
The macro will change the items selected to the color choice.
A group of existing colored actions looks like this: Green (2). When selected, it adds the 2 green actions to the items selected.
Configuration and optimization
For better performance, suspend Edit mode.
Watching KM show the live AppleScript updates is fun, but asks too much of a UI meant for hand editing when the number of a macros's actions runs into the hundreds.
In such cases, turning off the Edit mode can speed up processing.
Similarly loading actions into the prompt can take time.
There are 3 settings related to performance and processing feedback:
- localSuspendEditModeWhenMacroActionsExceed = 100
By setting the value of this variable, you can tell the macro to toggle Edit mode off during updates and on again when the loaded number of a macro's actions exceed a specific number. It is preset to 100.
The prompt title will show the total number of actions in parentheses, followed by the name of the macro being edited.
- localShouldDisplayProgressBar = true
By default a progress window shows the number of updates as they happen. You can turn this off by setting the value of this variable to false.
- localEditLimitWarning = 100
By default a display dialog warns when over 100 actions will be colored. Set the value of localEditLimitWarning to a higher number to keep it from displaying.
Escape cancels the macro at any time.
Actions already colored will remain colored. Those not colored will be unaffected by the cancel.
To undo changes, press command + z
Explanation
Scripting nested actions is tricky.
The AppleScript, "actions of macros", retrieves only the top level actions.
One strategy to access all actions is to flatten the top level actions.
The first "Execute an AppleScript" demos use of a handler that flattens top level actions to fill a prompt with list with the names of all actions.
To filter the flat actions, a simple but slower, AppleScript repeat loop could have been used.
Instead, the second "Execute an AppleScript" demos use of a filter-by-predicate to tweak performance.
Its syntax is verbose and fiddly-looking:
plistObject's filteredArrayUsingPr`edicate:(NSPredicate's predicateWithFormat:"ActionUID ==" & quoted form of varActionID
That is too bad.
It is actually the same thing as the AppleScript plural object filter.
For Example:
"actions whose name is ..."
However, it can filter in ways impossible in AppleScript:
actions whose id is IN {"15927027","15452518","15927030"}-->won't compile
Plus, the predicate filter method is available on ANY array of actions (action plist).
Plus, regex comparisons work...
These 2 methods are most useful for searching property lists with shallow nesting, such as flat lists of macros or actions.
- actionDicts's valueForKeyPath:name == "name of actions"
- actionDict's filteredArrayUsingPredicate == "actions whose..."
Because KM uses property listβ’ xml to represent groups, macros and and actions as data, predicate/plist filtering, verbosity aside, is especially well-suited to searching these KM objects.
"Key-value coding is a fundamental concept that underlies many other Cocoa technologies, such as key-value observing, Cocoa bindings, Core Data, and AppleScript-ability (emphasis mine). Key-value coding can also help to simplify your code in some cases."
Color Multiple Actions Macros.kmmacros (65.9 KB)
Color Multiple Actions + Action Kind Groups Macros.kmmacros (68.1 KB)