In this tutorial I'll share how I use Alfred to launch a Keyboard Maestro macro.
Of course there are many ways to trigger a KM macro, but I've found Alfred Workflows work quite nicely when I want to conditionally run a macro by supplying information via the %TriggerValue%.
I'll use Note Picker as the example macro; it can run in two modes:
- a value is not supplied via the %TriggerValue%, or
- a value of f/ folder_substring or t/ title_subscring is supplied via the %TriggerValue%.
See Note Picker for more information regarding the two modes.
Using the Alfred Workflow that I'll detail below, Note Picker can be triggered four different ways:
Method A
Method B
Method C
Method D
With Method A (user types: np
︎), Note Picker is run exactly as if it was triggered using the hot key configured in Keyboard Maestro.
Method B (user types: np log
︎) and D (user types:
np t.log
︎) are interchangeable. In these cases Note Picker is triggered and the macro receives t/ log via the %TriggerValue%.
With Method C (user types: np f.mac
︎), Note Picker is triggered and the macro receives f/ mac via %TriggerValue%.
Here's the Alfred Workflow:
The Input>Keyword is configured as follows:
Keyword: np
Title: np: KM Note Picker +'{query}'
Subtext: ⒪ title substring; f.folder substring; t.title substring
Aside: By convention, when I configure the Subtext for Input>Keywords, I use an ⒪
prefix to indicate that an argument is optional. In this case, that means that a user can launch Alfred and simply type np
︎ (that is, like with Method A).
The left Actions>Run Script block is as follows:
shell script ( expand / collapse )
# Build the %TriggerValue% for the macro
query=$1
# If nothing passed, then echo null
if [ -z "$query" ]
then
echo -n
# If f.some text is passed, then echo f/ some text
elif [[ "$query" =~ ^f\.* ]]
then
echo -n f/ ${query:2}
# If t.some text is passed, then echo f/ some text
elif [[ "$query" =~ ^t\.* ]]
then
echo -n t/ ${query:2}
# Otherwise, assume it a title and echo some text
else
echo -n t/ ${query}
fi
The right Actions>Run Script block is as follows:
shell script ( expand / collapse )
query=$1
# echo -n $query
osascript <<EndOfScript
-- Note Picker property theP : "$query"
tell application "Keyboard Maestro Engine"
do script "A3DF7C60-CF4B-4E64-AD35-72BAB6937E9F" with parameter theP
end tell
EndOfScript
I created the AppleScript portion of the above shell script using a utility macro:
Download: AppleScript To Run Selected Macro.kmmacros (16 KB)
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 13.4.1 (22F82)
- Keyboard Maestro v10.2