Last active
May 28, 2022 20:03
-
-
Save lolmaus/0359089f21af6e5d2482 to your computer and use it in GitHub Desktop.
A script to generate AutoKey configuration files that enable a new layer on your keyboard
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
# This Ruby script generates a config for the AutoKey Linux app. | |
# The config adds another layer to your keyboard. | |
# First, choose a modifier key that will activate the layer: | |
primary_modifier = 'super' | |
# Then outline which keys will do which actions. | |
# On the left is the desired action, on the right is the hotkey | |
# that activates the action. For example, if your modifier is | |
# the the Super key, then "left: :j" means that when you press | |
# "Super+J", "Left" will be triggered. | |
key_pairs = { | |
left: :j, | |
right: :l, | |
up: :i, | |
down: :k, | |
backspace: :e, | |
tab: :s, | |
enter: :f, | |
escape: :d, | |
insert: :w, | |
delete: :r, | |
home: :u, | |
end: :o, | |
page_up: :y, | |
page_down: :h | |
} | |
# If you want additional modifiers to be available on the | |
# new layer, list them here. | |
# For example, if you have "super" as your primary modifier | |
# and ['ctrl', 'shift', 'alt'] as secondary modifiers, | |
# then the following modifier combinations will be configured | |
# for each hotkey: | |
# Super | |
# Super+Ctrl | |
# Super+Shift | |
# Super+Alt | |
# Super+Ctrl+Shift | |
# Super+Ctrl+Alt | |
# Super+Shift+Alt | |
# Super+Ctrl+Shift+Alt | |
# If you want no secondary modifiers, set this to []. | |
secondary_modifiers = ['ctrl', 'shift', 'alt'] | |
# Set the directory to generate AutoKey configuration in: | |
directory = '~/.config/autokey/data/Keymap' | |
# Below are the templates to generate config files from. | |
# Don't edit them unless you're familiar with AutoKey's guts. | |
template = 'keyboard.send_keys("%{combination}")' | |
config = { | |
usageCount: 0, | |
omitTrigger: false, | |
prompt: false, | |
abbreviation: { | |
wordChars: "[\\w]", | |
abbreviations: [], | |
immediate: false, | |
ignoreCase: false, | |
backspace: true, | |
triggerInside: false | |
}, | |
hotkey: {}, | |
modes: [ | |
3 | |
], | |
showInTrayMenu: false, | |
filter: { | |
regex: nil, | |
isRecursive: false | |
}, | |
type: "script", | |
store: {} | |
} | |
# End of configuration | |
require 'fileutils' | |
require 'json' | |
# Some reusable methods | |
class Array | |
# Returns an array of all unique variations | |
# of the given array. | |
def combinations | |
(0 .. self.length).flat_map do |size| | |
self.combination(size).to_a | |
end | |
end | |
# Adds a value to current array, | |
# either to the beginning or end of it. | |
# Returns a new array, does not modify given array. | |
def add_to_copy(item, to_beginning = false) | |
if to_beginning | |
[item] + self | |
else | |
self.dup << item | |
end | |
end | |
end | |
class String | |
def wrap_with_chevrons | |
"<#{self}>" | |
end | |
end | |
class Symbol | |
def wrap_with_chevrons | |
self.to_s.wrap_with_chevrons | |
end | |
end | |
# Preparing the directory | |
directory = File.expand_path directory | |
FileUtils.mkdir_p directory | |
# Iterating through all key pairs | |
key_pairs.each do |desired_key, hotkey| | |
# For each key pair, iterating through all possible modifier combinations | |
secondary_modifiers.combinations.each do |combination| | |
### Python script | |
# Preparing combination name (used in config filenames) | |
combination_name = combination | |
.map(&:capitalize) | |
.add_to_copy(desired_key.to_s.capitalize, true) | |
.join("+") | |
# Preparing the filename for the Python script | |
script_filename = File.expand_path "#{combination_name}.py", directory | |
# Preparing desired key combination string (used in the contents of the Python script) | |
desired_combination_string = combination | |
.add_to_copy(desired_key) | |
.map(&:wrap_with_chevrons) | |
.join("+") | |
# Preparing the contents of the Python script | |
script_body = template % {combination: desired_combination_string} | |
# Writing the Python script to disk | |
File.open script_filename, 'w' do |file| | |
file.write script_body | |
end | |
### JSON config | |
# Preparing the JSON config filename | |
config_filename = File.expand_path ".#{combination_name}.json", directory | |
# Preparing the JSON config object (used for JSON config body) | |
config_object = config.dup.tap do |config| | |
config[:description] = combination_name | |
config[:hotkey][:hotKey] = hotkey.to_s | |
config[:hotkey][:modifiers] = combination | |
.add_to_copy(primary_modifier) | |
.map(&:wrap_with_chevrons) | |
end | |
# Preparing the JSON config body | |
config_body = JSON.pretty_generate config_object | |
# Writing the JSON config to disk | |
File.open config_filename, 'w' do |file| | |
file.write config_body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment