-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgenerate_autokeys.py
executable file
·91 lines (68 loc) · 2.74 KB
/
generate_autokeys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
"""
Script to turn the json file of output/shortcuts.yaml into config files for Autokey
"""
import json
import os
import yaml
def make_safe_filename_from_string(s : str) -> str:
"""Make a filename safe for use in a file path.
https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename"""
return "".join(x.lower() for x in s if x.isalnum())
def create_autokey_config_for_shortcut(phrase : str, abbrev : str) -> None:
"""Generate configs for Autokey based on a shortcut from phrase to abbreviation.
https://github.com/autokey/autokey
abbrev can be a string, or a comma separated list of abbreviations.
these files go in ~/.config/autokey/data/My Phrases/
Each abbrev gets two files, name.txt and .name.json"""
file_extensions = ["py"] # add '.' as a word character for any of these abbrevs.
if abbrev in file_extensions:
extra_word_chars = r"\."
else:
extra_word_chars = ""
words_regex = rf"[\w\t'{extra_word_chars}\-&\+]" # what should not trigger a substitution
result_path = "output/autokey_phrases/"
filter_regex = "google-chrome.Google-chrome" # shortcut only in these apps
# has any capitals in phrase
has_capitals = any(c.isupper() for c in phrase)
name = make_safe_filename_from_string(phrase)
os.makedirs(result_path, exist_ok=True)
abbrevs = [a.strip() for a in abbrev.split(",")]
with open(os.path.join(result_path, f"{name}.txt"), 'w', encoding="utf8") as f:
f.write(phrase)
with open(os.path.join(result_path, f".{name}.json"), 'w', encoding="utf8") as f:
config = {
"usageCount": 0,
"omitTrigger": False,
"prompt": False,
"description": phrase,
"abbreviation": {
"wordChars": words_regex,
"abbreviations": abbrevs,
"immediate": False,
"ignoreCase": not has_capitals,
"backspace": True,
"triggerInside": False
},
"hotkey": {
"hotKey": None,
"modifiers": []
},
"modes": [
1
],
"showInTrayMenu": False,
"matchCase": not has_capitals,
"filter": {
"regex": filter_regex,
"isRecursive": False
},
"type": "phrase",
"sendMode": "kb"
}
json.dump(config, f, indent=4)
if __name__ == "__main__":
with open("output/shortcuts.yaml", 'r', encoding="utf8") as file:
shortcuts = yaml.load(file, Loader=yaml.FullLoader)
for phrase, abbrev in shortcuts.items():
create_autokey_config_for_shortcut(phrase, abbrev)