blob: b3223bd6ab0a83a04b7d330ed2bf30b0f24828a3 [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 Jansen0ae32202003-04-09 13:25:43 +000033 """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
40
Jack Jansen0585d411996-09-20 15:26:20 +000041def launch(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000042 """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)
46
Jack Jansen0585d411996-09-20 15:26:20 +000047def Print(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000048 """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)
52
Jack Jansen0585d411996-09-20 15:26:20 +000053def copy(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000054 """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)
Jack Jansen0585d411996-09-20 15:26:20 +000064
65def move(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000066 """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)
76
Jack Jansen0585d411996-09-20 15:26:20 +000077def sleep():
Jack Jansen0ae32202003-04-09 13:25:43 +000078 """Put the mac to sleep"""
79 finder = _getfinder()
80 finder.sleep()
81
Jack Jansen0585d411996-09-20 15:26:20 +000082def shutdown():
Jack Jansen0ae32202003-04-09 13:25:43 +000083 """Shut the mac down"""
84 finder = _getfinder()
85 finder.shut_down()
86
Jack Jansen0585d411996-09-20 15:26:20 +000087def restart():
Jack Jansen0ae32202003-04-09 13:25:43 +000088 """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#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +000094# Additional findertools
Jack Jansen43935122001-04-07 12:53:45 +000095#
96
97def reveal(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000098 """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)
103
Jack Jansen43935122001-04-07 12:53:45 +0000104def select(file):
Jack Jansen0ae32202003-04-09 13:25:43 +0000105 """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)
110
Jack Jansen43935122001-04-07 12:53:45 +0000111def update(file):
Jack Jansen0ae32202003-04-09 13:25:43 +0000112 """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)
Jack Jansen43935122001-04-07 12:53:45 +0000118
119
120#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000121# More findertools
Jack Jansen43935122001-04-07 12:53:45 +0000122#
123
124def comment(object, comment=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000125 """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)
132
Jack Jansen43935122001-04-07 12:53:45 +0000133def _setcomment(object_alias, comment):
Jack Jansen0ae32202003-04-09 13:25:43 +0000134 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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000146
147def _getcomment(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000148 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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000159
160
161#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000162# Get information about current processes in the Finder.
Jack Jansen43935122001-04-07 12:53:45 +0000163
164def processes():
Jack Jansen0ae32202003-04-09 13:25:43 +0000165 """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
Jack Jansen43935122001-04-07 12:53:45 +0000210
211class _process:
Jack Jansen0ae32202003-04-09 13:25:43 +0000212 pass
Jack Jansen43935122001-04-07 12:53:45 +0000213
214def isactiveprocess(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000215 """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
Jack Jansen43935122001-04-07 12:53:45 +0000223def processinfo(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000224 """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
Jack Jansen43935122001-04-07 12:53:45 +0000242def _processproperty(processname, property):
Jack Jansen0ae32202003-04-09 13:25:43 +0000243 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000255
256
257#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000258# Mess around with Finder windows.
259
Jack Jansen43935122001-04-07 12:53:45 +0000260def openwindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000261 """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)
274
Jack Jansen43935122001-04-07 12:53:45 +0000275def closewindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000276 """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)
Jack Jansen43935122001-04-07 12:53:45 +0000289
290def location(object, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000291 """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)
298
Jack Jansen43935122001-04-07 12:53:45 +0000299def _setlocation(object_alias, (x, y)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000300 """_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
Jack Jansen43935122001-04-07 12:53:45 +0000313def _getlocation(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000314 """_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
Jack Jansen43935122001-04-07 12:53:45 +0000327
328def label(object, index=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000329 """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)
337
Jack Jansen43935122001-04-07 12:53:45 +0000338def _getlabel(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000339 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000351
352def _setlabel(object_alias, index):
Jack Jansen0ae32202003-04-09 13:25:43 +0000353 """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
Jack Jansen43935122001-04-07 12:53:45 +0000369
370def windowview(folder, view=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000371 """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 """
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)
381
Jack Jansen43935122001-04-07 12:53:45 +0000382def _setwindowview(folder_alias, view=0):
Jack Jansen0ae32202003-04-09 13:25:43 +0000383 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000410
411def _getwindowview(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000412 """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]
Jack Jansen43935122001-04-07 12:53:45 +0000428
429def windowsize(folder, size=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000430 """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 """
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)
440
Jack Jansen43935122001-04-07 12:53:45 +0000441def _setwindowsize(folder_alias, (w, h)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000442 """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
Jack Jansen43935122001-04-07 12:53:45 +0000462def _getwindowsize(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000463 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000479
480def windowposition(folder, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000481 """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)
491
Jack Jansen43935122001-04-07 12:53:45 +0000492def _setwindowposition(folder_alias, (x, y)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000493 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000510
511def _getwindowposition(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000512 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000528
529def icon(object, icondata=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000530 """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)
539
Jack Jansen43935122001-04-07 12:53:45 +0000540def _geticon(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000541 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000555
556def _seticon(object_alias, icondata):
Jack Jansen0ae32202003-04-09 13:25:43 +0000557 """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
Jack Jansen43935122001-04-07 12:53:45 +0000572
573
574#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000575# Volumes and servers.
576
Jack Jansen43935122001-04-07 12:53:45 +0000577def mountvolume(volume, server=None, username=None, password=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000578 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000597
598def unmountvolume(volume):
Jack Jansen0ae32202003-04-09 13:25:43 +0000599 """unmount a volume that's on the desktop"""
600 putaway(volume)
601
Jack Jansen43935122001-04-07 12:53:45 +0000602def putaway(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000603 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000613
614
615#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000616# Miscellaneous functions
Jack Jansen43935122001-04-07 12:53:45 +0000617#
618
619def volumelevel(level):
Jack Jansen0ae32202003-04-09 13:25:43 +0000620 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000634
635def OSversion():
Jack Jansen0ae32202003-04-09 13:25:43 +0000636 """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['----']
Jack Jansen43935122001-04-07 12:53:45 +0000647
648def filesharing():
Jack Jansen0ae32202003-04-09 13:25:43 +0000649 """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
Jack Jansen43935122001-04-07 12:53:45 +0000679def movetotrash(path):
Jack Jansen0ae32202003-04-09 13:25:43 +0000680 """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)
Jack Jansen43935122001-04-07 12:53:45 +0000684
685def emptytrash():
Jack Jansen0ae32202003-04-09 13:25:43 +0000686 """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)
Jack Jansen43935122001-04-07 12:53:45 +0000694
695
696def _test():
Jack Jansen0ae32202003-04-09 13:25:43 +0000697 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()
Jack Jansen43935122001-04-07 12:53:45 +0000756
757def _test2():
Jack Jansen0ae32202003-04-09 13:25:43 +0000758 print '\nmorefindertools version %s\nTests coming up...' %__version__
759 import os
760 import random
Jack Jansen43935122001-04-07 12:53:45 +0000761
Jack Jansen0ae32202003-04-09 13:25:43 +0000762 # miscellaneous
763 print '\tfilesharing on?', filesharing() # is file sharing on, off, starting up?
764 print '\tOS version', OSversion() # the version of the system software
Jack Jansen43935122001-04-07 12:53:45 +0000765
Jack Jansen0ae32202003-04-09 13:25:43 +0000766 # 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()
Jack Jansen43935122001-04-07 12:53:45 +0000771
Jack Jansen0ae32202003-04-09 13:25:43 +0000772 # 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
Jack Jansen43935122001-04-07 12:53:45 +0000777
Jack Jansen0ae32202003-04-09 13:25:43 +0000778 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
Jack Jansen43935122001-04-07 12:53:45 +0000782
Jack Jansen0ae32202003-04-09 13:25:43 +0000783 label(f, 2) # set the label of this file to something orange
784 print '\tlabel', label(f) # get the label of this file
Jack Jansen43935122001-04-07 12:53:45 +0000785
Jack Jansen0ae32202003-04-09 13:25:43 +0000786 # 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)))
Jack Jansen43935122001-04-07 12:53:45 +0000792
Jack Jansen0ae32202003-04-09 13:25:43 +0000793 windowsize(base, (200, 400))
794 windowview(base, 1) # set the view by icon
Jack Jansen43935122001-04-07 12:53:45 +0000795
Jack Jansen0ae32202003-04-09 13:25:43 +0000796 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
Jack Jansen43935122001-04-07 12:53:45 +0000803
Jack Jansen0ae32202003-04-09 13:25:43 +0000804 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
Jack Jansen43935122001-04-07 12:53:45 +0000809def _test3():
Jack Jansen0ae32202003-04-09 13:25:43 +0000810 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?
Jack Jansen43935122001-04-07 12:53:45 +0000827
Jack Jansen0585d411996-09-20 15:26:20 +0000828if __name__ == '__main__':
Jack Jansen0ae32202003-04-09 13:25:43 +0000829 _test()
830 _test2()
831 _test3()
832