1 | """Utility routines depending on the finder, |
---|
2 | a combination of code by Jack Jansen and erik@letterror.com. |
---|
3 | |
---|
4 | Most events have been captured from |
---|
5 | Lasso Capture AE and than translated to python code. |
---|
6 | |
---|
7 | IMPORTANT |
---|
8 | Note that the processes() function returns different values |
---|
9 | depending on the OS version it is running on. On MacOS 9 |
---|
10 | the Finder returns the process *names* which can then be |
---|
11 | used to find out more about them. On MacOS 8.6 and earlier |
---|
12 | the Finder returns a code which does not seem to work. |
---|
13 | So bottom line: the processes() stuff does not work on < MacOS9 |
---|
14 | |
---|
15 | Mostly written by erik@letterror.com |
---|
16 | """ |
---|
17 | |
---|
18 | from warnings import warnpy3k |
---|
19 | warnpy3k("In 3.x, the findertools module is removed.", stacklevel=2) |
---|
20 | |
---|
21 | import Finder |
---|
22 | from Carbon import AppleEvents |
---|
23 | import aetools |
---|
24 | import MacOS |
---|
25 | import sys |
---|
26 | import Carbon.File |
---|
27 | import Carbon.Folder |
---|
28 | import aetypes |
---|
29 | from types import * |
---|
30 | |
---|
31 | __version__ = '1.1' |
---|
32 | Error = 'findertools.Error' |
---|
33 | |
---|
34 | _finder_talker = None |
---|
35 | |
---|
36 | def _getfinder(): |
---|
37 | """returns basic (recyclable) Finder AE interface object""" |
---|
38 | global _finder_talker |
---|
39 | if not _finder_talker: |
---|
40 | _finder_talker = Finder.Finder() |
---|
41 | _finder_talker.send_flags = ( _finder_talker.send_flags | |
---|
42 | AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer) |
---|
43 | return _finder_talker |
---|
44 | |
---|
45 | def launch(file): |
---|
46 | """Open a file thru the finder. Specify file by name or fsspec""" |
---|
47 | finder = _getfinder() |
---|
48 | fss = Carbon.File.FSSpec(file) |
---|
49 | return finder.open(fss) |
---|
50 | |
---|
51 | def Print(file): |
---|
52 | """Print a file thru the finder. Specify file by name or fsspec""" |
---|
53 | finder = _getfinder() |
---|
54 | fss = Carbon.File.FSSpec(file) |
---|
55 | return finder._print(fss) |
---|
56 | |
---|
57 | def copy(src, dstdir): |
---|
58 | """Copy a file to a folder""" |
---|
59 | finder = _getfinder() |
---|
60 | if type(src) == type([]): |
---|
61 | src_fss = [] |
---|
62 | for s in src: |
---|
63 | src_fss.append(Carbon.File.FSSpec(s)) |
---|
64 | else: |
---|
65 | src_fss = Carbon.File.FSSpec(src) |
---|
66 | dst_fss = Carbon.File.FSSpec(dstdir) |
---|
67 | return finder.duplicate(src_fss, to=dst_fss) |
---|
68 | |
---|
69 | def move(src, dstdir): |
---|
70 | """Move a file to a folder""" |
---|
71 | finder = _getfinder() |
---|
72 | if type(src) == type([]): |
---|
73 | src_fss = [] |
---|
74 | for s in src: |
---|
75 | src_fss.append(Carbon.File.FSSpec(s)) |
---|
76 | else: |
---|
77 | src_fss = Carbon.File.FSSpec(src) |
---|
78 | dst_fss = Carbon.File.FSSpec(dstdir) |
---|
79 | return finder.move(src_fss, to=dst_fss) |
---|
80 | |
---|
81 | def sleep(): |
---|
82 | """Put the mac to sleep""" |
---|
83 | finder = _getfinder() |
---|
84 | finder.sleep() |
---|
85 | |
---|
86 | def shutdown(): |
---|
87 | """Shut the mac down""" |
---|
88 | finder = _getfinder() |
---|
89 | finder.shut_down() |
---|
90 | |
---|
91 | def restart(): |
---|
92 | """Restart the mac""" |
---|
93 | finder = _getfinder() |
---|
94 | finder.restart() |
---|
95 | |
---|
96 | |
---|
97 | #--------------------------------------------------- |
---|
98 | # Additional findertools |
---|
99 | # |
---|
100 | |
---|
101 | def reveal(file): |
---|
102 | """Reveal a file in the finder. Specify file by name, fsref or fsspec.""" |
---|
103 | finder = _getfinder() |
---|
104 | fsr = Carbon.File.FSRef(file) |
---|
105 | file_alias = fsr.FSNewAliasMinimal() |
---|
106 | return finder.reveal(file_alias) |
---|
107 | |
---|
108 | def select(file): |
---|
109 | """select a file in the finder. Specify file by name, fsref or fsspec.""" |
---|
110 | finder = _getfinder() |
---|
111 | fsr = Carbon.File.FSRef(file) |
---|
112 | file_alias = fsr.FSNewAliasMinimal() |
---|
113 | return finder.select(file_alias) |
---|
114 | |
---|
115 | def update(file): |
---|
116 | """Update the display of the specified object(s) to match |
---|
117 | their on-disk representation. Specify file by name, fsref or fsspec.""" |
---|
118 | finder = _getfinder() |
---|
119 | fsr = Carbon.File.FSRef(file) |
---|
120 | file_alias = fsr.FSNewAliasMinimal() |
---|
121 | return finder.update(file_alias) |
---|
122 | |
---|
123 | |
---|
124 | #--------------------------------------------------- |
---|
125 | # More findertools |
---|
126 | # |
---|
127 | |
---|
128 | def comment(object, comment=None): |
---|
129 | """comment: get or set the Finder-comment of the item, displayed in the 'Get Info' window.""" |
---|
130 | object = Carbon.File.FSRef(object) |
---|
131 | object_alias = object.FSNewAliasMinimal() |
---|
132 | if comment is None: |
---|
133 | return _getcomment(object_alias) |
---|
134 | else: |
---|
135 | return _setcomment(object_alias, comment) |
---|
136 | |
---|
137 | def _setcomment(object_alias, comment): |
---|
138 | finder = _getfinder() |
---|
139 | args = {} |
---|
140 | attrs = {} |
---|
141 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None) |
---|
142 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00) |
---|
143 | args['----'] = aeobj_01 |
---|
144 | args["data"] = comment |
---|
145 | _reply, args, attrs = finder.send("core", "setd", args, attrs) |
---|
146 | if 'errn' in args: |
---|
147 | raise Error, aetools.decodeerror(args) |
---|
148 | if '----' in args: |
---|
149 | return args['----'] |
---|
150 | |
---|
151 | def _getcomment(object_alias): |
---|
152 | finder = _getfinder() |
---|
153 | args = {} |
---|
154 | attrs = {} |
---|
155 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None) |
---|
156 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00) |
---|
157 | args['----'] = aeobj_01 |
---|
158 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
159 | if 'errn' in args: |
---|
160 | raise Error, aetools.decodeerror(args) |
---|
161 | if '----' in args: |
---|
162 | return args['----'] |
---|
163 | |
---|
164 | |
---|
165 | #--------------------------------------------------- |
---|
166 | # Get information about current processes in the Finder. |
---|
167 | |
---|
168 | def processes(): |
---|
169 | """processes returns a list of all active processes running on this computer and their creators.""" |
---|
170 | finder = _getfinder() |
---|
171 | args = {} |
---|
172 | attrs = {} |
---|
173 | processnames = [] |
---|
174 | processnumbers = [] |
---|
175 | creators = [] |
---|
176 | partitions = [] |
---|
177 | used = [] |
---|
178 | ## get the processnames or else the processnumbers |
---|
179 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None) |
---|
180 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
---|
181 | if 'errn' in args: |
---|
182 | raise Error, aetools.decodeerror(args) |
---|
183 | p = [] |
---|
184 | if '----' in args: |
---|
185 | p = args['----'] |
---|
186 | for proc in p: |
---|
187 | if hasattr(proc, 'seld'): |
---|
188 | # it has a real name |
---|
189 | processnames.append(proc.seld) |
---|
190 | elif hasattr(proc, 'type'): |
---|
191 | if proc.type == "psn ": |
---|
192 | # it has a process number |
---|
193 | processnumbers.append(proc.data) |
---|
194 | ## get the creators |
---|
195 | args = {} |
---|
196 | attrs = {} |
---|
197 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None) |
---|
198 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fcrt'), fr=aeobj_0) |
---|
199 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
---|
200 | if 'errn' in args: |
---|
201 | raise Error, aetools.decodeerror(_arg) |
---|
202 | if '----' in args: |
---|
203 | p = args['----'] |
---|
204 | creators = p[:] |
---|
205 | ## concatenate in one dict |
---|
206 | result = [] |
---|
207 | if len(processnames) > len(processnumbers): |
---|
208 | data = processnames |
---|
209 | else: |
---|
210 | data = processnumbers |
---|
211 | for i in range(len(creators)): |
---|
212 | result.append((data[i], creators[i])) |
---|
213 | return result |
---|
214 | |
---|
215 | class _process: |
---|
216 | pass |
---|
217 | |
---|
218 | def isactiveprocess(processname): |
---|
219 | """Check of processname is active. MacOS9""" |
---|
220 | all = processes() |
---|
221 | ok = 0 |
---|
222 | for n, c in all: |
---|
223 | if n == processname: |
---|
224 | return 1 |
---|
225 | return 0 |
---|
226 | |
---|
227 | def processinfo(processname): |
---|
228 | """Return an object with all process properties as attributes for processname. MacOS9""" |
---|
229 | p = _process() |
---|
230 | |
---|
231 | if processname == "Finder": |
---|
232 | p.partition = None |
---|
233 | p.used = None |
---|
234 | else: |
---|
235 | p.partition = _processproperty(processname, 'appt') |
---|
236 | p.used = _processproperty(processname, 'pusd') |
---|
237 | p.visible = _processproperty(processname, 'pvis') #Is the process' layer visible? |
---|
238 | p.frontmost = _processproperty(processname, 'pisf') #Is the process the frontmost process? |
---|
239 | p.file = _processproperty(processname, 'file') #the file from which the process was launched |
---|
240 | p.filetype = _processproperty(processname, 'asty') #the OSType of the file type of the process |
---|
241 | p.creatortype = _processproperty(processname, 'fcrt') #the OSType of the creator of the process (the signature) |
---|
242 | p.accepthighlevel = _processproperty(processname, 'revt') #Is the process high-level event aware (accepts open application, open document, print document, and quit)? |
---|
243 | p.hasscripting = _processproperty(processname, 'hscr') #Does the process have a scripting terminology, i.e., can it be scripted? |
---|
244 | return p |
---|
245 | |
---|
246 | def _processproperty(processname, property): |
---|
247 | """return the partition size and memory used for processname""" |
---|
248 | finder = _getfinder() |
---|
249 | args = {} |
---|
250 | attrs = {} |
---|
251 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="name", seld=processname, fr=None) |
---|
252 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type(property), fr=aeobj_00) |
---|
253 | args['----'] = aeobj_01 |
---|
254 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
255 | if 'errn' in args: |
---|
256 | raise Error, aetools.decodeerror(args) |
---|
257 | if '----' in args: |
---|
258 | return args['----'] |
---|
259 | |
---|
260 | |
---|
261 | #--------------------------------------------------- |
---|
262 | # Mess around with Finder windows. |
---|
263 | |
---|
264 | def openwindow(object): |
---|
265 | """Open a Finder window for object, Specify object by name or fsspec.""" |
---|
266 | finder = _getfinder() |
---|
267 | object = Carbon.File.FSRef(object) |
---|
268 | object_alias = object.FSNewAliasMinimal() |
---|
269 | args = {} |
---|
270 | attrs = {} |
---|
271 | _code = 'aevt' |
---|
272 | _subcode = 'odoc' |
---|
273 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
---|
274 | args['----'] = aeobj_0 |
---|
275 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
---|
276 | if 'errn' in args: |
---|
277 | raise Error, aetools.decodeerror(args) |
---|
278 | |
---|
279 | def closewindow(object): |
---|
280 | """Close a Finder window for folder, Specify by path.""" |
---|
281 | finder = _getfinder() |
---|
282 | object = Carbon.File.FSRef(object) |
---|
283 | object_alias = object.FSNewAliasMinimal() |
---|
284 | args = {} |
---|
285 | attrs = {} |
---|
286 | _code = 'core' |
---|
287 | _subcode = 'clos' |
---|
288 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
---|
289 | args['----'] = aeobj_0 |
---|
290 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
---|
291 | if 'errn' in args: |
---|
292 | raise Error, aetools.decodeerror(args) |
---|
293 | |
---|
294 | def location(object, pos=None): |
---|
295 | """Set the position of a Finder window for folder to pos=(w, h). Specify file by name or fsspec. |
---|
296 | If pos=None, location will return the current position of the object.""" |
---|
297 | object = Carbon.File.FSRef(object) |
---|
298 | object_alias = object.FSNewAliasMinimal() |
---|
299 | if not pos: |
---|
300 | return _getlocation(object_alias) |
---|
301 | return _setlocation(object_alias, pos) |
---|
302 | |
---|
303 | def _setlocation(object_alias, (x, y)): |
---|
304 | """_setlocation: Set the location of the icon for the object.""" |
---|
305 | finder = _getfinder() |
---|
306 | args = {} |
---|
307 | attrs = {} |
---|
308 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
---|
309 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00) |
---|
310 | args['----'] = aeobj_01 |
---|
311 | args["data"] = [x, y] |
---|
312 | _reply, args, attrs = finder.send("core", "setd", args, attrs) |
---|
313 | if 'errn' in args: |
---|
314 | raise Error, aetools.decodeerror(args) |
---|
315 | return (x,y) |
---|
316 | |
---|
317 | def _getlocation(object_alias): |
---|
318 | """_getlocation: get the location of the icon for the object.""" |
---|
319 | finder = _getfinder() |
---|
320 | args = {} |
---|
321 | attrs = {} |
---|
322 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
---|
323 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00) |
---|
324 | args['----'] = aeobj_01 |
---|
325 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
326 | if 'errn' in args: |
---|
327 | raise Error, aetools.decodeerror(args) |
---|
328 | if '----' in args: |
---|
329 | pos = args['----'] |
---|
330 | return pos.h, pos.v |
---|
331 | |
---|
332 | def label(object, index=None): |
---|
333 | """label: set or get the label of the item. Specify file by name or fsspec.""" |
---|
334 | object = Carbon.File.FSRef(object) |
---|
335 | object_alias = object.FSNewAliasMinimal() |
---|
336 | if index is None: |
---|
337 | return _getlabel(object_alias) |
---|
338 | if index < 0 or index > 7: |
---|
339 | index = 0 |
---|
340 | return _setlabel(object_alias, index) |
---|
341 | |
---|
342 | def _getlabel(object_alias): |
---|
343 | """label: Get the label for the object.""" |
---|
344 | finder = _getfinder() |
---|
345 | args = {} |
---|
346 | attrs = {} |
---|
347 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None) |
---|
348 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('labi'), fr=aeobj_00) |
---|
349 | args['----'] = aeobj_01 |
---|
350 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
351 | if 'errn' in args: |
---|
352 | raise Error, aetools.decodeerror(args) |
---|
353 | if '----' in args: |
---|
354 | return args['----'] |
---|
355 | |
---|
356 | def _setlabel(object_alias, index): |
---|
357 | """label: Set the label for the object.""" |
---|
358 | finder = _getfinder() |
---|
359 | args = {} |
---|
360 | attrs = {} |
---|
361 | _code = 'core' |
---|
362 | _subcode = 'setd' |
---|
363 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
364 | form="alis", seld=object_alias, fr=None) |
---|
365 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
366 | form="prop", seld=aetypes.Type('labi'), fr=aeobj_0) |
---|
367 | args['----'] = aeobj_1 |
---|
368 | args["data"] = index |
---|
369 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
---|
370 | if 'errn' in args: |
---|
371 | raise Error, aetools.decodeerror(args) |
---|
372 | return index |
---|
373 | |
---|
374 | def windowview(folder, view=None): |
---|
375 | """windowview: Set the view of the window for the folder. Specify file by name or fsspec. |
---|
376 | 0 = by icon (default) |
---|
377 | 1 = by name |
---|
378 | 2 = by button |
---|
379 | """ |
---|
380 | fsr = Carbon.File.FSRef(folder) |
---|
381 | folder_alias = fsr.FSNewAliasMinimal() |
---|
382 | if view is None: |
---|
383 | return _getwindowview(folder_alias) |
---|
384 | return _setwindowview(folder_alias, view) |
---|
385 | |
---|
386 | def _setwindowview(folder_alias, view=0): |
---|
387 | """set the windowview""" |
---|
388 | attrs = {} |
---|
389 | args = {} |
---|
390 | if view == 1: |
---|
391 | _v = aetypes.Type('pnam') |
---|
392 | elif view == 2: |
---|
393 | _v = aetypes.Type('lgbu') |
---|
394 | else: |
---|
395 | _v = aetypes.Type('iimg') |
---|
396 | finder = _getfinder() |
---|
397 | aeobj_0 = aetypes.ObjectSpecifier(want = aetypes.Type('cfol'), |
---|
398 | form = 'alis', seld = folder_alias, fr=None) |
---|
399 | aeobj_1 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
---|
400 | form = 'prop', seld = aetypes.Type('cwnd'), fr=aeobj_0) |
---|
401 | aeobj_2 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
---|
402 | form = 'prop', seld = aetypes.Type('pvew'), fr=aeobj_1) |
---|
403 | aeobj_3 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
---|
404 | form = 'prop', seld = _v, fr=None) |
---|
405 | _code = 'core' |
---|
406 | _subcode = 'setd' |
---|
407 | args['----'] = aeobj_2 |
---|
408 | args['data'] = aeobj_3 |
---|
409 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
---|
410 | if 'errn' in args: |
---|
411 | raise Error, aetools.decodeerror(args) |
---|
412 | if '----' in args: |
---|
413 | return args['----'] |
---|
414 | |
---|
415 | def _getwindowview(folder_alias): |
---|
416 | """get the windowview""" |
---|
417 | attrs = {} |
---|
418 | args = {} |
---|
419 | finder = _getfinder() |
---|
420 | args = {} |
---|
421 | attrs = {} |
---|
422 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=folder_alias, fr=None) |
---|
423 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_00) |
---|
424 | aeobj_02 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('pvew'), fr=aeobj_01) |
---|
425 | args['----'] = aeobj_02 |
---|
426 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
427 | if 'errn' in args: |
---|
428 | raise Error, aetools.decodeerror(args) |
---|
429 | views = {'iimg':0, 'pnam':1, 'lgbu':2} |
---|
430 | if '----' in args: |
---|
431 | return views[args['----'].enum] |
---|
432 | |
---|
433 | def windowsize(folder, size=None): |
---|
434 | """Set the size of a Finder window for folder to size=(w, h), Specify by path. |
---|
435 | If size=None, windowsize will return the current size of the window. |
---|
436 | Specify file by name or fsspec. |
---|
437 | """ |
---|
438 | fsr = Carbon.File.FSRef(folder) |
---|
439 | folder_alias = fsr.FSNewAliasMinimal() |
---|
440 | openwindow(fsr) |
---|
441 | if not size: |
---|
442 | return _getwindowsize(folder_alias) |
---|
443 | return _setwindowsize(folder_alias, size) |
---|
444 | |
---|
445 | def _setwindowsize(folder_alias, (w, h)): |
---|
446 | """Set the size of a Finder window for folder to (w, h)""" |
---|
447 | finder = _getfinder() |
---|
448 | args = {} |
---|
449 | attrs = {} |
---|
450 | _code = 'core' |
---|
451 | _subcode = 'setd' |
---|
452 | aevar00 = [w, h] |
---|
453 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
---|
454 | form="alis", seld=folder_alias, fr=None) |
---|
455 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
456 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
---|
457 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
458 | form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1) |
---|
459 | args['----'] = aeobj_2 |
---|
460 | args["data"] = aevar00 |
---|
461 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
---|
462 | if 'errn' in args: |
---|
463 | raise Error, aetools.decodeerror(args) |
---|
464 | return (w, h) |
---|
465 | |
---|
466 | def _getwindowsize(folder_alias): |
---|
467 | """Set the size of a Finder window for folder to (w, h)""" |
---|
468 | finder = _getfinder() |
---|
469 | args = {} |
---|
470 | attrs = {} |
---|
471 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
---|
472 | form="alis", seld=folder_alias, fr=None) |
---|
473 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
474 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
---|
475 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
476 | form="prop", seld=aetypes.Type('posn'), fr=aeobj_1) |
---|
477 | args['----'] = aeobj_2 |
---|
478 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
---|
479 | if 'errn' in args: |
---|
480 | raise Error, aetools.decodeerror(args) |
---|
481 | if '----' in args: |
---|
482 | return args['----'] |
---|
483 | |
---|
484 | def windowposition(folder, pos=None): |
---|
485 | """Set the position of a Finder window for folder to pos=(w, h).""" |
---|
486 | fsr = Carbon.File.FSRef(folder) |
---|
487 | folder_alias = fsr.FSNewAliasMinimal() |
---|
488 | openwindow(fsr) |
---|
489 | if not pos: |
---|
490 | return _getwindowposition(folder_alias) |
---|
491 | if type(pos) == InstanceType: |
---|
492 | # pos might be a QDPoint object as returned by _getwindowposition |
---|
493 | pos = (pos.h, pos.v) |
---|
494 | return _setwindowposition(folder_alias, pos) |
---|
495 | |
---|
496 | def _setwindowposition(folder_alias, (x, y)): |
---|
497 | """Set the size of a Finder window for folder to (w, h).""" |
---|
498 | finder = _getfinder() |
---|
499 | args = {} |
---|
500 | attrs = {} |
---|
501 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
---|
502 | form="alis", seld=folder_alias, fr=None) |
---|
503 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
504 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
---|
505 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
506 | form="prop", seld=aetypes.Type('posn'), fr=aeobj_1) |
---|
507 | args['----'] = aeobj_2 |
---|
508 | args["data"] = [x, y] |
---|
509 | _reply, args, attrs = finder.send('core', 'setd', args, attrs) |
---|
510 | if 'errn' in args: |
---|
511 | raise Error, aetools.decodeerror(args) |
---|
512 | if '----' in args: |
---|
513 | return args['----'] |
---|
514 | |
---|
515 | def _getwindowposition(folder_alias): |
---|
516 | """Get the size of a Finder window for folder, Specify by path.""" |
---|
517 | finder = _getfinder() |
---|
518 | args = {} |
---|
519 | attrs = {} |
---|
520 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
---|
521 | form="alis", seld=folder_alias, fr=None) |
---|
522 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
523 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
---|
524 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
525 | form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1) |
---|
526 | args['----'] = aeobj_2 |
---|
527 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
---|
528 | if 'errn' in args: |
---|
529 | raise Error, aetools.decodeerror(args) |
---|
530 | if '----' in args: |
---|
531 | return args['----'] |
---|
532 | |
---|
533 | def icon(object, icondata=None): |
---|
534 | """icon sets the icon of object, if no icondata is given, |
---|
535 | icon will return an AE object with binary data for the current icon. |
---|
536 | If left untouched, this data can be used to paste the icon on another file. |
---|
537 | Development opportunity: get and set the data as PICT.""" |
---|
538 | fsr = Carbon.File.FSRef(object) |
---|
539 | object_alias = fsr.FSNewAliasMinimal() |
---|
540 | if icondata is None: |
---|
541 | return _geticon(object_alias) |
---|
542 | return _seticon(object_alias, icondata) |
---|
543 | |
---|
544 | def _geticon(object_alias): |
---|
545 | """get the icondata for object. Binary data of some sort.""" |
---|
546 | finder = _getfinder() |
---|
547 | args = {} |
---|
548 | attrs = {} |
---|
549 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), |
---|
550 | form="alis", seld=object_alias, fr=None) |
---|
551 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
552 | form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00) |
---|
553 | args['----'] = aeobj_01 |
---|
554 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
555 | if 'errn' in args: |
---|
556 | raise Error, aetools.decodeerror(args) |
---|
557 | if '----' in args: |
---|
558 | return args['----'] |
---|
559 | |
---|
560 | def _seticon(object_alias, icondata): |
---|
561 | """set the icondata for object, formatted as produced by _geticon()""" |
---|
562 | finder = _getfinder() |
---|
563 | args = {} |
---|
564 | attrs = {} |
---|
565 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), |
---|
566 | form="alis", seld=object_alias, fr=None) |
---|
567 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
---|
568 | form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00) |
---|
569 | args['----'] = aeobj_01 |
---|
570 | args["data"] = icondata |
---|
571 | _reply, args, attrs = finder.send("core", "setd", args, attrs) |
---|
572 | if 'errn' in args: |
---|
573 | raise Error, aetools.decodeerror(args) |
---|
574 | if '----' in args: |
---|
575 | return args['----'].data |
---|
576 | |
---|
577 | |
---|
578 | #--------------------------------------------------- |
---|
579 | # Volumes and servers. |
---|
580 | |
---|
581 | def mountvolume(volume, server=None, username=None, password=None): |
---|
582 | """mount a volume, local or on a server on AppleTalk. |
---|
583 | Note: mounting a ASIP server requires a different operation. |
---|
584 | server is the name of the server where the volume belongs |
---|
585 | username, password belong to a registered user of the volume.""" |
---|
586 | finder = _getfinder() |
---|
587 | args = {} |
---|
588 | attrs = {} |
---|
589 | if password: |
---|
590 | args["PASS"] = password |
---|
591 | if username: |
---|
592 | args["USER"] = username |
---|
593 | if server: |
---|
594 | args["SRVR"] = server |
---|
595 | args['----'] = volume |
---|
596 | _reply, args, attrs = finder.send("aevt", "mvol", args, attrs) |
---|
597 | if 'errn' in args: |
---|
598 | raise Error, aetools.decodeerror(args) |
---|
599 | if '----' in args: |
---|
600 | return args['----'] |
---|
601 | |
---|
602 | def unmountvolume(volume): |
---|
603 | """unmount a volume that's on the desktop""" |
---|
604 | putaway(volume) |
---|
605 | |
---|
606 | def putaway(object): |
---|
607 | """puth the object away, whereever it came from.""" |
---|
608 | finder = _getfinder() |
---|
609 | args = {} |
---|
610 | attrs = {} |
---|
611 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('cdis'), form="name", seld=object, fr=None) |
---|
612 | _reply, args, attrs = talker.send("fndr", "ptwy", args, attrs) |
---|
613 | if 'errn' in args: |
---|
614 | raise Error, aetools.decodeerror(args) |
---|
615 | if '----' in args: |
---|
616 | return args['----'] |
---|
617 | |
---|
618 | |
---|
619 | #--------------------------------------------------- |
---|
620 | # Miscellaneous functions |
---|
621 | # |
---|
622 | |
---|
623 | def volumelevel(level): |
---|
624 | """set the audio output level, parameter between 0 (silent) and 7 (full blast)""" |
---|
625 | finder = _getfinder() |
---|
626 | args = {} |
---|
627 | attrs = {} |
---|
628 | if level < 0: |
---|
629 | level = 0 |
---|
630 | elif level > 7: |
---|
631 | level = 7 |
---|
632 | args['----'] = level |
---|
633 | _reply, args, attrs = finder.send("aevt", "stvl", args, attrs) |
---|
634 | if 'errn' in args: |
---|
635 | raise Error, aetools.decodeerror(args) |
---|
636 | if '----' in args: |
---|
637 | return args['----'] |
---|
638 | |
---|
639 | def OSversion(): |
---|
640 | """return the version of the system software""" |
---|
641 | finder = _getfinder() |
---|
642 | args = {} |
---|
643 | attrs = {} |
---|
644 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('ver2'), fr=None) |
---|
645 | args['----'] = aeobj_00 |
---|
646 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
647 | if 'errn' in args: |
---|
648 | raise Error, aetools.decodeerror(args) |
---|
649 | if '----' in args: |
---|
650 | return args['----'] |
---|
651 | |
---|
652 | def filesharing(): |
---|
653 | """return the current status of filesharing and whether it is starting up or not: |
---|
654 | -1 file sharing is off and not starting up |
---|
655 | 0 file sharing is off and starting up |
---|
656 | 1 file sharing is on""" |
---|
657 | status = -1 |
---|
658 | finder = _getfinder() |
---|
659 | # see if it is on |
---|
660 | args = {} |
---|
661 | attrs = {} |
---|
662 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fshr'), fr=None) |
---|
663 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
664 | if 'errn' in args: |
---|
665 | raise Error, aetools.decodeerror(args) |
---|
666 | if '----' in args: |
---|
667 | if args['----'] == 0: |
---|
668 | status = -1 |
---|
669 | else: |
---|
670 | status = 1 |
---|
671 | # is it starting up perchance? |
---|
672 | args = {} |
---|
673 | attrs = {} |
---|
674 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fsup'), fr=None) |
---|
675 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
---|
676 | if 'errn' in args: |
---|
677 | raise Error, aetools.decodeerror(args) |
---|
678 | if '----' in args: |
---|
679 | if args['----'] == 1: |
---|
680 | status = 0 |
---|
681 | return status |
---|
682 | |
---|
683 | def movetotrash(path): |
---|
684 | """move the object to the trash""" |
---|
685 | fss = Carbon.File.FSSpec(path) |
---|
686 | trashfolder = Carbon.Folder.FSFindFolder(fss.as_tuple()[0], 'trsh', 0) |
---|
687 | move(path, trashfolder) |
---|
688 | |
---|
689 | def emptytrash(): |
---|
690 | """empty the trash""" |
---|
691 | finder = _getfinder() |
---|
692 | args = {} |
---|
693 | attrs = {} |
---|
694 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('trsh'), fr=None) |
---|
695 | _reply, args, attrs = finder.send("fndr", "empt", args, attrs) |
---|
696 | if 'errn' in args: |
---|
697 | raise aetools.Error, aetools.decodeerror(args) |
---|
698 | |
---|
699 | |
---|
700 | def _test(): |
---|
701 | import EasyDialogs |
---|
702 | print 'Original findertools functionality test...' |
---|
703 | print 'Testing launch...' |
---|
704 | pathname = EasyDialogs.AskFileForOpen('File to launch:') |
---|
705 | if pathname: |
---|
706 | result = launch(pathname) |
---|
707 | if result: |
---|
708 | print 'Result: ', result |
---|
709 | print 'Press return-', |
---|
710 | sys.stdin.readline() |
---|
711 | print 'Testing print...' |
---|
712 | pathname = EasyDialogs.AskFileForOpen('File to print:') |
---|
713 | if pathname: |
---|
714 | result = Print(pathname) |
---|
715 | if result: |
---|
716 | print 'Result: ', result |
---|
717 | print 'Press return-', |
---|
718 | sys.stdin.readline() |
---|
719 | print 'Testing copy...' |
---|
720 | pathname = EasyDialogs.AskFileForOpen('File to copy:') |
---|
721 | if pathname: |
---|
722 | destdir = EasyDialogs.AskFolder('Destination:') |
---|
723 | if destdir: |
---|
724 | result = copy(pathname, destdir) |
---|
725 | if result: |
---|
726 | print 'Result:', result |
---|
727 | print 'Press return-', |
---|
728 | sys.stdin.readline() |
---|
729 | print 'Testing move...' |
---|
730 | pathname = EasyDialogs.AskFileForOpen('File to move:') |
---|
731 | if pathname: |
---|
732 | destdir = EasyDialogs.AskFolder('Destination:') |
---|
733 | if destdir: |
---|
734 | result = move(pathname, destdir) |
---|
735 | if result: |
---|
736 | print 'Result:', result |
---|
737 | print 'Press return-', |
---|
738 | sys.stdin.readline() |
---|
739 | print 'Testing sleep...' |
---|
740 | if EasyDialogs.AskYesNoCancel('Sleep?') > 0: |
---|
741 | result = sleep() |
---|
742 | if result: |
---|
743 | print 'Result:', result |
---|
744 | print 'Press return-', |
---|
745 | sys.stdin.readline() |
---|
746 | print 'Testing shutdown...' |
---|
747 | if EasyDialogs.AskYesNoCancel('Shut down?') > 0: |
---|
748 | result = shutdown() |
---|
749 | if result: |
---|
750 | print 'Result:', result |
---|
751 | print 'Press return-', |
---|
752 | sys.stdin.readline() |
---|
753 | print 'Testing restart...' |
---|
754 | if EasyDialogs.AskYesNoCancel('Restart?') > 0: |
---|
755 | result = restart() |
---|
756 | if result: |
---|
757 | print 'Result:', result |
---|
758 | print 'Press return-', |
---|
759 | sys.stdin.readline() |
---|
760 | |
---|
761 | def _test2(): |
---|
762 | print '\nmorefindertools version %s\nTests coming up...' %__version__ |
---|
763 | import os |
---|
764 | import random |
---|
765 | |
---|
766 | # miscellaneous |
---|
767 | print '\tfilesharing on?', filesharing() # is file sharing on, off, starting up? |
---|
768 | print '\tOS version', OSversion() # the version of the system software |
---|
769 | |
---|
770 | # set the soundvolume in a simple way |
---|
771 | print '\tSystem beep volume' |
---|
772 | for i in range(0, 7): |
---|
773 | volumelevel(i) |
---|
774 | MacOS.SysBeep() |
---|
775 | |
---|
776 | # Finder's windows, file location, file attributes |
---|
777 | open("@findertoolstest", "w") |
---|
778 | f = "@findertoolstest" |
---|
779 | reveal(f) # reveal this file in a Finder window |
---|
780 | select(f) # select this file |
---|
781 | |
---|
782 | base, file = os.path.split(f) |
---|
783 | closewindow(base) # close the window this file is in (opened by reveal) |
---|
784 | openwindow(base) # open it again |
---|
785 | windowview(base, 1) # set the view by list |
---|
786 | |
---|
787 | label(f, 2) # set the label of this file to something orange |
---|
788 | print '\tlabel', label(f) # get the label of this file |
---|
789 | |
---|
790 | # the file location only works in a window with icon view! |
---|
791 | print 'Random locations for an icon' |
---|
792 | windowview(base, 0) # set the view by icon |
---|
793 | windowsize(base, (600, 600)) |
---|
794 | for i in range(50): |
---|
795 | location(f, (random.randint(10, 590), random.randint(10, 590))) |
---|
796 | |
---|
797 | windowsize(base, (200, 400)) |
---|
798 | windowview(base, 1) # set the view by icon |
---|
799 | |
---|
800 | orgpos = windowposition(base) |
---|
801 | print 'Animated window location' |
---|
802 | for i in range(10): |
---|
803 | pos = (100+i*10, 100+i*10) |
---|
804 | windowposition(base, pos) |
---|
805 | print '\twindow position', pos |
---|
806 | windowposition(base, orgpos) # park it where it was before |
---|
807 | |
---|
808 | print 'Put a comment in file', f, ':' |
---|
809 | print '\t', comment(f) # print the Finder comment this file has |
---|
810 | s = 'This is a comment no one reads!' |
---|
811 | comment(f, s) # set the Finder comment |
---|
812 | |
---|
813 | def _test3(): |
---|
814 | print 'MacOS9 or better specific functions' |
---|
815 | # processes |
---|
816 | pr = processes() # return a list of tuples with (active_processname, creatorcode) |
---|
817 | print 'Return a list of current active processes:' |
---|
818 | for p in pr: |
---|
819 | print '\t', p |
---|
820 | |
---|
821 | # get attributes of the first process in the list |
---|
822 | print 'Attributes of the first process in the list:' |
---|
823 | pinfo = processinfo(pr[0][0]) |
---|
824 | print '\t', pr[0][0] |
---|
825 | print '\t\tmemory partition', pinfo.partition # the memory allocated to this process |
---|
826 | print '\t\tmemory used', pinfo.used # the memory actuall used by this process |
---|
827 | print '\t\tis visible', pinfo.visible # is the process visible to the user |
---|
828 | print '\t\tis frontmost', pinfo.frontmost # is the process the front most one? |
---|
829 | print '\t\thas scripting', pinfo.hasscripting # is the process scriptable? |
---|
830 | print '\t\taccepts high level events', pinfo.accepthighlevel # does the process accept high level appleevents? |
---|
831 | |
---|
832 | if __name__ == '__main__': |
---|
833 | _test() |
---|
834 | _test2() |
---|
835 | _test3() |
---|