-
Notifications
You must be signed in to change notification settings - Fork 0
/
macapp.py
284 lines (206 loc) · 6.92 KB
/
macapp.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
275
276
277
278
279
280
281
import ScriptingBridge
import shutil
import time
ShiftDown = 1265854068
CommandDown = 1264807268
OptionDown = 1265594484
ControlDown = 1264809068
LeftArrow = 0x7B
RightArrow = 0x7C
DownArrow = 0x7D
UpArrow = 0x7E
KeyC = 8
def app( indentifier ):
return ScriptingBridge.SBApplication.applicationWithBundleIdentifier_( indentifier )
class Object():
def __init__(__):
__.current_obj = None
def find(__, obj, name ):
#print "finding obj: ", name
for i in obj:
#print i.name()
if i.name() == name:
__.current_obj = i
return True
return False
def get(__):
return __.current_obj
class Key():
def __init__(__):
__.key = app("com.apple.systemevents")
def home(__):
__.key_code( UpArrow, CommandDown )
def newline(__, arg=1 ):
map(__.type_out( '\r' ), [None] * arg )
def up(__, arg=1 ):
map(__.key_code( UpArrow ), [None] * arg )
def down(__, arg=1 ):
map(__.key_code( DownArrow ), [None] * arg )
def page_break(__):
__.type_out( '\r', ControlDown ) # user defined control-Enter for page break.
def type_down(__, keys, arg=1 ):
__.type_out( keys )
map(__.down(), [None] * arg )
def type_out(__, key, option=0 ):
__.key.keystroke_using_( key, option )
def key_code(__, arg, option=0 ):
__.key.keyCode_using_( arg, option )
class Printer():
def __init__(__):
print 'key init'
__.print_app = app("com.apple.print.PrintCenter")
__.currentPrinter = __.print_app.currentPrinter()
__.default = __.currentPrinter
__.printers = __.print_app.printers()
def current(__, name='' ):
if name:
for i in __.printers:
if i.name() == name:
__.app.setCurrentPrinter_(i)
__.currentPrinter = __.print_app.currentPrinter()
return __.currentPrinter
def default(__):
__.app.setCurrentPrinter_( __.default )
class Label( Key ):
def __init__(__, url, lines=1):
Key.__init__(__)
__.pages = app("com.apple.iwork.pages")
__.pages.activate()
shutil.copy2( url + 'participants-%1d.pages' % lines, url + 'participants.pages')
__.document = __.pages.open_( url + 'participants.pages')
class Check( Key ):
def __init__(__, url):
Key.__init__(__)
__.pages = app("com.apple.iwork.pages")
__.pages.activate()
shutil.copy2( url + 'check-one.pages', url + 'check_temp.pages')
__.document = __.pages.open_( url + 'check_temp.pages')
__.pages.activate()
def activate(__):
__.pages.activate()
class Pages( Key, Printer ):
def __init__(__):
Key.__init__(__)
__.app = app("com.apple.iwork.pages")
__.app.activate()
__.documents = __.app.documents().get()
def document__(__, name):
__.document = find(__.documents, name)
p = i.bodyText().paragraphs()[0]
print p.text()[2].get()
i.bodyText().paragraphs()[1].setTo_('This is it\n')
print '=' * 100
def print__(__):
__.app.print_printDialog_withProperties_(i, False, None)
def activate(__):
__.app.activate()
class Cell():
def __init__(__, cell):
__.cell = cell
def value(__, value=None):
if value:
__.cell.setValue_( value )
properties = __.cell.properties()
return properties['value']
def address(__):
return __.cell.properties()['address']
class Column():
def __init__(__, column):
__.column = column
def value(__, value=None):
if value:
__.cell.setValue_( value )
properties = __.cell.properties()
return properties['value']
def column_name( column ):
ascii = ord('A')
digit2 = chr( ascii + column / 26 % 26 - 1) if column > 25 else ''
digit1 = chr( ascii + column % 26 )
return digit2 + digit1
class Numbers( Key ):
def __init__(__):
Key.__init__(__)
__.obj = Object()
__.app = app("com.apple.iwork.numbers")
#__.app.activate()
__.documents = __.app.documents().get()
__.current_document = None
__.current_sheet = None
__.current_table = None
__.current_cell = None
def document(__, name='' ):
if __.obj.find( __.documents, name):
__.current_document = __.obj.get()
__.sheets = __.current_document.sheets().get()
return __
def sheet(__, name):
if __.obj.find( __.sheets, name):
__.current_sheet = __.obj.get()
__.tables = __.current_sheet.tables().get()
return __
def table(__, name):
if __.obj.find(__.tables, name):
__.current_table = __.obj.get()
__.cells = __.current_table.cells().get()
__.rows = __.current_table.rows().get()
__.columns = __.current_table.columns().get()
return __
def row(__, name):
if __.obj.find(__.rows, name):
__.current_row = __.obj.get()
__.cells = __.current_row.cells().get()
return __
def column(__, name):
if type( name ) == int:
name = column_name( name )
print name
if __.obj.find(__.columns, name):
__.current_column = __.obj.get()
__.cells = __.current_column.cells().get()
return __
def cell(__, name):
if __.obj.find(__.cells, name):
__.current_cell = __.obj.get()
__.Cell = Cell( __.current_cell )
return __
def offset(__, row, column):
return __.cell( column_name( column ) + str( row + 1 ) )
def value(__, row, column):
__.offset( row, column )
return __.Cell.value()
class iTunes( Key ):
def __init__(__):
Key.__init__(__)
__.app = app("com.apple.itunes")
__.app.activate()
class Chrome( Key ):
def __init__(__):
Key.__init__(__)
__.app = app("com.google.chrome")
__.app.activate()
__.windows = __.app.windows().get()
__.tabs = __.window[-1].tabs()
__.tab = __.tabs[-1]
def url(__, location ):
__.tab.setURL_( location )
def javascript(__, source ):
__.tab.executeJavascript_( source )
def selectAll(__):
__.tab.selectAll()
def copy(__):
__.tab.copySelection()
def viewSource(__):
__.tab.viewSource()
def dirs( obj ):
print (list(set(dir( obj )) - set(dir( ScriptingBridge.SBObject ))))
if __name__ == '__main__':
n = Numbers()
n.document('Untitled 21').sheet('Sheet 1').table('Table 1')
n.cell('C2')
print n.current_table
dirs( n.current_cell )
n.Cell.value('Hello World')
print n.Cell.value()
# p = Printer()
# print p.current().name()
# print p.current('brother_ql-700@krypton').name()