blob: 036c5c5d89bf79260784f367407b920229afc766 [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()
Tim Peters182b5ac2004-07-18 06:16:08 +000037 _finder_talker.send_flags = ( _finder_talker.send_flags |
Jack Jansen0ae32202003-04-09 13:25:43 +000038 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
39 return _finder_talker
Tim Peters182b5ac2004-07-18 06:16:08 +000040
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)
Tim Peters182b5ac2004-07-18 06:16:08 +000046
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)
Tim Peters182b5ac2004-07-18 06:16:08 +000052
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)
Tim Peters182b5ac2004-07-18 06:16:08 +000076
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()
Tim Peters182b5ac2004-07-18 06:16:08 +000081
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()
Tim Peters182b5ac2004-07-18 06:16:08 +000086
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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000103
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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000110
Jack Jansen43935122001-04-07 12:53:45 +0000111def update(file):
Tim Peters182b5ac2004-07-18 06:16:08 +0000112 """Update the display of the specified object(s) to match
Jack Jansen0ae32202003-04-09 13:25:43 +0000113 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000132
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000142 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000143 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000144 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000145 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000155 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000156 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000157 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000158 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000177 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000178 raise Error, aetools.decodeerror(args)
179 p = []
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000180 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000181 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000196 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000197 raise Error, aetools.decodeerror(_arg)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000198 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000199 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
Tim Peters182b5ac2004-07-18 06:16:08 +0000222
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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000226
Jack Jansen0ae32202003-04-09 13:25:43 +0000227 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
Tim Peters182b5ac2004-07-18 06:16:08 +0000241
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000251 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000252 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000253 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000254 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.
Tim Peters182b5ac2004-07-18 06:16:08 +0000259
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000272 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000273 raise Error, aetools.decodeerror(args)
Tim Peters182b5ac2004-07-18 06:16:08 +0000274
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000287 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000288 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000298
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000299def _setlocation(object_alias, location):
Jack Jansen0ae32202003-04-09 13:25:43 +0000300 """_setlocation: Set the location of the icon for the object."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000301 x, y = location
Jack Jansen0ae32202003-04-09 13:25:43 +0000302 finder = _getfinder()
303 args = {}
304 attrs = {}
305 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
306 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00)
307 args['----'] = aeobj_01
308 args["data"] = [x, y]
309 _reply, args, attrs = finder.send("core", "setd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000310 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000311 raise Error, aetools.decodeerror(args)
312 return (x,y)
Tim Peters182b5ac2004-07-18 06:16:08 +0000313
Jack Jansen43935122001-04-07 12:53:45 +0000314def _getlocation(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000315 """_getlocation: get the location of the icon for the object."""
316 finder = _getfinder()
317 args = {}
318 attrs = {}
319 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
320 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('posn'), fr=aeobj_00)
321 args['----'] = aeobj_01
322 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000323 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000324 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000325 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000326 pos = args['----']
327 return pos.h, pos.v
Jack Jansen43935122001-04-07 12:53:45 +0000328
329def label(object, index=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000330 """label: set or get the label of the item. Specify file by name or fsspec."""
331 object = Carbon.File.FSRef(object)
332 object_alias = object.FSNewAliasMinimal()
333 if index == None:
334 return _getlabel(object_alias)
335 if index < 0 or index > 7:
336 index = 0
337 return _setlabel(object_alias, index)
Tim Peters182b5ac2004-07-18 06:16:08 +0000338
Jack Jansen43935122001-04-07 12:53:45 +0000339def _getlabel(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000340 """label: Get the label for the object."""
341 finder = _getfinder()
342 args = {}
343 attrs = {}
344 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
345 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('labi'), fr=aeobj_00)
346 args['----'] = aeobj_01
347 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000348 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000349 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000350 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000351 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000352
353def _setlabel(object_alias, index):
Jack Jansen0ae32202003-04-09 13:25:43 +0000354 """label: Set the label for the object."""
355 finder = _getfinder()
356 args = {}
357 attrs = {}
358 _code = 'core'
359 _subcode = 'setd'
360 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
361 form="alis", seld=object_alias, fr=None)
362 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
363 form="prop", seld=aetypes.Type('labi'), fr=aeobj_0)
364 args['----'] = aeobj_1
365 args["data"] = index
366 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000367 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000368 raise Error, aetools.decodeerror(args)
369 return index
Jack Jansen43935122001-04-07 12:53:45 +0000370
371def windowview(folder, view=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000372 """windowview: Set the view of the window for the folder. Specify file by name or fsspec.
373 0 = by icon (default)
374 1 = by name
375 2 = by button
376 """
377 fsr = Carbon.File.FSRef(folder)
378 folder_alias = fsr.FSNewAliasMinimal()
379 if view == None:
380 return _getwindowview(folder_alias)
381 return _setwindowview(folder_alias, view)
Tim Peters182b5ac2004-07-18 06:16:08 +0000382
Jack Jansen43935122001-04-07 12:53:45 +0000383def _setwindowview(folder_alias, view=0):
Jack Jansen0ae32202003-04-09 13:25:43 +0000384 """set the windowview"""
385 attrs = {}
386 args = {}
387 if view == 1:
388 _v = aetypes.Type('pnam')
389 elif view == 2:
390 _v = aetypes.Type('lgbu')
391 else:
392 _v = aetypes.Type('iimg')
393 finder = _getfinder()
Tim Peters182b5ac2004-07-18 06:16:08 +0000394 aeobj_0 = aetypes.ObjectSpecifier(want = aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000395 form = 'alis', seld = folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000396 aeobj_1 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000397 form = 'prop', seld = aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000398 aeobj_2 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000399 form = 'prop', seld = aetypes.Type('pvew'), fr=aeobj_1)
Tim Peters182b5ac2004-07-18 06:16:08 +0000400 aeobj_3 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000401 form = 'prop', seld = _v, fr=None)
402 _code = 'core'
403 _subcode = 'setd'
404 args['----'] = aeobj_2
405 args['data'] = aeobj_3
406 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000407 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000408 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000409 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000410 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000411
412def _getwindowview(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000413 """get the windowview"""
414 attrs = {}
415 args = {}
416 finder = _getfinder()
417 args = {}
418 attrs = {}
419 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=folder_alias, fr=None)
420 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_00)
421 aeobj_02 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('pvew'), fr=aeobj_01)
422 args['----'] = aeobj_02
423 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000424 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000425 raise Error, aetools.decodeerror(args)
426 views = {'iimg':0, 'pnam':1, 'lgbu':2}
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000427 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000428 return views[args['----'].enum]
Jack Jansen43935122001-04-07 12:53:45 +0000429
430def windowsize(folder, size=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000431 """Set the size of a Finder window for folder to size=(w, h), Specify by path.
432 If size=None, windowsize will return the current size of the window.
433 Specify file by name or fsspec.
434 """
435 fsr = Carbon.File.FSRef(folder)
436 folder_alias = fsr.FSNewAliasMinimal()
437 openwindow(fsr)
438 if not size:
439 return _getwindowsize(folder_alias)
440 return _setwindowsize(folder_alias, size)
Tim Peters182b5ac2004-07-18 06:16:08 +0000441
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000442def _setwindowsize(folder_alias, size):
Jack Jansen0ae32202003-04-09 13:25:43 +0000443 """Set the size of a Finder window for folder to (w, h)"""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000444 w, h = size
Jack Jansen0ae32202003-04-09 13:25:43 +0000445 finder = _getfinder()
446 args = {}
447 attrs = {}
448 _code = 'core'
449 _subcode = 'setd'
450 aevar00 = [w, h]
451 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
452 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000453 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000454 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000455 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000456 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
457 args['----'] = aeobj_2
458 args["data"] = aevar00
459 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000460 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000461 raise Error, aetools.decodeerror(args)
462 return (w, h)
Tim Peters182b5ac2004-07-18 06:16:08 +0000463
Jack Jansen43935122001-04-07 12:53:45 +0000464def _getwindowsize(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000465 """Set the size of a Finder window for folder to (w, h)"""
466 finder = _getfinder()
467 args = {}
468 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000469 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000470 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000471 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000472 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000473 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000474 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
475 args['----'] = aeobj_2
476 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000477 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000478 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000479 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000480 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000481
482def windowposition(folder, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000483 """Set the position of a Finder window for folder to pos=(w, h)."""
484 fsr = Carbon.File.FSRef(folder)
485 folder_alias = fsr.FSNewAliasMinimal()
486 openwindow(fsr)
487 if not pos:
488 return _getwindowposition(folder_alias)
Georg Brandl57b39e02007-04-11 19:24:50 +0000489 if aetypes.IsQDPoint(pos):
490 # QDPoint object as returned by _getwindowposition
Jack Jansen0ae32202003-04-09 13:25:43 +0000491 pos = (pos.h, pos.v)
492 return _setwindowposition(folder_alias, pos)
Tim Peters182b5ac2004-07-18 06:16:08 +0000493
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000494def _setwindowposition(folder_alias, position):
Jack Jansen0ae32202003-04-09 13:25:43 +0000495 """Set the size of a Finder window for folder to (w, h)."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000496 x, y = position
Jack Jansen0ae32202003-04-09 13:25:43 +0000497 finder = _getfinder()
498 args = {}
499 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000500 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000501 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000502 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000503 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000504 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000505 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
506 args['----'] = aeobj_2
507 args["data"] = [x, y]
508 _reply, args, attrs = finder.send('core', 'setd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000509 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000510 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000511 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000512 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000513
514def _getwindowposition(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000515 """Get the size of a Finder window for folder, Specify by path."""
516 finder = _getfinder()
517 args = {}
518 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000519 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000520 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000521 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000522 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000523 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000524 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
525 args['----'] = aeobj_2
526 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000527 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000528 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000529 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000530 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000531
532def icon(object, icondata=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000533 """icon sets the icon of object, if no icondata is given,
534 icon will return an AE object with binary data for the current icon.
535 If left untouched, this data can be used to paste the icon on another file.
536 Development opportunity: get and set the data as PICT."""
537 fsr = Carbon.File.FSRef(object)
538 object_alias = fsr.FSNewAliasMinimal()
539 if icondata == None:
540 return _geticon(object_alias)
541 return _seticon(object_alias, icondata)
Tim Peters182b5ac2004-07-18 06:16:08 +0000542
Jack Jansen43935122001-04-07 12:53:45 +0000543def _geticon(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000544 """get the icondata for object. Binary data of some sort."""
545 finder = _getfinder()
546 args = {}
547 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000548 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000549 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000550 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000551 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
552 args['----'] = aeobj_01
553 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000554 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000555 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000556 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000557 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000558
559def _seticon(object_alias, icondata):
Jack Jansen0ae32202003-04-09 13:25:43 +0000560 """set the icondata for object, formatted as produced by _geticon()"""
561 finder = _getfinder()
562 args = {}
563 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000564 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000565 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000566 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000567 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
568 args['----'] = aeobj_01
569 args["data"] = icondata
570 _reply, args, attrs = finder.send("core", "setd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000571 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000572 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000573 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000574 return args['----'].data
Jack Jansen43935122001-04-07 12:53:45 +0000575
576
577#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000578# Volumes and servers.
Tim Peters182b5ac2004-07-18 06:16:08 +0000579
Jack Jansen43935122001-04-07 12:53:45 +0000580def mountvolume(volume, server=None, username=None, password=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000581 """mount a volume, local or on a server on AppleTalk.
582 Note: mounting a ASIP server requires a different operation.
583 server is the name of the server where the volume belongs
584 username, password belong to a registered user of the volume."""
585 finder = _getfinder()
586 args = {}
587 attrs = {}
588 if password:
589 args["PASS"] = password
590 if username:
591 args["USER"] = username
592 if server:
593 args["SRVR"] = server
594 args['----'] = volume
595 _reply, args, attrs = finder.send("aevt", "mvol", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000596 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000597 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000598 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000599 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000600
601def unmountvolume(volume):
Jack Jansen0ae32202003-04-09 13:25:43 +0000602 """unmount a volume that's on the desktop"""
603 putaway(volume)
Tim Peters182b5ac2004-07-18 06:16:08 +0000604
Jack Jansen43935122001-04-07 12:53:45 +0000605def putaway(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000606 """puth the object away, whereever it came from."""
607 finder = _getfinder()
608 args = {}
609 attrs = {}
610 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('cdis'), form="name", seld=object, fr=None)
611 _reply, args, attrs = talker.send("fndr", "ptwy", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000612 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000613 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000614 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000615 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000616
617
618#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000619# Miscellaneous functions
Jack Jansen43935122001-04-07 12:53:45 +0000620#
621
622def volumelevel(level):
Jack Jansen0ae32202003-04-09 13:25:43 +0000623 """set the audio output level, parameter between 0 (silent) and 7 (full blast)"""
624 finder = _getfinder()
625 args = {}
626 attrs = {}
627 if level < 0:
628 level = 0
629 elif level > 7:
630 level = 7
631 args['----'] = level
632 _reply, args, attrs = finder.send("aevt", "stvl", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000633 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000634 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000635 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000636 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000637
638def OSversion():
Jack Jansen0ae32202003-04-09 13:25:43 +0000639 """return the version of the system software"""
640 finder = _getfinder()
641 args = {}
642 attrs = {}
643 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('ver2'), fr=None)
644 args['----'] = aeobj_00
645 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000646 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000647 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000648 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000649 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000650
651def filesharing():
Jack Jansen0ae32202003-04-09 13:25:43 +0000652 """return the current status of filesharing and whether it is starting up or not:
653 -1 file sharing is off and not starting up
654 0 file sharing is off and starting up
655 1 file sharing is on"""
656 status = -1
657 finder = _getfinder()
658 # see if it is on
659 args = {}
660 attrs = {}
661 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fshr'), fr=None)
662 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000663 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000664 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000665 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000666 if args['----'] == 0:
667 status = -1
668 else:
669 status = 1
670 # is it starting up perchance?
671 args = {}
672 attrs = {}
673 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fsup'), fr=None)
674 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000675 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000676 raise Error, aetools.decodeerror(args)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000677 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000678 if args['----'] == 1:
679 status = 0
680 return status
Tim Peters182b5ac2004-07-18 06:16:08 +0000681
Jack Jansen43935122001-04-07 12:53:45 +0000682def movetotrash(path):
Jack Jansen0ae32202003-04-09 13:25:43 +0000683 """move the object to the trash"""
684 fss = Carbon.File.FSSpec(path)
685 trashfolder = Carbon.Folder.FSFindFolder(fss.as_tuple()[0], 'trsh', 0)
686 move(path, trashfolder)
Jack Jansen43935122001-04-07 12:53:45 +0000687
688def emptytrash():
Jack Jansen0ae32202003-04-09 13:25:43 +0000689 """empty the trash"""
690 finder = _getfinder()
691 args = {}
692 attrs = {}
693 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('trsh'), fr=None)
694 _reply, args, attrs = finder.send("fndr", "empt", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000695 if 'errn' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000696 raise aetools.Error, aetools.decodeerror(args)
Jack Jansen43935122001-04-07 12:53:45 +0000697
698
699def _test():
Jack Jansen0ae32202003-04-09 13:25:43 +0000700 import EasyDialogs
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000701 print('Original findertools functionality test...')
702 print('Testing launch...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000703 pathname = EasyDialogs.AskFileForOpen('File to launch:')
704 if pathname:
705 result = launch(pathname)
706 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000707 print('Result: ', result)
708 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000709 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000710 print('Testing print...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000711 pathname = EasyDialogs.AskFileForOpen('File to print:')
712 if pathname:
713 result = Print(pathname)
714 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000715 print('Result: ', result)
716 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000717 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000718 print('Testing copy...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000719 pathname = EasyDialogs.AskFileForOpen('File to copy:')
720 if pathname:
721 destdir = EasyDialogs.AskFolder('Destination:')
722 if destdir:
723 result = copy(pathname, destdir)
724 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000725 print('Result:', result)
726 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000727 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000728 print('Testing move...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000729 pathname = EasyDialogs.AskFileForOpen('File to move:')
730 if pathname:
731 destdir = EasyDialogs.AskFolder('Destination:')
732 if destdir:
733 result = move(pathname, destdir)
734 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000735 print('Result:', result)
736 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000737 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000738 print('Testing sleep...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000739 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
740 result = sleep()
741 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000742 print('Result:', result)
743 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000744 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000745 print('Testing shutdown...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000746 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
747 result = shutdown()
748 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000749 print('Result:', result)
750 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000751 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000752 print('Testing restart...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000753 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
754 result = restart()
755 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000756 print('Result:', result)
757 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000758 sys.stdin.readline()
Jack Jansen43935122001-04-07 12:53:45 +0000759
760def _test2():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000761 print('\nmorefindertools version %s\nTests coming up...' %__version__)
Jack Jansen0ae32202003-04-09 13:25:43 +0000762 import os
763 import random
Jack Jansen43935122001-04-07 12:53:45 +0000764
Jack Jansen0ae32202003-04-09 13:25:43 +0000765 # miscellaneous
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000766 print('\tfilesharing on?', filesharing()) # is file sharing on, off, starting up?
767 print('\tOS version', OSversion()) # the version of the system software
Jack Jansen43935122001-04-07 12:53:45 +0000768
Jack Jansen0ae32202003-04-09 13:25:43 +0000769 # set the soundvolume in a simple way
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000770 print('\tSystem beep volume')
Jack Jansen0ae32202003-04-09 13:25:43 +0000771 for i in range(0, 7):
Tim Peters182b5ac2004-07-18 06:16:08 +0000772 volumelevel(i)
Jack Jansen0ae32202003-04-09 13:25:43 +0000773 MacOS.SysBeep()
Jack Jansen43935122001-04-07 12:53:45 +0000774
Jack Jansen0ae32202003-04-09 13:25:43 +0000775 # Finder's windows, file location, file attributes
776 open("@findertoolstest", "w")
777 f = "@findertoolstest"
778 reveal(f) # reveal this file in a Finder window
779 select(f) # select this file
Jack Jansen43935122001-04-07 12:53:45 +0000780
Jack Jansen0ae32202003-04-09 13:25:43 +0000781 base, file = os.path.split(f)
782 closewindow(base) # close the window this file is in (opened by reveal)
783 openwindow(base) # open it again
784 windowview(base, 1) # set the view by list
Jack Jansen43935122001-04-07 12:53:45 +0000785
Jack Jansen0ae32202003-04-09 13:25:43 +0000786 label(f, 2) # set the label of this file to something orange
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000787 print('\tlabel', label(f)) # get the label of this file
Jack Jansen43935122001-04-07 12:53:45 +0000788
Jack Jansen0ae32202003-04-09 13:25:43 +0000789 # the file location only works in a window with icon view!
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000790 print('Random locations for an icon')
Jack Jansen0ae32202003-04-09 13:25:43 +0000791 windowview(base, 0) # set the view by icon
792 windowsize(base, (600, 600))
793 for i in range(50):
794 location(f, (random.randint(10, 590), random.randint(10, 590)))
Jack Jansen43935122001-04-07 12:53:45 +0000795
Jack Jansen0ae32202003-04-09 13:25:43 +0000796 windowsize(base, (200, 400))
797 windowview(base, 1) # set the view by icon
Jack Jansen43935122001-04-07 12:53:45 +0000798
Jack Jansen0ae32202003-04-09 13:25:43 +0000799 orgpos = windowposition(base)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000800 print('Animated window location')
Jack Jansen0ae32202003-04-09 13:25:43 +0000801 for i in range(10):
802 pos = (100+i*10, 100+i*10)
803 windowposition(base, pos)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000804 print('\twindow position', pos)
Jack Jansen0ae32202003-04-09 13:25:43 +0000805 windowposition(base, orgpos) # park it where it was before
Jack Jansen43935122001-04-07 12:53:45 +0000806
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000807 print('Put a comment in file', f, ':')
808 print('\t', comment(f)) # print the Finder comment this file has
Jack Jansen0ae32202003-04-09 13:25:43 +0000809 s = 'This is a comment no one reads!'
810 comment(f, s) # set the Finder comment
Tim Peters182b5ac2004-07-18 06:16:08 +0000811
Jack Jansen43935122001-04-07 12:53:45 +0000812def _test3():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000813 print('MacOS9 or better specific functions')
Jack Jansen0ae32202003-04-09 13:25:43 +0000814 # processes
815 pr = processes() # return a list of tuples with (active_processname, creatorcode)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000816 print('Return a list of current active processes:')
Jack Jansen0ae32202003-04-09 13:25:43 +0000817 for p in pr:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000818 print('\t', p)
Tim Peters182b5ac2004-07-18 06:16:08 +0000819
Jack Jansen0ae32202003-04-09 13:25:43 +0000820 # get attributes of the first process in the list
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000821 print('Attributes of the first process in the list:')
Jack Jansen0ae32202003-04-09 13:25:43 +0000822 pinfo = processinfo(pr[0][0])
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000823 print('\t', pr[0][0])
824 print('\t\tmemory partition', pinfo.partition) # the memory allocated to this process
825 print('\t\tmemory used', pinfo.used) # the memory actuall used by this process
826 print('\t\tis visible', pinfo.visible) # is the process visible to the user
827 print('\t\tis frontmost', pinfo.frontmost) # is the process the front most one?
828 print('\t\thas scripting', pinfo.hasscripting) # is the process scriptable?
829 print('\t\taccepts high level events', pinfo.accepthighlevel) # does the process accept high level appleevents?
Jack Jansen43935122001-04-07 12:53:45 +0000830
Jack Jansen0585d411996-09-20 15:26:20 +0000831if __name__ == '__main__':
Jack Jansen0ae32202003-04-09 13:25:43 +0000832 _test()
833 _test2()
834 _test3()