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