blob: 5add05f1f438f03516de1ac854a6c1d6eb76204b [file] [log] [blame]
Jack Jansen43935122001-04-07 12:53:45 +00001"""Utility routines depending on the finder,
2a combination of code by Jack Jansen and erik@letterror.com.
Jack Jansen0585d411996-09-20 15:26:20 +00003
Jack Jansen43935122001-04-07 12:53:45 +00004Most events have been captured from
5Lasso Capture AE and than translated to python code.
6
7IMPORTANT
8Note that the processes() function returns different values
9depending on the OS version it is running on. On MacOS 9
10the Finder returns the process *names* which can then be
11used to find out more about them. On MacOS 8.6 and earlier
12the Finder returns a code which does not seem to work.
13So bottom line: the processes() stuff does not work on < MacOS9
14
15Mostly written by erik@letterror.com
16"""
Jack Jansen8bcd4712000-08-20 19:56:13 +000017import Finder
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000018from Carbon import AppleEvents
Jack Jansen0585d411996-09-20 15:26:20 +000019import aetools
20import MacOS
21import sys
Jack Jansen8cb1ff52003-02-21 23:14:30 +000022import Carbon.File
23import Carbon.Folder
Jack Jansen43935122001-04-07 12:53:45 +000024import aetypes
25from types import *
26
27__version__ = '1.1'
28Error = 'findertools.Error'
Jack Jansen0585d411996-09-20 15:26:20 +000029
Jack Jansen0585d411996-09-20 15:26:20 +000030_finder_talker = None
31
32def _getfinder():
Jack Jansen43935122001-04-07 12:53:45 +000033 """returns basic (recyclable) Finder AE interface object"""
Jack Jansen0585d411996-09-20 15:26:20 +000034 global _finder_talker
35 if not _finder_talker:
Jack Jansen8bcd4712000-08-20 19:56:13 +000036 _finder_talker = Finder.Finder()
Jack Jansen0e0d3e71998-10-15 14:02:54 +000037 _finder_talker.send_flags = ( _finder_talker.send_flags |
38 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
Jack Jansen0585d411996-09-20 15:26:20 +000039 return _finder_talker
40
41def launch(file):
42 """Open a file thru the finder. Specify file by name or fsspec"""
43 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +000044 fss = Carbon.File.FSSpec(file)
Jack Jansen8bcd4712000-08-20 19:56:13 +000045 return finder.open(fss)
Jack Jansen0585d411996-09-20 15:26:20 +000046
47def Print(file):
48 """Print a file thru the finder. Specify file by name or fsspec"""
49 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +000050 fss = Carbon.File.FSSpec(file)
Jack Jansen8bcd4712000-08-20 19:56:13 +000051 return finder._print(fss)
Jack Jansen0585d411996-09-20 15:26:20 +000052
53def copy(src, dstdir):
54 """Copy a file to a folder"""
55 finder = _getfinder()
Jack Jansenfdd22692000-09-22 12:17:14 +000056 if type(src) == type([]):
57 src_fss = []
58 for s in src:
Jack Jansen8cb1ff52003-02-21 23:14:30 +000059 src_fss.append(Carbon.File.FSSpec(s))
Jack Jansenfdd22692000-09-22 12:17:14 +000060 else:
Jack Jansen8cb1ff52003-02-21 23:14:30 +000061 src_fss = Carbon.File.FSSpec(src)
62 dst_fss = Carbon.File.FSSpec(dstdir)
Jack Jansen8bcd4712000-08-20 19:56:13 +000063 return finder.duplicate(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000064
65def move(src, dstdir):
66 """Move a file to a folder"""
67 finder = _getfinder()
Jack Jansenfdd22692000-09-22 12:17:14 +000068 if type(src) == type([]):
69 src_fss = []
70 for s in src:
Jack Jansen8cb1ff52003-02-21 23:14:30 +000071 src_fss.append(Carbon.File.FSSpec(s))
Jack Jansenfdd22692000-09-22 12:17:14 +000072 else:
Jack Jansen8cb1ff52003-02-21 23:14:30 +000073 src_fss = Carbon.File.FSSpec(src)
74 dst_fss = Carbon.File.FSSpec(dstdir)
Jack Jansen8bcd4712000-08-20 19:56:13 +000075 return finder.move(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000076
77def sleep():
78 """Put the mac to sleep"""
79 finder = _getfinder()
80 finder.sleep()
81
82def shutdown():
83 """Shut the mac down"""
84 finder = _getfinder()
85 finder.shut_down()
86
87def restart():
88 """Restart the mac"""
89 finder = _getfinder()
90 finder.restart()
Jack Jansen0585d411996-09-20 15:26:20 +000091
Jack Jansen43935122001-04-07 12:53:45 +000092
93#---------------------------------------------------
94# Additional findertools
95#
96
97def reveal(file):
Jack Jansen8cb1ff52003-02-21 23:14:30 +000098 """Reveal a file in the finder. Specify file by name, fsref or fsspec."""
Jack Jansen43935122001-04-07 12:53:45 +000099 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000100 fsr = Carbon.File.FSRef(file)
101 file_alias = fsr.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000102 return finder.reveal(file_alias)
103
104def select(file):
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000105 """select a file in the finder. Specify file by name, fsref or fsspec."""
Jack Jansen43935122001-04-07 12:53:45 +0000106 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000107 fsr = Carbon.File.FSRef(file)
108 file_alias = fsr.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000109 return finder.select(file_alias)
110
111def update(file):
112 """Update the display of the specified object(s) to match
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000113 their on-disk representation. Specify file by name, fsref or fsspec."""
Jack Jansen43935122001-04-07 12:53:45 +0000114 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000115 fsr = Carbon.File.FSRef(file)
116 file_alias = fsr.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000117 return finder.update(file_alias)
118
119
120#---------------------------------------------------
121# More findertools
122#
123
124def comment(object, comment=None):
Jack Jansencb100d12003-01-21 15:30:21 +0000125 """comment: get or set the Finder-comment of the item, displayed in the 'Get Info' window."""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000126 object = Carbon.File.FSRef(object)
127 object_alias = object.FSNewAliasMonimal()
Jack Jansen43935122001-04-07 12:53:45 +0000128 if comment == None:
129 return _getcomment(object_alias)
130 else:
131 return _setcomment(object_alias, comment)
132
133def _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['----']
146
147def _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['----']
159
160
161#---------------------------------------------------
162# Get information about current processes in the Finder.
163
164def 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
210
211class _process:
212 pass
213
214def 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
222
223def processinfo(processname):
224 """Return an object with all process properties as attributes for processname. MacOS9"""
225 p = _process()
226
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
241
242def _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['----']
255
256
257#---------------------------------------------------
258# Mess around with Finder windows.
259
260def openwindow(object):
261 """Open a Finder window for object, Specify object by name or fsspec."""
262 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000263 object = Carbon.File.FSRef(object)
264 object_alias = object.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000265 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)
274
275def closewindow(object):
276 """Close a Finder window for folder, Specify by path."""
277 finder = _getfinder()
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000278 object = Carbon.File.FSRef(object)
279 object_alias = object.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000280 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)
289
290def 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."""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000293 object = Carbon.File.FSRef(object)
294 object_alias = object.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000295 if not pos:
296 return _getlocation(object_alias)
297 return _setlocation(object_alias, pos)
298
299def _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)
312
313def _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
327
328def label(object, index=None):
329 """label: set or get the label of the item. Specify file by name or fsspec."""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000330 object = Carbon.File.FSRef(object)
331 object_alias = object.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000332 if index == None:
333 return _getlabel(object_alias)
334 if index < 0 or index > 7:
335 index = 0
336 return _setlabel(object_alias, index)
337
338def _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['----']
351
352def _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
369
370def 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
375 """
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000376 fsr = Carbon.File.FSRef(folder)
377 folder_alias = fsr.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000378 if view == None:
379 return _getwindowview(folder_alias)
380 return _setwindowview(folder_alias, view)
381
382def _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['----']
410
411def _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]
428
429def 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.
433 """
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000434 fsr = Carbon.File.FSRef(folder)
435 folder_alias = fsr.FSNewAliasMinimal()
436 openwindow(fsr)
Jack Jansen43935122001-04-07 12:53:45 +0000437 if not size:
438 return _getwindowsize(folder_alias)
439 return _setwindowsize(folder_alias, size)
440
441def _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)
461
462def _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['----']
479
480def windowposition(folder, pos=None):
481 """Set the position of a Finder window for folder to pos=(w, h)."""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000482 fsr = Carbon.File.FSRef(folder)
483 folder_alias = fsr.FSNewAliasMinimal()
484 openwindow(fsr)
Jack Jansen43935122001-04-07 12:53:45 +0000485 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)
491
492def _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['----']
510
511def _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['----']
528
529def 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."""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000534 fsr = Carbon.File.FSRef(object)
535 object_alias = fsr.FSNewAliasMinimal()
Jack Jansen43935122001-04-07 12:53:45 +0000536 if icondata == None:
537 return _geticon(object_alias)
538 return _seticon(object_alias, icondata)
539
540def _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['----']
555
556def _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
572
573
574#---------------------------------------------------
575# Volumes and servers.
576
577def 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['----']
597
598def unmountvolume(volume):
599 """unmount a volume that's on the desktop"""
600 putaway(volume)
601
602def 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['----']
613
614
615#---------------------------------------------------
616# Miscellaneous functions
617#
618
619def 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['----']
634
635def 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['----']
647
648def 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
678
679def movetotrash(path):
680 """move the object to the trash"""
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000681 fss = Carbon.File.FSSpec(path)
682 trashfolder = Carbon.Folder.FSFindFolder(fss.as_tuple()[0], 'trsh', 0)
Jack Jansen5bb6ff92001-07-24 11:37:23 +0000683 move(path, trashfolder)
Jack Jansen43935122001-04-07 12:53:45 +0000684
685def 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)
694
695
696def _test():
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000697 import EasyDialogs
Jack Jansen43935122001-04-07 12:53:45 +0000698 print 'Original findertools functionality test...'
Jack Jansen0585d411996-09-20 15:26:20 +0000699 print 'Testing launch...'
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000700 pathname = EasyDialogs.AskFileForOpen('File to launch:')
701 if pathname:
702 result = launch(pathname)
Jack Jansen0585d411996-09-20 15:26:20 +0000703 if result:
704 print 'Result: ', result
705 print 'Press return-',
706 sys.stdin.readline()
707 print 'Testing print...'
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000708 pathname = EasyDialogs.AskFileForOpen('File to print:')
709 if pathname:
710 result = Print(pathname)
Jack Jansen0585d411996-09-20 15:26:20 +0000711 if result:
712 print 'Result: ', result
713 print 'Press return-',
714 sys.stdin.readline()
715 print 'Testing copy...'
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000716 pathname = EasyDialogs.AskFileForOpen('File to copy:')
717 if pathname:
718 destdir = EasyDialogs.AskFolder('Destination:')
719 if destdir:
720 result = copy(pathname, destdir)
Jack Jansen0585d411996-09-20 15:26:20 +0000721 if result:
722 print 'Result:', result
723 print 'Press return-',
724 sys.stdin.readline()
725 print 'Testing move...'
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000726 pathname = EasyDialogs.AskFileForOpen('File to move:')
727 if pathname:
728 destdir = EasyDialogs.AskFolder('Destination:')
729 if destdir:
730 result = move(pathname, destdir)
Jack Jansen0585d411996-09-20 15:26:20 +0000731 if result:
732 print 'Result:', result
733 print 'Press return-',
734 sys.stdin.readline()
Jack Jansen0585d411996-09-20 15:26:20 +0000735 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()
Jack Jansen43935122001-04-07 12:53:45 +0000756
757def _test2():
Jack Jansencb100d12003-01-21 15:30:21 +0000758 print '\nmorefindertools version %s\nTests coming up...' %__version__
Jack Jansen43935122001-04-07 12:53:45 +0000759 import os
760 import random
761
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
765
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()
771
772 # Finder's windows, file location, file attributes
773 open("@findertoolstest", "w")
Jack Jansen8cb1ff52003-02-21 23:14:30 +0000774 f = "@findertoolstest"
Jack Jansen43935122001-04-07 12:53:45 +0000775 reveal(f) # reveal this file in a Finder window
776 select(f) # select this file
777
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
782
783 label(f, 2) # set the label of this file to something orange
784 print '\tlabel', label(f) # get the label of this file
785
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)))
792
793 windowsize(base, (200, 400))
794 windowview(base, 1) # set the view by icon
795
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
Jack Jansencb100d12003-01-21 15:30:21 +0000802 windowposition(base, orgpos) # park it where it was before
Jack Jansen43935122001-04-07 12:53:45 +0000803
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
808
809def _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
816
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?
827
Jack Jansen0585d411996-09-20 15:26:20 +0000828if __name__ == '__main__':
Jack Jansen43935122001-04-07 12:53:45 +0000829 _test()
830 _test2()
831 _test3()
Jack Jansen0585d411996-09-20 15:26:20 +0000832