Created
May 7, 2021 13:22
-
-
Save flee2free/94a2a01cf702d7ae7aab9a9736b0fb99 to your computer and use it in GitHub Desktop.
Keyboard Maestro Launcher Macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gre: | |
C1: | |
name: F14 Finder | |
hot_key: F14 | |
code: 107 | |
bundle: com.apple.finder | |
path: /System/Library/CoreServices/Finder.app | |
C2: | |
name: F15 Brave | |
hot_key: F15 | |
code: 113 | |
bundle: com.brave.Browser | |
path: /Applications/Brave Browser.app | |
C3: | |
name: F16 Bear | |
hot_key: F16 | |
code: 106 | |
bundle: net.shinyfrog.bear | |
path: /Applications/Bear.app | |
C4: | |
name: F17 Devon | |
hot_key: F17 | |
code: 64 | |
bundle: com.devon-technologies.think3 | |
path: /Applications/DEVONthink 3.app | |
proshop: | |
C1: | |
name: F14 Forklift | |
hot_key: F14 | |
code: 107 | |
bundle: com.binarynights.ForkLift-3 | |
path: /Applications/ForkLift.app/Contents/MacOS/ForkLift | |
C2: | |
name: F15 | |
hot_key: F15 Brave | |
code: 113 | |
bundle: com.brave.Browser | |
path: /Applications/Brave Browser.app | |
C3: | |
name: F16 Intellij | |
hot_key: F16 | |
code: 106 | |
bundle: com.jetbrains.intellij | |
path: /Applications/IntelliJ IDEA.app/Contents/MacOS/idea | |
C4: | |
name: F17 | |
hot_key: F17 | |
code: 64 | |
bundle: com.googlecode.iterm2 | |
path: /Applications/iTerm.app/Contents/MacOS/iTerm2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
L_ENV=$1 | |
L_PLIST="Info.plist" | |
MASTER_LAUNCHER="Master Launcher" | |
parse_yaml() { | |
local prefix=$2 | |
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') | |
config="$(sed -ne "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | | |
awk -F$fs '{ | |
indent = length($1)/2; | |
vname[indent] = $2; | |
for (i in vname) { if (i > indent) { delete vname[i]; }} | |
if (length($3) > 0) { | |
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")} | |
printf("%s%s%s=\"%s\"~", "'$prefix'",vn, $2, $3); | |
} else {printf("^")} | |
}')" | |
IFS=$'^' | |
for i in $config; do | |
local w='^config_\(.*\)_C[0-8]_name=\"\(.*\)\".*key=\"\(.*\)\".*code=\"\(.*\)\".*bundle=\"\(.*\)\".*path=\"\(.*\)\".*' | |
echo $(echo $i | sed -ne "s|$w|\1~\2~\3~\4~\5~\6|p") | |
done | |
} | |
__build_trigger() { | |
config_code=$1 | |
tmp=$2 | |
/usr/libexec/Plistbuddy -c "clear dict" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :FileType string Pressed" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :KeyCode integer $config_code" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :MacroTriggerType string HotKey" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :Modifiers integer 0" $L_PLIST | |
/usr/libexec/PlistBuddy -x -c "Print" $L_PLIST | sed '1,3d;$d' | tr -d '[[:space:]]' >> $tmp | |
} | |
__build_actions() { | |
config_bundle=$1 | |
config_name=$2 | |
config_path=$3 | |
tmp=$4 | |
/usr/libexec/Plistbuddy -c "clear dict" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :AllWindows bool true" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :AlreadyActivatedActionType string SwitchToLast" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :Application dict" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :Application:BundleIdentifier string $config_bundle" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :Application:Name string $config_name" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :Application:NewFile string $config_path" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :MacroActionType string ActivateApplication" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :ReopenWindows bool false" $L_PLIST | |
/usr/libexec/Plistbuddy -c "add :TimeOutAbortsMacro bool true" $L_PLIST | |
/usr/libexec/PlistBuddy -x -c "Print" $L_PLIST | sed '1,3d;$d' | tr -d '[[:space:]]' >> $tmp | |
} | |
__build_macros() { | |
tmpFileTrigger=$1 | |
tmpFileActions=$2 | |
config_name=$3 | |
osascript <<-EndOfScript | |
set srcFileTrigger to "$tmpFileTrigger" | |
set srcFileTrigger to POSIX file srcFileTrigger | |
set theFileContentTrigger to (read file srcFileTrigger) | |
set srcFileAction to "$tmpFileActions" | |
set srcFileAction to POSIX file srcFileAction | |
set theFileContentAction to (read file srcFileAction) | |
tell application id "com.stairways.keyboardmaestro.editor" | |
set mg to macro group "$MASTER_LAUNCHER" | |
tell mg | |
set m to make new macro with properties {name:"$config_name"} | |
tell m | |
make new trigger with properties {xml:theFileContentTrigger} | |
make new action with properties {xml:theFileContentAction} | |
end tell | |
end tell | |
end tell | |
EndOfScript | |
} | |
__delete_macro_group() { | |
osascript <<-EndOfScript | |
tell application id "com.stairways.keyboardmaestro.editor" | |
try | |
delete macro group "Master Launcher" | |
end try | |
end tell | |
EndOfScript | |
echo "Macro Group Deleted" | |
} | |
__init_macro_group() { | |
__delete_macro_group | |
osascript <<-EndOfScript | |
tell application id "com.stairways.keyboardmaestro.editor" | |
set mg to make new macro group with properties {name:"$MASTER_LAUNCHER"} | |
end tell | |
EndOfScript | |
echo "Initalized Macro Group" | |
} | |
__main() { | |
[[ $# == 0 || $# > 1 ]] && echo 'Please pass the YAML index.' && exit | |
if [[ $1 == "-d" ]]; then | |
__delete_macro_group | |
exit | |
fi | |
cd ~/lib/launcher/ | |
local lines=$(parse_yaml config.yml 'config_') | |
local available=$(echo "$lines" | cut -d '~' -f1 | sort | uniq) | |
[[ ! $available =~ $L_ENV ]] && echo 'Invalid Index' && exit | |
__init_macro_group | |
IFS=$'\n' | |
for config in $lines; do | |
if [ ! -z $config ] && [[ $config =~ $@ ]]; then | |
config_name=$(echo $config | awk -F~ '{print $2}') | |
config_key=$(echo $config | awk -F~ '{print $3}') | |
config_code=$(echo $config | awk -F~ '{print $4}') | |
config_bundle=$(echo $config | awk -F~ '{print $5}') | |
config_path=$(echo $config | awk -F~ '{print $6}') | |
tmpFileTrigger=$(mktemp) | |
tmpFileActions=$(mktemp) | |
__build_trigger $config_code $tmpFileTrigger | |
__build_actions $config_bundle $config_name $config_path $tmpFileActions | |
echo "----------" | |
echo $config_name | |
echo $config_key | |
echo $config_bundle | |
echo $config_path | |
echo "----------" | |
echo $tmpFileTrigger | |
echo $tmpFileActions | |
__build_macros $tmpFileTrigger $tmpFileActions $config_name | |
echo "Cleaning Tmp Files..." | |
rm -f ${tmpFileTrigger} | |
rm -f ${tmpFileActions} | |
fi | |
done | |
} | |
__main $@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment