Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Anykey hotkeys.
  • Loading branch information
temochka committed Feb 25, 2021
1 parent e335a1a commit 913f6f2
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -4,7 +4,7 @@ APPLESCRIPT := applescript/*/*.applescript
JXA := applescript/*/*.js
LAUNCHERS := applescript/*/launcher.json

all: launchers applescript-automation jxa-automation open-note-app alfred-workflow ical-alfred
all: launchers applescript-automation jxa-automation open-note-app alfred-workflow ical-alfred hotkeys
install:
cp $(OUTPUT_DIR)/cli/* ~/bin/
clean:
Expand All @@ -14,6 +14,7 @@ target-dir:
mkdir -p $(OUTPUT_DIR)/applescript;
mkdir -p $(OUTPUT_DIR)/apps;
mkdir -p $(OUTPUT_DIR)/cli;
mkdir -p $(OUTPUT_DIR)/hotkeys;
for dir in $$(find applescript -mindepth 1 -maxdepth 1 -not -type f); do \
mkdir -p $(OUTPUT_DIR)/applescript/$$(basename $$dir); \
done
Expand Down Expand Up @@ -60,3 +61,6 @@ sync-alfred-workflow: Process.alfredworkflow
ical-alfred: target-dir cli/ical-alfred.swift
@xcrun swiftc -sdk $(shell xcrun --show-sdk-path --sdk macosx) -o $(OUTPUT_DIR)/cli/ical-alfred cli/ical-alfred.swift
chmod +x $(OUTPUT_DIR)/ical-alfred

hotkeys: target-dir applescript hotkeys/Anykey.base.json
./hotkeys/build.rb > $(OUTPUT_DIR)/hotkeys/Anykey.json
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,13 @@ Then install the workflow via:
open target/Process.alfredworkflow
```

<!-- End Process -->
<!-- End AlfredProcess -->

## Hotkeys

I use an app I built called [Anykey](https://github.com/temochka/Anykey) to assign hotkeys to some of the automations. The Anykey configuration file is automatically built from individual launchers.

<!-- End Hotkeys -->

<img src="AlfredProcess/assets/icons/calendar-alt.png" width="75" alt="Calendar Icon" title="Calendar" align="right" style="background-color: #fff;">

Expand Down
4 changes: 4 additions & 0 deletions applescript/com.apple.AddressBook/launcher.json
Expand Up @@ -8,6 +8,10 @@
}
, { "title": "View in Alfred"
, "arg": "view_in_alfred.scpt"
, "anykey":
{ "key": "return"
, "modifiers": ["", ""]
}
}
]
}
4 changes: 4 additions & 0 deletions applescript/com.apple.Notes/launcher.json
Expand Up @@ -20,6 +20,10 @@
{ "filter_placeholder_title": "Choose the destination folder..."
, "filter_script": "filter_destination_folders.scpt"
}
, "anykey":
{ "key": "m"
, "modifiers": ["", ""]
}
}
, { "title": "Selected text to OmniFocus task"
, "arg": "selection_to_omnifocus.scpt"
Expand Down
19 changes: 19 additions & 0 deletions hotkeys/Anykey.base.json
@@ -0,0 +1,19 @@
{
"hotkeys":
[ { "title": "Launch iTerm 2"
, "key": "`"
, "modifiers": ["", ""]
, "shellCommand": "open /Applications/iTerm.app"
}
, { "title": "Launch Music"
, "key": "F8"
, "modifiers": ["fn", ""]
, "shellCommand": "open /System/Applications/Music.app"
}
, { "title": "Launch Process"
, "key": "space"
, "modifiers": ["", ""]
, "shellCommand": "osascript -e 'tell application \"Alfred 4\" to search \"proc\"'"
}
]
}
42 changes: 42 additions & 0 deletions hotkeys/build.rb
@@ -0,0 +1,42 @@
#!/usr/bin/env ruby

require 'json'
require 'shellwords'

SCRIPTS_DIR = File.join(__dir__, '..', 'applescript')
SCRIPT_TRIGGER =
<<~APPLESCRIPT
tell application id "com.runningwithcrayons.Alfred" to run trigger "script" in workflow "com.temochka.alfred.process" with argument "%<arg>s"
APPLESCRIPT

script_hotkeys = Dir.glob(File.join(SCRIPTS_DIR, '*', 'launcher.json')).flat_map do |launcher|
JSON.parse(File.read(launcher), symbolize_names: true)
.fetch(:items)
.select { |item| item[:anykey] }
.map do |item|
arg = {
alfredworkflow: item.merge(
variables: item.fetch(:variables, {}).merge(
focusedapp: File.join(File.basename(File.dirname(launcher)))
)
)
}

item[:anykey]
.merge(
shellCommand: "osascript -e '#{format(SCRIPT_TRIGGER, arg: arg.to_json.gsub(/\"/, "\\\""))}'",
title: item[:anykey].fetch(:title, item[:title])
)
.merge(
if item[:keyword]
{}
else
{ onlyIn: [File.basename(File.dirname(launcher))] }
end
)
end
end

base_hotkeys = JSON.parse(File.read(File.join(__dir__, 'Anykey.base.json')), symbolize_names: true).fetch(:hotkeys, [])

puts JSON.pretty_generate(hotkeys: base_hotkeys + script_hotkeys)

0 comments on commit 913f6f2

Please sign in to comment.