-
Notifications
You must be signed in to change notification settings - Fork 5
/
marsedit_insert.py
executable file
·223 lines (151 loc) · 5.88 KB
/
marsedit_insert.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
## marsedit_insert.py
## --------------
## Inserts HTML text into MarsEdit at the current insertion point that looks
## like it is individual keys. It's designed to represent keyboard shortcuts
## as keys and it's made to be called from a macro program like Quickeys.
## It requires appscript be installed. To install appscript just type
## sudo easy_install appscript at the command line.
##
## For more information on this script see the posts at Clark's Tech Blog
## http://www.libertypages.com/clarktech/?p=5830
import string, sys, time, shutil
from appscript import *
from osax import OSAX
import datetime, re
import optparse
## For logging. Since I typically run this via command scripts activated by
## keystrokes you can't use print statements for debugging. A nice technique
## is to use the logging feature of Python and set it so it is a rotating
# log. I use the location in my script but you can use whatever you want.
import logging
import logging.handlers
LOG_FILE = '/Users/clarkgoble/bin/text/marsedit_insert.log'
my_log = logging.getLogger('theLog')
my_log.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=10000, backupCount=10)
my_log.addHandler(handler)
# Inserts text by saving clipboard content and then using clipboard to
# insert the text. We paste by accessing MarsEdit's menubar.
def insert_text_cb(text):
sa = OSAX()
# save old clipboard (only text part)
oldtext = sa.the_clipboard()
# put our text on clipboard
sa.set_the_clipboard_to(text)
# paste clipboard
ME = app(u'System Events').application_processes[u'MarsEdit']
ME.menu_bars[1].menu_bar_items[4].menus[1].menu_items[6].click()
# restore old clipboard
# need to pause so we don't replace clipboard before it is pasted
time.sleep(.5)
sa.set_the_clipboard_to( oldtext )
# type back arrow presses number of times
def backarrow(presses=1):
for i in range(presses):
app(u'System Events').key_code(123)
# type forward arrow presses number of times
def forwardarrow(presses=1):
for i in range(presses):
app(u'System Events').key_code(124)
# type command - down arrow
def cmddownkey():
app(u'System Events').key_code(125, using=k.command_down)
# type text s as if it were typed on the keyboard
def typestring(s):
for i in range(0,len(s)):
c = s[i]
if c == '\n':
continue
if c in string.ascii_uppercase:
app(u'System Events').keystroke(s[i], using=k.shift_down)
else:
app(u'System Events').keystroke(s[i])
# returns the text of the format menu in the Post menu in MarsEdit
# should be Edit Rich Text or Edit HTML Text depending upon mode
def geteditmode():
app(u'/Applications/MarsEdit.app').activate()
SE = app(u'System Events').application_processes[u'MarsEdit']
menu = SE.menu_bars[1].menu_bar_items[7].menus[1].menu_items[3]
menu_name = menu.name.get()
return menu_name
# put ourselves in HTML editing more in MarsEdit
def sethtmlmode():
app(u'/Applications/MarsEdit.app').activate()
SE = app(u'System Events').application_processes[u'MarsEdit']
menu = SE.menu_bars[1].menu_bar_items[7].menus[1].menu_items[3]
menu_name = menu.name.get()
if "Edit HTML" in menu_name:
# click HTML mode
SE.menu_bars[1].menu_bar_items[7].menus[1].menu_items[3].click()
# skip over </p> tag MarsEdit automatically puts at end since when
# we switch to html mode MarsEdit puts us at the end of the text
# rather than maintaining our edit position
backarrow(4)
# put ourselves in rtf editing more in MarsEdit
def setrtfmode():
app(u'/Applications/MarsEdit.app').activate()
SE = app(u'System Events').application_processes[u'MarsEdit']
menu = SE.menu_bars[1].menu_bar_items[7].menus[1].menu_items[3]
menu_name = menu.name.get()
if "Edit Rich" in menu_name:
# click rtf mode
SE.menu_bars[1].menu_bar_items[7].menus[1].menu_items[3].click()
# go to end
cmddownkey()
# inserts tag to make HTML so that the character prior to the cursor in
# MarsEdit looks like a keyboard key
def inserttags():
# move back before typed character
backarrow()
typestring('<span class="keyboardshortcut">')
forwadarrow()
typestring('</span>')
# inserts character c into text as if it were a keyboard key
def makekey(c = u"X"):
my_log.info("inside makekey")
insert_text_cb('<span class="keyboardshortcut">' + c + '</span>')
# functions for specially named keys
def shiftkey():
makekey( u"\u2318" )
def cmdkey():
makekey( u"\u2318" )
def caplockkey():
makekey( u"\u21EA" )
def ctrlkey():
makekey( u"\u2303")
def deletekey():
makekey( u"\u232B")
def optionkey():
makekey( u"\u2325")
def shiftkey():
makekey( u"\u21E7")
def tabkey():
makekey( u"\u21E5")
# tries to evaluate the keyname in case it is a function otherwise just
# generates html for the text as a key
def htmlkey( keyname ):
try:
my_log.info("Try:"+ keyname )
eval( keyname + "()" )
my_log.info("Success:"+keyname )
except:
my_log.info("Except: " + keyname)
makekey( keyname )
def test():
htmlkey( "tabkey" )
htmlkey( "test" )
if __name__ == '__main__':
option_parser = optparse.OptionParser(usage='%prog keyname1 keyname2')
options, args = option_parser.parse_args()
if len(args) < 1:
test()
# we need to be in MarsEdit's HTML mode
sethtmlmode()
for a in range(len(args)):
htmlkey( args[a] )
# put us back in rtf mode
setrtfmode()
# change to 0 for success, 1 for (partial) failure
sys.exit(0)