blob: 56e798010c46535bff89c504c976e4afe4c577d2 [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"""
Benjamin Peterson23681932008-05-12 21:42:13 +000017
18from warnings import warnpy3k
Benjamin Petersona6864e02008-07-14 17:42:17 +000019warnpy3k("In 3.x, the findertools module is removed.", stacklevel=2)
Benjamin Peterson23681932008-05-12 21:42:13 +000020
Jack Jansen8bcd4712000-08-20 19:56:13 +000021import Finder
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000022from Carbon import AppleEvents
Jack Jansen0585d411996-09-20 15:26:20 +000023import aetools
24import MacOS
25import sys
Jack Jansen8cb1ff52003-02-21 23:14:30 +000026import Carbon.File
27import Carbon.Folder
Jack Jansen43935122001-04-07 12:53:45 +000028import aetypes
29from types import *
30
31__version__ = '1.1'
32Error = 'findertools.Error'
Jack Jansen0585d411996-09-20 15:26:20 +000033
Jack Jansen0585d411996-09-20 15:26:20 +000034_finder_talker = None
35
36def _getfinder():
Jack Jansen0ae32202003-04-09 13:25:43 +000037 """returns basic (recyclable) Finder AE interface object"""
38 global _finder_talker
39 if not _finder_talker:
40 _finder_talker = Finder.Finder()
Tim Peters182b5ac2004-07-18 06:16:08 +000041 _finder_talker.send_flags = ( _finder_talker.send_flags |
Jack Jansen0ae32202003-04-09 13:25:43 +000042 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
43 return _finder_talker
Tim Peters182b5ac2004-07-18 06:16:08 +000044
Jack Jansen0585d411996-09-20 15:26:20 +000045def launch(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000046 """Open a file thru the finder. Specify file by name or fsspec"""
47 finder = _getfinder()
48 fss = Carbon.File.FSSpec(file)
49 return finder.open(fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000050
Jack Jansen0585d411996-09-20 15:26:20 +000051def Print(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000052 """Print a file thru the finder. Specify file by name or fsspec"""
53 finder = _getfinder()
54 fss = Carbon.File.FSSpec(file)
55 return finder._print(fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000056
Jack Jansen0585d411996-09-20 15:26:20 +000057def copy(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000058 """Copy a file to a folder"""
59 finder = _getfinder()
60 if type(src) == type([]):
61 src_fss = []
62 for s in src:
63 src_fss.append(Carbon.File.FSSpec(s))
64 else:
65 src_fss = Carbon.File.FSSpec(src)
66 dst_fss = Carbon.File.FSSpec(dstdir)
67 return finder.duplicate(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000068
69def move(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000070 """Move a file to a folder"""
71 finder = _getfinder()
72 if type(src) == type([]):
73 src_fss = []
74 for s in src:
75 src_fss.append(Carbon.File.FSSpec(s))
76 else:
77 src_fss = Carbon.File.FSSpec(src)
78 dst_fss = Carbon.File.FSSpec(dstdir)
79 return finder.move(src_fss, to=dst_fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000080
Jack Jansen0585d411996-09-20 15:26:20 +000081def sleep():
Jack Jansen0ae32202003-04-09 13:25:43 +000082 """Put the mac to sleep"""
83 finder = _getfinder()
84 finder.sleep()
Tim Peters182b5ac2004-07-18 06:16:08 +000085
Jack Jansen0585d411996-09-20 15:26:20 +000086def shutdown():
Jack Jansen0ae32202003-04-09 13:25:43 +000087 """Shut the mac down"""
88 finder = _getfinder()
89 finder.shut_down()
Tim Peters182b5ac2004-07-18 06:16:08 +000090
Jack Jansen0585d411996-09-20 15:26:20 +000091def restart():
Jack Jansen0ae32202003-04-09 13:25:43 +000092 """Restart the mac"""
93 finder = _getfinder()
94 finder.restart()
Jack Jansen0585d411996-09-20 15:26:20 +000095
Jack Jansen43935122001-04-07 12:53:45 +000096
97#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +000098# Additional findertools
Jack Jansen43935122001-04-07 12:53:45 +000099#
100
101def reveal(file):
Jack Jansen0ae32202003-04-09 13:25:43 +0000102 """Reveal a file in the finder. Specify file by name, fsref or fsspec."""
103 finder = _getfinder()
104 fsr = Carbon.File.FSRef(file)
105 file_alias = fsr.FSNewAliasMinimal()
106 return finder.reveal(file_alias)
Tim Peters182b5ac2004-07-18 06:16:08 +0000107
Jack Jansen43935122001-04-07 12:53:45 +0000108def select(file):
Jack Jansen0ae32202003-04-09 13:25:43 +0000109 """select a file in the finder. Specify file by name, fsref or fsspec."""
110 finder = _getfinder()
111 fsr = Carbon.File.FSRef(file)
112 file_alias = fsr.FSNewAliasMinimal()
113 return finder.select(file_alias)
Tim Peters182b5ac2004-07-18 06:16:08 +0000114
Jack Jansen43935122001-04-07 12:53:45 +0000115def update(file):
Tim Peters182b5ac2004-07-18 06:16:08 +0000116 """Update the display of the specified object(s) to match
Jack Jansen0ae32202003-04-09 13:25:43 +0000117 their on-disk representation. Specify file by name, fsref or fsspec."""
118 finder = _getfinder()
119 fsr = Carbon.File.FSRef(file)
120 file_alias = fsr.FSNewAliasMinimal()
121 return finder.update(file_alias)
Jack Jansen43935122001-04-07 12:53:45 +0000122
123
124#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000125# More findertools
Jack Jansen43935122001-04-07 12:53:45 +0000126#
127
128def comment(object, comment=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000129 """comment: get or set the Finder-comment of the item, displayed in the 'Get Info' window."""
130 object = Carbon.File.FSRef(object)
131 object_alias = object.FSNewAliasMonimal()
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000132 if comment is None:
Jack Jansen0ae32202003-04-09 13:25:43 +0000133 return _getcomment(object_alias)
134 else:
135 return _setcomment(object_alias, comment)
Tim Peters182b5ac2004-07-18 06:16:08 +0000136
Jack Jansen43935122001-04-07 12:53:45 +0000137def _setcomment(object_alias, comment):
Jack Jansen0ae32202003-04-09 13:25:43 +0000138 finder = _getfinder()
139 args = {}
140 attrs = {}
141 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
142 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00)
143 args['----'] = aeobj_01
144 args["data"] = comment
145 _reply, args, attrs = finder.send("core", "setd", args, attrs)
146 if args.has_key('errn'):
147 raise Error, aetools.decodeerror(args)
148 if args.has_key('----'):
149 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000150
151def _getcomment(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000152 finder = _getfinder()
153 args = {}
154 attrs = {}
155 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
156 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00)
157 args['----'] = aeobj_01
158 _reply, args, attrs = finder.send("core", "getd", args, attrs)
159 if args.has_key('errn'):
160 raise Error, aetools.decodeerror(args)
161 if args.has_key('----'):
162 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000163
164
165#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000166# Get information about current processes in the Finder.
Jack Jansen43935122001-04-07 12:53:45 +0000167
168def processes():
Jack Jansen0ae32202003-04-09 13:25:43 +0000169 """processes returns a list of all active processes running on this computer and their creators."""
170 finder = _getfinder()
171 args = {}
172 attrs = {}
173 processnames = []
174 processnumbers = []
175 creators = []
176 partitions = []
177 used = []
178 ## get the processnames or else the processnumbers
179 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None)
180 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
181 if args.has_key('errn'):
182 raise Error, aetools.decodeerror(args)
183 p = []
184 if args.has_key('----'):
185 p = args['----']
186 for proc in p:
187 if hasattr(proc, 'seld'):
188 # it has a real name
189 processnames.append(proc.seld)
190 elif hasattr(proc, 'type'):
191 if proc.type == "psn ":
192 # it has a process number
193 processnumbers.append(proc.data)
194 ## get the creators
195 args = {}
196 attrs = {}
197 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None)
198 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fcrt'), fr=aeobj_0)
199 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
200 if args.has_key('errn'):
201 raise Error, aetools.decodeerror(_arg)
202 if args.has_key('----'):
203 p = args['----']
204 creators = p[:]
205 ## concatenate in one dict
206 result = []
207 if len(processnames) > len(processnumbers):
208 data = processnames
209 else:
210 data = processnumbers
211 for i in range(len(creators)):
212 result.append((data[i], creators[i]))
213 return result
Jack Jansen43935122001-04-07 12:53:45 +0000214
215class _process:
Jack Jansen0ae32202003-04-09 13:25:43 +0000216 pass
Jack Jansen43935122001-04-07 12:53:45 +0000217
218def isactiveprocess(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000219 """Check of processname is active. MacOS9"""
220 all = processes()
221 ok = 0
222 for n, c in all:
223 if n == processname:
224 return 1
225 return 0
Tim Peters182b5ac2004-07-18 06:16:08 +0000226
Jack Jansen43935122001-04-07 12:53:45 +0000227def processinfo(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000228 """Return an object with all process properties as attributes for processname. MacOS9"""
229 p = _process()
Tim Peters182b5ac2004-07-18 06:16:08 +0000230
Jack Jansen0ae32202003-04-09 13:25:43 +0000231 if processname == "Finder":
232 p.partition = None
233 p.used = None
234 else:
235 p.partition = _processproperty(processname, 'appt')
236 p.used = _processproperty(processname, 'pusd')
237 p.visible = _processproperty(processname, 'pvis') #Is the process' layer visible?
238 p.frontmost = _processproperty(processname, 'pisf') #Is the process the frontmost process?
239 p.file = _processproperty(processname, 'file') #the file from which the process was launched
240 p.filetype = _processproperty(processname, 'asty') #the OSType of the file type of the process
241 p.creatortype = _processproperty(processname, 'fcrt') #the OSType of the creator of the process (the signature)
242 p.accepthighlevel = _processproperty(processname, 'revt') #Is the process high-level event aware (accepts open application, open document, print document, and quit)?
243 p.hasscripting = _processproperty(processname, 'hscr') #Does the process have a scripting terminology, i.e., can it be scripted?
244 return p
Tim Peters182b5ac2004-07-18 06:16:08 +0000245
Jack Jansen43935122001-04-07 12:53:45 +0000246def _processproperty(processname, property):
Jack Jansen0ae32202003-04-09 13:25:43 +0000247 """return the partition size and memory used for processname"""
248 finder = _getfinder()
249 args = {}
250 attrs = {}
251 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="name", seld=processname, fr=None)
252 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type(property), fr=aeobj_00)
253 args['----'] = aeobj_01
254 _reply, args, attrs = finder.send("core", "getd", args, attrs)
255 if args.has_key('errn'):
256 raise Error, aetools.decodeerror(args)
257 if args.has_key('----'):
258 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000259
260
261#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000262# Mess around with Finder windows.
Tim Peters182b5ac2004-07-18 06:16:08 +0000263
Jack Jansen43935122001-04-07 12:53:45 +0000264def openwindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000265 """Open a Finder window for object, Specify object by name or fsspec."""
266 finder = _getfinder()
267 object = Carbon.File.FSRef(object)
268 object_alias = object.FSNewAliasMinimal()
269 args = {}
270 attrs = {}
271 _code = 'aevt'
272 _subcode = 'odoc'
273 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
274 args['----'] = aeobj_0
275 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
276 if args.has_key('errn'):
277 raise Error, aetools.decodeerror(args)
Tim Peters182b5ac2004-07-18 06:16:08 +0000278
Jack Jansen43935122001-04-07 12:53:45 +0000279def closewindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000280 """Close a Finder window for folder, Specify by path."""
281 finder = _getfinder()
282 object = Carbon.File.FSRef(object)
283 object_alias = object.FSNewAliasMinimal()
284 args = {}
285 attrs = {}
286 _code = 'core'
287 _subcode = 'clos'
288 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
289 args['----'] = aeobj_0
290 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
291 if args.has_key('errn'):
292 raise Error, aetools.decodeerror(args)
Jack Jansen43935122001-04-07 12:53:45 +0000293
294def location(object, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000295 """Set the position of a Finder window for folder to pos=(w, h). Specify file by name or fsspec.
296 If pos=None, location will return the current position of the object."""
297 object = Carbon.File.FSRef(object)
298 object_alias = object.FSNewAliasMinimal()
299 if not pos:
300 return _getlocation(object_alias)
301 return _setlocation(object_alias, pos)
Tim Peters182b5ac2004-07-18 06:16:08 +0000302
Jack Jansen43935122001-04-07 12:53:45 +0000303def _setlocation(object_alias, (x, y)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000304 """_setlocation: Set the location of the icon for the object."""
305 finder = _getfinder()
306 args = {}
307 attrs = {}
308 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
309 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00)
310 args['----'] = aeobj_01
311 args["data"] = [x, y]
312 _reply, args, attrs = finder.send("core", "setd", args, attrs)
313 if args.has_key('errn'):
314 raise Error, aetools.decodeerror(args)
315 return (x,y)
Tim Peters182b5ac2004-07-18 06:16:08 +0000316
Jack Jansen43935122001-04-07 12:53:45 +0000317def _getlocation(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000318 """_getlocation: get the location of the icon for the object."""
319 finder = _getfinder()
320 args = {}
321 attrs = {}
322 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
323 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00)
324 args['----'] = aeobj_01
325 _reply, args, attrs = finder.send("core", "getd", args, attrs)
326 if args.has_key('errn'):
327 raise Error, aetools.decodeerror(args)
328 if args.has_key('----'):
329 pos = args['----']
330 return pos.h, pos.v
Jack Jansen43935122001-04-07 12:53:45 +0000331
332def label(object, index=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000333 """label: set or get the label of the item. Specify file by name or fsspec."""
334 object = Carbon.File.FSRef(object)
335 object_alias = object.FSNewAliasMinimal()
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000336 if index is None:
Jack Jansen0ae32202003-04-09 13:25:43 +0000337 return _getlabel(object_alias)
338 if index < 0 or index > 7:
339 index = 0
340 return _setlabel(object_alias, index)
Tim Peters182b5ac2004-07-18 06:16:08 +0000341
Jack Jansen43935122001-04-07 12:53:45 +0000342def _getlabel(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000343 """label: Get the label for the object."""
344 finder = _getfinder()
345 args = {}
346 attrs = {}
347 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
348 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('labi'), fr=aeobj_00)
349 args['----'] = aeobj_01
350 _reply, args, attrs = finder.send("core", "getd", args, attrs)
351 if args.has_key('errn'):
352 raise Error, aetools.decodeerror(args)
353 if args.has_key('----'):
354 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000355
356def _setlabel(object_alias, index):
Jack Jansen0ae32202003-04-09 13:25:43 +0000357 """label: Set the label for the object."""
358 finder = _getfinder()
359 args = {}
360 attrs = {}
361 _code = 'core'
362 _subcode = 'setd'
363 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
364 form="alis", seld=object_alias, fr=None)
365 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
366 form="prop", seld=aetypes.Type('labi'), fr=aeobj_0)
367 args['----'] = aeobj_1
368 args["data"] = index
369 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
370 if args.has_key('errn'):
371 raise Error, aetools.decodeerror(args)
372 return index
Jack Jansen43935122001-04-07 12:53:45 +0000373
374def windowview(folder, view=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000375 """windowview: Set the view of the window for the folder. Specify file by name or fsspec.
376 0 = by icon (default)
377 1 = by name
378 2 = by button
379 """
380 fsr = Carbon.File.FSRef(folder)
381 folder_alias = fsr.FSNewAliasMinimal()
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000382 if view is None:
Jack Jansen0ae32202003-04-09 13:25:43 +0000383 return _getwindowview(folder_alias)
384 return _setwindowview(folder_alias, view)
Tim Peters182b5ac2004-07-18 06:16:08 +0000385
Jack Jansen43935122001-04-07 12:53:45 +0000386def _setwindowview(folder_alias, view=0):
Jack Jansen0ae32202003-04-09 13:25:43 +0000387 """set the windowview"""
388 attrs = {}
389 args = {}
390 if view == 1:
391 _v = aetypes.Type('pnam')
392 elif view == 2:
393 _v = aetypes.Type('lgbu')
394 else:
395 _v = aetypes.Type('iimg')
396 finder = _getfinder()
Tim Peters182b5ac2004-07-18 06:16:08 +0000397 aeobj_0 = aetypes.ObjectSpecifier(want = aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000398 form = 'alis', seld = folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000399 aeobj_1 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000400 form = 'prop', seld = aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000401 aeobj_2 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000402 form = 'prop', seld = aetypes.Type('pvew'), fr=aeobj_1)
Tim Peters182b5ac2004-07-18 06:16:08 +0000403 aeobj_3 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000404 form = 'prop', seld = _v, fr=None)
405 _code = 'core'
406 _subcode = 'setd'
407 args['----'] = aeobj_2
408 args['data'] = aeobj_3
409 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
410 if args.has_key('errn'):
411 raise Error, aetools.decodeerror(args)
412 if args.has_key('----'):
413 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000414
415def _getwindowview(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000416 """get the windowview"""
417 attrs = {}
418 args = {}
419 finder = _getfinder()
420 args = {}
421 attrs = {}
422 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=folder_alias, fr=None)
423 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_00)
424 aeobj_02 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('pvew'), fr=aeobj_01)
425 args['----'] = aeobj_02
426 _reply, args, attrs = finder.send("core", "getd", args, attrs)
427 if args.has_key('errn'):
428 raise Error, aetools.decodeerror(args)
429 views = {'iimg':0, 'pnam':1, 'lgbu':2}
430 if args.has_key('----'):
431 return views[args['----'].enum]
Jack Jansen43935122001-04-07 12:53:45 +0000432
433def windowsize(folder, size=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000434 """Set the size of a Finder window for folder to size=(w, h), Specify by path.
435 If size=None, windowsize will return the current size of the window.
436 Specify file by name or fsspec.
437 """
438 fsr = Carbon.File.FSRef(folder)
439 folder_alias = fsr.FSNewAliasMinimal()
440 openwindow(fsr)
441 if not size:
442 return _getwindowsize(folder_alias)
443 return _setwindowsize(folder_alias, size)
Tim Peters182b5ac2004-07-18 06:16:08 +0000444
Jack Jansen43935122001-04-07 12:53:45 +0000445def _setwindowsize(folder_alias, (w, h)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000446 """Set the size of a Finder window for folder to (w, h)"""
447 finder = _getfinder()
448 args = {}
449 attrs = {}
450 _code = 'core'
451 _subcode = 'setd'
452 aevar00 = [w, h]
453 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
454 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000455 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000456 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000457 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000458 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
459 args['----'] = aeobj_2
460 args["data"] = aevar00
461 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
462 if args.has_key('errn'):
463 raise Error, aetools.decodeerror(args)
464 return (w, h)
Tim Peters182b5ac2004-07-18 06:16:08 +0000465
Jack Jansen43935122001-04-07 12:53:45 +0000466def _getwindowsize(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000467 """Set the size of a Finder window for folder to (w, h)"""
468 finder = _getfinder()
469 args = {}
470 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000471 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000472 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000473 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000474 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000475 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000476 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
477 args['----'] = aeobj_2
478 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
479 if args.has_key('errn'):
480 raise Error, aetools.decodeerror(args)
481 if args.has_key('----'):
482 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000483
484def windowposition(folder, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000485 """Set the position of a Finder window for folder to pos=(w, h)."""
486 fsr = Carbon.File.FSRef(folder)
487 folder_alias = fsr.FSNewAliasMinimal()
488 openwindow(fsr)
489 if not pos:
490 return _getwindowposition(folder_alias)
491 if type(pos) == InstanceType:
492 # pos might be a QDPoint object as returned by _getwindowposition
493 pos = (pos.h, pos.v)
494 return _setwindowposition(folder_alias, pos)
Tim Peters182b5ac2004-07-18 06:16:08 +0000495
Jack Jansen43935122001-04-07 12:53:45 +0000496def _setwindowposition(folder_alias, (x, y)):
Jack Jansen0ae32202003-04-09 13:25:43 +0000497 """Set the size of a Finder window for folder to (w, h)."""
498 finder = _getfinder()
499 args = {}
500 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000501 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000502 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000503 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000504 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000505 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000506 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
507 args['----'] = aeobj_2
508 args["data"] = [x, y]
509 _reply, args, attrs = finder.send('core', 'setd', args, attrs)
510 if args.has_key('errn'):
511 raise Error, aetools.decodeerror(args)
512 if args.has_key('----'):
513 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000514
515def _getwindowposition(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000516 """Get the size of a Finder window for folder, Specify by path."""
517 finder = _getfinder()
518 args = {}
519 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000520 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000521 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000522 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000523 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000524 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000525 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
526 args['----'] = aeobj_2
527 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
528 if args.has_key('errn'):
529 raise Error, aetools.decodeerror(args)
530 if args.has_key('----'):
531 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000532
533def icon(object, icondata=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000534 """icon sets the icon of object, if no icondata is given,
535 icon will return an AE object with binary data for the current icon.
536 If left untouched, this data can be used to paste the icon on another file.
537 Development opportunity: get and set the data as PICT."""
538 fsr = Carbon.File.FSRef(object)
539 object_alias = fsr.FSNewAliasMinimal()
Benjamin Peterson5b63acd2008-03-29 15:24:25 +0000540 if icondata is None:
Jack Jansen0ae32202003-04-09 13:25:43 +0000541 return _geticon(object_alias)
542 return _seticon(object_alias, icondata)
Tim Peters182b5ac2004-07-18 06:16:08 +0000543
Jack Jansen43935122001-04-07 12:53:45 +0000544def _geticon(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000545 """get the icondata for object. Binary data of some sort."""
546 finder = _getfinder()
547 args = {}
548 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000549 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000550 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000551 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000552 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
553 args['----'] = aeobj_01
554 _reply, args, attrs = finder.send("core", "getd", args, attrs)
555 if args.has_key('errn'):
556 raise Error, aetools.decodeerror(args)
557 if args.has_key('----'):
558 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000559
560def _seticon(object_alias, icondata):
Jack Jansen0ae32202003-04-09 13:25:43 +0000561 """set the icondata for object, formatted as produced by _geticon()"""
562 finder = _getfinder()
563 args = {}
564 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000565 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000566 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000567 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000568 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
569 args['----'] = aeobj_01
570 args["data"] = icondata
571 _reply, args, attrs = finder.send("core", "setd", args, attrs)
572 if args.has_key('errn'):
573 raise Error, aetools.decodeerror(args)
574 if args.has_key('----'):
575 return args['----'].data
Jack Jansen43935122001-04-07 12:53:45 +0000576
577
578#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000579# Volumes and servers.
Tim Peters182b5ac2004-07-18 06:16:08 +0000580
Jack Jansen43935122001-04-07 12:53:45 +0000581def mountvolume(volume, server=None, username=None, password=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000582 """mount a volume, local or on a server on AppleTalk.
583 Note: mounting a ASIP server requires a different operation.
584 server is the name of the server where the volume belongs
585 username, password belong to a registered user of the volume."""
586 finder = _getfinder()
587 args = {}
588 attrs = {}
589 if password:
590 args["PASS"] = password
591 if username:
592 args["USER"] = username
593 if server:
594 args["SRVR"] = server
595 args['----'] = volume
596 _reply, args, attrs = finder.send("aevt", "mvol", args, attrs)
597 if args.has_key('errn'):
598 raise Error, aetools.decodeerror(args)
599 if args.has_key('----'):
600 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000601
602def unmountvolume(volume):
Jack Jansen0ae32202003-04-09 13:25:43 +0000603 """unmount a volume that's on the desktop"""
604 putaway(volume)
Tim Peters182b5ac2004-07-18 06:16:08 +0000605
Jack Jansen43935122001-04-07 12:53:45 +0000606def putaway(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000607 """puth the object away, whereever it came from."""
608 finder = _getfinder()
609 args = {}
610 attrs = {}
611 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('cdis'), form="name", seld=object, fr=None)
612 _reply, args, attrs = talker.send("fndr", "ptwy", args, attrs)
613 if args.has_key('errn'):
614 raise Error, aetools.decodeerror(args)
615 if args.has_key('----'):
616 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000617
618
619#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000620# Miscellaneous functions
Jack Jansen43935122001-04-07 12:53:45 +0000621#
622
623def volumelevel(level):
Jack Jansen0ae32202003-04-09 13:25:43 +0000624 """set the audio output level, parameter between 0 (silent) and 7 (full blast)"""
625 finder = _getfinder()
626 args = {}
627 attrs = {}
628 if level < 0:
629 level = 0
630 elif level > 7:
631 level = 7
632 args['----'] = level
633 _reply, args, attrs = finder.send("aevt", "stvl", args, attrs)
634 if args.has_key('errn'):
635 raise Error, aetools.decodeerror(args)
636 if args.has_key('----'):
637 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000638
639def OSversion():
Jack Jansen0ae32202003-04-09 13:25:43 +0000640 """return the version of the system software"""
641 finder = _getfinder()
642 args = {}
643 attrs = {}
644 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('ver2'), fr=None)
645 args['----'] = aeobj_00
646 _reply, args, attrs = finder.send("core", "getd", args, attrs)
647 if args.has_key('errn'):
648 raise Error, aetools.decodeerror(args)
649 if args.has_key('----'):
650 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000651
652def filesharing():
Jack Jansen0ae32202003-04-09 13:25:43 +0000653 """return the current status of filesharing and whether it is starting up or not:
654 -1 file sharing is off and not starting up
655 0 file sharing is off and starting up
656 1 file sharing is on"""
657 status = -1
658 finder = _getfinder()
659 # see if it is on
660 args = {}
661 attrs = {}
662 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fshr'), fr=None)
663 _reply, args, attrs = finder.send("core", "getd", args, attrs)
664 if args.has_key('errn'):
665 raise Error, aetools.decodeerror(args)
666 if args.has_key('----'):
667 if args['----'] == 0:
668 status = -1
669 else:
670 status = 1
671 # is it starting up perchance?
672 args = {}
673 attrs = {}
674 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fsup'), fr=None)
675 _reply, args, attrs = finder.send("core", "getd", args, attrs)
676 if args.has_key('errn'):
677 raise Error, aetools.decodeerror(args)
678 if args.has_key('----'):
679 if args['----'] == 1:
680 status = 0
681 return status
Tim Peters182b5ac2004-07-18 06:16:08 +0000682
Jack Jansen43935122001-04-07 12:53:45 +0000683def movetotrash(path):
Jack Jansen0ae32202003-04-09 13:25:43 +0000684 """move the object to the trash"""
685 fss = Carbon.File.FSSpec(path)
686 trashfolder = Carbon.Folder.FSFindFolder(fss.as_tuple()[0], 'trsh', 0)
687 move(path, trashfolder)
Jack Jansen43935122001-04-07 12:53:45 +0000688
689def emptytrash():
Jack Jansen0ae32202003-04-09 13:25:43 +0000690 """empty the trash"""
691 finder = _getfinder()
692 args = {}
693 attrs = {}
694 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('trsh'), fr=None)
695 _reply, args, attrs = finder.send("fndr", "empt", args, attrs)
696 if args.has_key('errn'):
697 raise aetools.Error, aetools.decodeerror(args)
Jack Jansen43935122001-04-07 12:53:45 +0000698
699
700def _test():
Jack Jansen0ae32202003-04-09 13:25:43 +0000701 import EasyDialogs
702 print 'Original findertools functionality test...'
703 print 'Testing launch...'
704 pathname = EasyDialogs.AskFileForOpen('File to launch:')
705 if pathname:
706 result = launch(pathname)
707 if result:
708 print 'Result: ', result
709 print 'Press return-',
710 sys.stdin.readline()
711 print 'Testing print...'
712 pathname = EasyDialogs.AskFileForOpen('File to print:')
713 if pathname:
714 result = Print(pathname)
715 if result:
716 print 'Result: ', result
717 print 'Press return-',
718 sys.stdin.readline()
719 print 'Testing copy...'
720 pathname = EasyDialogs.AskFileForOpen('File to copy:')
721 if pathname:
722 destdir = EasyDialogs.AskFolder('Destination:')
723 if destdir:
724 result = copy(pathname, destdir)
725 if result:
726 print 'Result:', result
727 print 'Press return-',
728 sys.stdin.readline()
729 print 'Testing move...'
730 pathname = EasyDialogs.AskFileForOpen('File to move:')
731 if pathname:
732 destdir = EasyDialogs.AskFolder('Destination:')
733 if destdir:
734 result = move(pathname, destdir)
735 if result:
736 print 'Result:', result
737 print 'Press return-',
738 sys.stdin.readline()
739 print 'Testing sleep...'
740 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
741 result = sleep()
742 if result:
743 print 'Result:', result
744 print 'Press return-',
745 sys.stdin.readline()
746 print 'Testing shutdown...'
747 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
748 result = shutdown()
749 if result:
750 print 'Result:', result
751 print 'Press return-',
752 sys.stdin.readline()
753 print 'Testing restart...'
754 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
755 result = restart()
756 if result:
757 print 'Result:', result
758 print 'Press return-',
759 sys.stdin.readline()
Jack Jansen43935122001-04-07 12:53:45 +0000760
761def _test2():
Jack Jansen0ae32202003-04-09 13:25:43 +0000762 print '\nmorefindertools version %s\nTests coming up...' %__version__
763 import os
764 import random
Jack Jansen43935122001-04-07 12:53:45 +0000765
Jack Jansen0ae32202003-04-09 13:25:43 +0000766 # miscellaneous
767 print '\tfilesharing on?', filesharing() # is file sharing on, off, starting up?
768 print '\tOS version', OSversion() # the version of the system software
Jack Jansen43935122001-04-07 12:53:45 +0000769
Jack Jansen0ae32202003-04-09 13:25:43 +0000770 # set the soundvolume in a simple way
771 print '\tSystem beep volume'
772 for i in range(0, 7):
Tim Peters182b5ac2004-07-18 06:16:08 +0000773 volumelevel(i)
Jack Jansen0ae32202003-04-09 13:25:43 +0000774 MacOS.SysBeep()
Jack Jansen43935122001-04-07 12:53:45 +0000775
Jack Jansen0ae32202003-04-09 13:25:43 +0000776 # Finder's windows, file location, file attributes
777 open("@findertoolstest", "w")
778 f = "@findertoolstest"
779 reveal(f) # reveal this file in a Finder window
780 select(f) # select this file
Jack Jansen43935122001-04-07 12:53:45 +0000781
Jack Jansen0ae32202003-04-09 13:25:43 +0000782 base, file = os.path.split(f)
783 closewindow(base) # close the window this file is in (opened by reveal)
784 openwindow(base) # open it again
785 windowview(base, 1) # set the view by list
Jack Jansen43935122001-04-07 12:53:45 +0000786
Jack Jansen0ae32202003-04-09 13:25:43 +0000787 label(f, 2) # set the label of this file to something orange
788 print '\tlabel', label(f) # get the label of this file
Jack Jansen43935122001-04-07 12:53:45 +0000789
Jack Jansen0ae32202003-04-09 13:25:43 +0000790 # the file location only works in a window with icon view!
791 print 'Random locations for an icon'
792 windowview(base, 0) # set the view by icon
793 windowsize(base, (600, 600))
794 for i in range(50):
795 location(f, (random.randint(10, 590), random.randint(10, 590)))
Jack Jansen43935122001-04-07 12:53:45 +0000796
Jack Jansen0ae32202003-04-09 13:25:43 +0000797 windowsize(base, (200, 400))
798 windowview(base, 1) # set the view by icon
Jack Jansen43935122001-04-07 12:53:45 +0000799
Jack Jansen0ae32202003-04-09 13:25:43 +0000800 orgpos = windowposition(base)
801 print 'Animated window location'
802 for i in range(10):
803 pos = (100+i*10, 100+i*10)
804 windowposition(base, pos)
805 print '\twindow position', pos
806 windowposition(base, orgpos) # park it where it was before
Jack Jansen43935122001-04-07 12:53:45 +0000807
Jack Jansen0ae32202003-04-09 13:25:43 +0000808 print 'Put a comment in file', f, ':'
809 print '\t', comment(f) # print the Finder comment this file has
810 s = 'This is a comment no one reads!'
811 comment(f, s) # set the Finder comment
Tim Peters182b5ac2004-07-18 06:16:08 +0000812
Jack Jansen43935122001-04-07 12:53:45 +0000813def _test3():
Jack Jansen0ae32202003-04-09 13:25:43 +0000814 print 'MacOS9 or better specific functions'
815 # processes
816 pr = processes() # return a list of tuples with (active_processname, creatorcode)
817 print 'Return a list of current active processes:'
818 for p in pr:
819 print '\t', p
Tim Peters182b5ac2004-07-18 06:16:08 +0000820
Jack Jansen0ae32202003-04-09 13:25:43 +0000821 # get attributes of the first process in the list
822 print 'Attributes of the first process in the list:'
823 pinfo = processinfo(pr[0][0])
824 print '\t', pr[0][0]
825 print '\t\tmemory partition', pinfo.partition # the memory allocated to this process
826 print '\t\tmemory used', pinfo.used # the memory actuall used by this process
827 print '\t\tis visible', pinfo.visible # is the process visible to the user
828 print '\t\tis frontmost', pinfo.frontmost # is the process the front most one?
829 print '\t\thas scripting', pinfo.hasscripting # is the process scriptable?
830 print '\t\taccepts high level events', pinfo.accepthighlevel # does the process accept high level appleevents?
Jack Jansen43935122001-04-07 12:53:45 +0000831
Jack Jansen0585d411996-09-20 15:26:20 +0000832if __name__ == '__main__':
Jack Jansen0ae32202003-04-09 13:25:43 +0000833 _test()
834 _test2()
835 _test3()