-
Notifications
You must be signed in to change notification settings - Fork 5
/
macro.py
125 lines (98 loc) · 3 KB
/
macro.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
"""
macros are not savable, but they are semantic in that functions are recorded,
not keypresses. This means that macros can interact with your computer state
such as by looking at what is in the clipboard, are referencing nearby text.
"""
import time
from talon.voice import Context, talon, Key, Str, press
from talon.engine import engine
from .utils import parse_words_as_integer, optional_numerals, format_phrase_with_dictations
macro = []
last_actions = None
macro_recording = False
last_usage = None
RECORD_PHRASE = 'macro start'
STOP_PHRASE = 'macro stop'
def macro_record(j):
global macro
global last_actions
global macro_recording
global last_usage
if macro_recording:
if j["cmd"] == "p.end" and j["grammar"] == "talon":
new = talon.last_actions
macro.extend(new)
last_actions = new
last_usage = time.time()
def macro_start(m):
global macro
global macro_recording
macro_recording = True
macro = []
def is_recording():
global macro_recording
return macro_recording
def macro_record_phrase():
global RECORD_PHRASE
return RECORD_PHRASE
def macro_stop_phrase():
global STOP_PHRASE
return STOP_PHRASE
def display_macro():
if last_usage == None:
return False
now = time.time()
return now - last_usage < 120
def macro_stop(m):
global macro
global macro_recording
global last_usage
last_usage = time.time()
if macro_recording:
macro = macro[1:]
macro_recording = False
def macro_play(m):
global macro
global last_usage
last_usage = time.time()
macro_stop(None)
override_num = parse_words_as_integer(m._words)
for item in macro:
for action, rule in item:
if override_num != None and 'basic_keys_digits__' in str(rule):
for char in str(override_num):
press(char)
else:
action(rule) or (action, rule)
def macro_print(m):
global macro
global last_usage
last_usage = time.time()
macro_stop(None)
actions = []
for item in macro:
for action, rule in item:
if isinstance(action, Key):
actions.append('press("{}")'.format(action.data))
elif isinstance(action, Str):
actions.append('Str("{}")(None)'.format(action.data))
elif '__basic_keys' in str(rule):
actions.append('press_keys("{}")(None)'.format(format_phrase_with_dictations(rule._words)))
else:
# TODO: other conditions
print(action)
actions.append(str(action))
for action in actions:
Str(action)(None)
press("enter")
engine.register("post:phrase", macro_record)
ctx = Context("macro")
ctx.keymap(
{
RECORD_PHRASE: macro_start,
STOP_PHRASE: macro_stop,
"macro play": macro_play,
"(replay)" + optional_numerals: macro_play,
"macro print": macro_print,
}
)