Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Created May 16, 2024 09:15
Show Gist options
  • Save frankrolf/2132e17236564ea7c585e7ff80055622 to your computer and use it in GitHub Desktop.
Save frankrolf/2132e17236564ea7c585e7ff80055622 to your computer and use it in GitHub Desktop.
Robofont: which shortkeys are assigned to what action?
'''
Robofont: which shortkeys are assigned to what action?
'''
import AppKit
import itertools
import mojo
import re
from pathlib import Path
def make_key_code_dict():
modifiers = {
AppKit.NSEventModifierFlagOption: '⌥',
AppKit.NSEventModifierFlagCommand: '⌘',
AppKit.NSEventModifierFlagControl: '^',
AppKit.NSEventModifierFlagShift: '⇧',
}
key_codes = {
# no idea how to get to those key codes in a smarter way
'\uf700': '↑',
'\uf701': '↓',
'\uf72c': '⇞',
'\uf72d': '⇟',
}
for i in range(1, len(modifiers) + 1):
# calculcate key codes for all possible combinations of modifier keys
for key_combination in itertools.combinations(modifiers.keys(), i):
key_labels = [modifiers.get(key_code) for key_code in key_combination]
key_code = sum(key_combination)
key_codes[key_code] = ' + '.join(key_labels)
return key_codes
def output_menu_dict(menu_dict, headline, clear=True):
longest_command = max([len(command) for command in menu_dict.keys()])
w = mojo.UI.OutputWindow()
if clear:
w.clear()
w.write('\n' + headline + '\n')
w.write('-' * len(headline) + '\n')
# sorted by action
# for key_sequence, menu_path in sorted(
# menu_dict.items(), key=lambda item: item[1]
# ):
# print(key_sequence.rjust(longest_command), ':', menu_path)
# sorted by shortcut
for key_sequence, menu_path in sorted(menu_dict.items()):
w.write(f'{key_sequence.rjust(longest_command)} : {menu_path}\n')
w.show()
def shortcuts_in_app():
key_codes = make_key_code_dict()
shortcuts = mojo.UI.getMenuShortCuts()
menu_dict = {}
for menu_path, key_sequence in shortcuts.items():
readable_key_sequence = ' + '.join(
[key_codes.get(k, str(k)) for k in key_sequence])
joined_menu_path = ' > '.join(menu_path)
menu_dict[f'{readable_key_sequence}'] = f'{joined_menu_path}'
output_menu_dict(menu_dict, 'shortcuts assigned in preferences', True)
def extract_shortkey_from_file(script_file):
with open(script_file, 'r', encoding='utf-8') as blob:
# this assumes each script to be at least 2 lines long
lines = blob.read().splitlines()[:2]
if len(lines) > 1:
sc_match = re.match(r'\s*# shortCut: (.+?)$', lines[1])
if sc_match:
return sc_match.group(1)
def shortcuts_in_scripts():
menu_dict = {}
prefs_dict = AppKit.NSUserDefaults.standardUserDefaults().dictionaryRepresentation()
scripts_dir = prefs_dict['pythonDir']
for script in Path(scripts_dir).rglob('*.py'):
key_combo = extract_shortkey_from_file(script)
if key_combo:
shortened_path = str(script.relative_to(scripts_dir)).split('/')
joined_menu_path = ' > '.join(shortened_path)
menu_dict[key_combo] = joined_menu_path
output_menu_dict(menu_dict, 'shortcuts assigned in script files', False)
if __name__ == '__main__':
shortcuts_in_app()
shortcuts_in_scripts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment