forked from rjames86/textexpander_to_keyboardmaestro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TE.py
274 lines (241 loc) · 9.73 KB
/
TE.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import plistlib
import os
import glob
'''
This script will parse through all group_*.xml files within your TextExpander folder.
Anything marked as Plain Text, Shell Script or JavaScript should be converted into
Keyboard Maestro groups with the same title and abbreviation.
All new KM Macro files will be saved to the Desktop.
'''
# Modify this area to customize how the script will run
# Change this path to where ever your TextExander Settings live
HOME = os.path.expanduser('~')
TEXTEXPANDER_PATH = HOME + '/Dropbox/TextExpander/Settings.textexpandersettings'
SAVE_PATH = HOME + '/Desktop/TextExpander_to_KeyboardMaestro'
# Change this if you'd like to change your snippets when importing to Keyboard Maestro
# If your snippet is ttest, you can make it ;;ttest by changing the variable to ';;'
OPTIONAL_NEW_PREFIX = ''
# Change this if you want the snippet to inserted by typing or pasting
# Remember it MUST be 'paste' or 'type' or the script will fail
PASTE_OR_TYPE = 'paste' # 'type'
############
# Edit below at your own risk
############
snippet_types = {
'plaintext': 0,
'applescript': 2,
'shell': 3,
'javascript': 4,
}
snippet_types_to_values = dict((value, key) for key, value in snippet_types.iteritems())
class KeyboardMaestroMacros(object):
@classmethod
def macro_by_name(cls, macro_name, group_name, name, text, abbreviation):
return getattr(cls, macro_name)(group_name, name, text, abbreviation)
@staticmethod
def javascript(group_name, name, text, abbreviation):
return {
'Activate': 'Normal',
'CreationDate': 0.0,
'IsActive': True,
'Macros': [
{'Actions': [
{'DisplayKind': KeyboardMaestroMacros._paste_or_type(),
'IncludeStdErr': True,
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'ExecuteJavaScriptForAutomation',
'Path': '',
'Text': text,
'TimeOutAbortsMacro': True,
'TrimResults': True,
'TrimResultsNew': True,
'UseText': True}, {
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'DeletePastClipboard',
'PastExpression': '0'}
],
'CreationDate': 482018934.65354,
'IsActive': True,
'ModificationDate': 482018953.856014,
'Name': name,
'Triggers': [{
'Case': 'Exact',
'DiacriticalsMatter': True,
'MacroTriggerType': 'TypedString',
'OnlyAfterWordBreak': False,
'SimulateDeletes': True,
'TypedString': KeyboardMaestroMacros._abbreviation(abbreviation)}]}
],
'Name': 'Snippet - %s' % group_name,
}
@staticmethod
def applescript(group_name, name, text, abbreviation):
return {
'Activate': 'Normal',
'CreationDate': 0.0,
'IsActive': True,
'Macros': [
{'Actions': [
{'DisplayKind': KeyboardMaestroMacros._paste_or_type(),
'IncludeStdErr': True,
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'ExecuteAppleScript',
'Path': '',
'Text': text,
'TimeOutAbortsMacro': True,
'TrimResults': True,
'TrimResultsNew': True,
'UseText': True}, {
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'DeletePastClipboard',
'PastExpression': '0'}
],
'CreationDate': 482018934.65354,
'IsActive': True,
'ModificationDate': 482018953.856014,
'Name': name,
'Triggers': [{
'Case': 'Exact',
'DiacriticalsMatter': True,
'MacroTriggerType': 'TypedString',
'OnlyAfterWordBreak': False,
'SimulateDeletes': True,
'TypedString': KeyboardMaestroMacros._abbreviation(abbreviation)}]}
],
'Name': 'Snippet - %s' % group_name,
}
@staticmethod
def plaintext(group_name, name, text, abbreviation):
return {
'Activate': 'Normal',
'CreationDate': 0.0,
'IsActive': True,
'Macros': [{'Actions': [
{
'Action': KeyboardMaestroMacros._paste_or_type('plaintext'),
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'InsertText',
'Paste': True,
'Text': text.replace("%|", "%|%", 1)}, {
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'DeletePastClipboard',
'PastExpression': '0'
}],
'CreationDate': 0.0,
'IsActive': True,
'ModificationDate': 482031702.132113,
'Name': name,
'Triggers': [{
'Case': 'Exact',
'DiacriticalsMatter': True,
'MacroTriggerType': 'TypedString',
'OnlyAfterWordBreak': False,
'SimulateDeletes': True,
'TypedString': KeyboardMaestroMacros._abbreviation(abbreviation)}],
}],
'Name': 'Snippet - %s' % group_name,
}
@staticmethod
def shell(group_name, name, text, abbreviation):
return {
'Activate': 'Normal',
'CreationDate': 0.0,
'IsActive': True,
'Macros': [{
'Actions': [{
'DisplayKind': KeyboardMaestroMacros._paste_or_type(),
'IncludeStdErr': True,
'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'ExecuteShellScript',
'Path': '',
'Text': text,
'TimeOutAbortsMacro': True,
'TrimResults': True,
'TrimResultsNew': True,
'UseText': True},
{'IsActive': True,
'IsDisclosed': True,
'MacroActionType': 'DeletePastClipboard',
'PastExpression': '0'}],
'CreationDate': 482018896.698121,
'IsActive': True,
'ModificationDate': 482020783.300151,
'Name': name,
'Triggers': [{
'Case': 'Exact',
'DiacriticalsMatter': True,
'MacroTriggerType': 'TypedString',
'OnlyAfterWordBreak': False,
'SimulateDeletes': True,
'TypedString': KeyboardMaestroMacros._abbreviation(abbreviation)}],
}],
'Name': 'Snippet - %s' % group_name,
}
@staticmethod
def _abbreviation(name):
return OPTIONAL_NEW_PREFIX + name
@staticmethod
def _paste_or_type(snippet_type=None):
value = {
'paste': "Pasting",
'type': "Typing"
}
if snippet_type == 'plaintext':
return "By%s" % value[PASTE_OR_TYPE]
else:
return value[PASTE_OR_TYPE]
def parse_textexpander():
'''
Each TextExpander group is its own file starting with the file name 'group_'.
Example snippet dictionary
{
'abbreviation': '.bimg',
'abbreviationMode': 0,
'creationDate': datetime.datetime(2013, 5, 19, 19, 42, 16),
'label': '',
'modificationDate': datetime.datetime(2015, 1, 10, 20, 19, 59),
'plainText': 'some text,
'snippetType': 3,
'uuidString': '100F8D1F-A2D1-4313-8B55-EFD504AE7894'
}
Return a list of dictionaries where the keys are the name of the group
'''
to_ret = {}
# Let's get all the xml group files in the directory
xml_files = [f for f in glob.glob(TEXTEXPANDER_PATH + "/*.xml")
if f.startswith(TEXTEXPANDER_PATH + "/group_")]
for xml_file in xml_files:
pl = plistlib.readPlist(xml_file)
if pl['name'] not in to_ret:
to_ret[pl['name']] = []
for snippet in pl['snippetPlists']:
if snippet['snippetType'] in snippet_types.values():
to_ret[pl['name']].append(snippet)
return to_ret
def main():
text_expanders = parse_textexpander()
for group, text_expander in text_expanders.iteritems():
macros_to_create = []
for snippet in text_expander:
macros_to_create.append(
KeyboardMaestroMacros.macro_by_name(snippet_types_to_values[snippet['snippetType']],
group,
snippet['label'],
snippet['plainText'],
snippet['abbreviation'])
)
# Create a new folder on the desktop to put the macros
if not os.path.exists(SAVE_PATH):
os.mkdir(SAVE_PATH)
# Save the macros
with open(SAVE_PATH + '/%s.kmmacros' % group, 'w') as f:
f.write(plistlib.writePlistToString(macros_to_create))
if __name__ == '__main__':
main()