blob: bebd2a0d7dd68f37e3ba40e9f71623bfeab39675 [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
Jack Jansen43935122001-04-07 12:53:45 +000025
26__version__ = '1.1'
27Error = 'findertools.Error'
Jack Jansen0585d411996-09-20 15:26:20 +000028
Jack Jansen0585d411996-09-20 15:26:20 +000029_finder_talker = None
30
31def _getfinder():
Jack Jansen0ae32202003-04-09 13:25:43 +000032 """returns basic (recyclable) Finder AE interface object"""
33 global _finder_talker
34 if not _finder_talker:
35 _finder_talker = Finder.Finder()
Tim Peters182b5ac2004-07-18 06:16:08 +000036 _finder_talker.send_flags = ( _finder_talker.send_flags |
Jack Jansen0ae32202003-04-09 13:25:43 +000037 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
38 return _finder_talker
Tim Peters182b5ac2004-07-18 06:16:08 +000039
Jack Jansen0585d411996-09-20 15:26:20 +000040def launch(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000041 """Open a file thru the finder. Specify file by name or fsspec"""
42 finder = _getfinder()
43 fss = Carbon.File.FSSpec(file)
44 return finder.open(fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000045
Jack Jansen0585d411996-09-20 15:26:20 +000046def Print(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000047 """Print a file thru the finder. Specify file by name or fsspec"""
48 finder = _getfinder()
49 fss = Carbon.File.FSSpec(file)
50 return finder._print(fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000051
Jack Jansen0585d411996-09-20 15:26:20 +000052def copy(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000053 """Copy a file to a folder"""
54 finder = _getfinder()
55 if type(src) == type([]):
56 src_fss = []
57 for s in src:
58 src_fss.append(Carbon.File.FSSpec(s))
59 else:
60 src_fss = Carbon.File.FSSpec(src)
61 dst_fss = Carbon.File.FSSpec(dstdir)
62 return finder.duplicate(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000063
64def move(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +000065 """Move a file to a folder"""
66 finder = _getfinder()
67 if type(src) == type([]):
68 src_fss = []
69 for s in src:
70 src_fss.append(Carbon.File.FSSpec(s))
71 else:
72 src_fss = Carbon.File.FSSpec(src)
73 dst_fss = Carbon.File.FSSpec(dstdir)
74 return finder.move(src_fss, to=dst_fss)
Tim Peters182b5ac2004-07-18 06:16:08 +000075
Jack Jansen0585d411996-09-20 15:26:20 +000076def sleep():
Jack Jansen0ae32202003-04-09 13:25:43 +000077 """Put the mac to sleep"""
78 finder = _getfinder()
79 finder.sleep()
Tim Peters182b5ac2004-07-18 06:16:08 +000080
Jack Jansen0585d411996-09-20 15:26:20 +000081def shutdown():
Jack Jansen0ae32202003-04-09 13:25:43 +000082 """Shut the mac down"""
83 finder = _getfinder()
84 finder.shut_down()
Tim Peters182b5ac2004-07-18 06:16:08 +000085
Jack Jansen0585d411996-09-20 15:26:20 +000086def restart():
Jack Jansen0ae32202003-04-09 13:25:43 +000087 """Restart the mac"""
88 finder = _getfinder()
89 finder.restart()
Jack Jansen0585d411996-09-20 15:26:20 +000090
Jack Jansen43935122001-04-07 12:53:45 +000091
92#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +000093# Additional findertools
Jack Jansen43935122001-04-07 12:53:45 +000094#
95
96def reveal(file):
Jack Jansen0ae32202003-04-09 13:25:43 +000097 """Reveal a file in the finder. Specify file by name, fsref or fsspec."""
98 finder = _getfinder()
99 fsr = Carbon.File.FSRef(file)
100 file_alias = fsr.FSNewAliasMinimal()
101 return finder.reveal(file_alias)
Tim Peters182b5ac2004-07-18 06:16:08 +0000102
Jack Jansen43935122001-04-07 12:53:45 +0000103def select(file):
Jack Jansen0ae32202003-04-09 13:25:43 +0000104 """select a file in the finder. Specify file by name, fsref or fsspec."""
105 finder = _getfinder()
106 fsr = Carbon.File.FSRef(file)
107 file_alias = fsr.FSNewAliasMinimal()
108 return finder.select(file_alias)
Tim Peters182b5ac2004-07-18 06:16:08 +0000109
Jack Jansen43935122001-04-07 12:53:45 +0000110def update(file):
Tim Peters182b5ac2004-07-18 06:16:08 +0000111 """Update the display of the specified object(s) to match
Jack Jansen0ae32202003-04-09 13:25:43 +0000112 their on-disk representation. Specify file by name, fsref or fsspec."""
113 finder = _getfinder()
114 fsr = Carbon.File.FSRef(file)
115 file_alias = fsr.FSNewAliasMinimal()
116 return finder.update(file_alias)
Jack Jansen43935122001-04-07 12:53:45 +0000117
118
119#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000120# More findertools
Jack Jansen43935122001-04-07 12:53:45 +0000121#
122
123def comment(object, comment=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000124 """comment: get or set the Finder-comment of the item, displayed in the 'Get Info' window."""
125 object = Carbon.File.FSRef(object)
126 object_alias = object.FSNewAliasMonimal()
127 if comment == None:
128 return _getcomment(object_alias)
129 else:
130 return _setcomment(object_alias, comment)
Tim Peters182b5ac2004-07-18 06:16:08 +0000131
Jack Jansen43935122001-04-07 12:53:45 +0000132def _setcomment(object_alias, comment):
Jack Jansen0ae32202003-04-09 13:25:43 +0000133 finder = _getfinder()
134 args = {}
135 attrs = {}
136 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
137 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00)
138 args['----'] = aeobj_01
139 args["data"] = comment
140 _reply, args, attrs = finder.send("core", "setd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000141 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000142 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000143 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000144 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000145
146def _getcomment(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000147 finder = _getfinder()
148 args = {}
149 attrs = {}
150 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None)
151 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00)
152 args['----'] = aeobj_01
153 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000154 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000155 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000156 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000157 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000158
159
160#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000161# Get information about current processes in the Finder.
Jack Jansen43935122001-04-07 12:53:45 +0000162
163def processes():
Jack Jansen0ae32202003-04-09 13:25:43 +0000164 """processes returns a list of all active processes running on this computer and their creators."""
165 finder = _getfinder()
166 args = {}
167 attrs = {}
168 processnames = []
169 processnumbers = []
170 creators = []
171 partitions = []
172 used = []
173 ## get the processnames or else the processnumbers
174 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None)
175 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000176 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000177 raise Error(aetools.decodeerror(args))
Jack Jansen0ae32202003-04-09 13:25:43 +0000178 p = []
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000179 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000180 p = args['----']
181 for proc in p:
182 if hasattr(proc, 'seld'):
183 # it has a real name
184 processnames.append(proc.seld)
185 elif hasattr(proc, 'type'):
186 if proc.type == "psn ":
187 # it has a process number
188 processnumbers.append(proc.data)
189 ## get the creators
190 args = {}
191 attrs = {}
192 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None)
193 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fcrt'), fr=aeobj_0)
194 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000195 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000196 raise Error(aetools.decodeerror(_arg))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000197 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000198 p = args['----']
199 creators = p[:]
200 ## concatenate in one dict
201 result = []
202 if len(processnames) > len(processnumbers):
203 data = processnames
204 else:
205 data = processnumbers
206 for i in range(len(creators)):
207 result.append((data[i], creators[i]))
208 return result
Jack Jansen43935122001-04-07 12:53:45 +0000209
210class _process:
Jack Jansen0ae32202003-04-09 13:25:43 +0000211 pass
Jack Jansen43935122001-04-07 12:53:45 +0000212
213def isactiveprocess(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000214 """Check of processname is active. MacOS9"""
215 all = processes()
216 ok = 0
217 for n, c in all:
218 if n == processname:
219 return 1
220 return 0
Tim Peters182b5ac2004-07-18 06:16:08 +0000221
Jack Jansen43935122001-04-07 12:53:45 +0000222def processinfo(processname):
Jack Jansen0ae32202003-04-09 13:25:43 +0000223 """Return an object with all process properties as attributes for processname. MacOS9"""
224 p = _process()
Tim Peters182b5ac2004-07-18 06:16:08 +0000225
Jack Jansen0ae32202003-04-09 13:25:43 +0000226 if processname == "Finder":
227 p.partition = None
228 p.used = None
229 else:
230 p.partition = _processproperty(processname, 'appt')
231 p.used = _processproperty(processname, 'pusd')
232 p.visible = _processproperty(processname, 'pvis') #Is the process' layer visible?
233 p.frontmost = _processproperty(processname, 'pisf') #Is the process the frontmost process?
234 p.file = _processproperty(processname, 'file') #the file from which the process was launched
235 p.filetype = _processproperty(processname, 'asty') #the OSType of the file type of the process
236 p.creatortype = _processproperty(processname, 'fcrt') #the OSType of the creator of the process (the signature)
237 p.accepthighlevel = _processproperty(processname, 'revt') #Is the process high-level event aware (accepts open application, open document, print document, and quit)?
238 p.hasscripting = _processproperty(processname, 'hscr') #Does the process have a scripting terminology, i.e., can it be scripted?
239 return p
Tim Peters182b5ac2004-07-18 06:16:08 +0000240
Jack Jansen43935122001-04-07 12:53:45 +0000241def _processproperty(processname, property):
Jack Jansen0ae32202003-04-09 13:25:43 +0000242 """return the partition size and memory used for processname"""
243 finder = _getfinder()
244 args = {}
245 attrs = {}
246 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="name", seld=processname, fr=None)
247 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type(property), fr=aeobj_00)
248 args['----'] = aeobj_01
249 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000250 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000251 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000252 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000253 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000254
255
256#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000257# Mess around with Finder windows.
Tim Peters182b5ac2004-07-18 06:16:08 +0000258
Jack Jansen43935122001-04-07 12:53:45 +0000259def openwindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000260 """Open a Finder window for object, Specify object by name or fsspec."""
261 finder = _getfinder()
262 object = Carbon.File.FSRef(object)
263 object_alias = object.FSNewAliasMinimal()
264 args = {}
265 attrs = {}
266 _code = 'aevt'
267 _subcode = 'odoc'
268 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
269 args['----'] = aeobj_0
270 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000271 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000272 raise Error(aetools.decodeerror(args))
Tim Peters182b5ac2004-07-18 06:16:08 +0000273
Jack Jansen43935122001-04-07 12:53:45 +0000274def closewindow(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000275 """Close a Finder window for folder, Specify by path."""
276 finder = _getfinder()
277 object = Carbon.File.FSRef(object)
278 object_alias = object.FSNewAliasMinimal()
279 args = {}
280 attrs = {}
281 _code = 'core'
282 _subcode = 'clos'
283 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None)
284 args['----'] = aeobj_0
285 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000286 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000287 raise Error(aetools.decodeerror(args))
Jack Jansen43935122001-04-07 12:53:45 +0000288
289def location(object, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000290 """Set the position of a Finder window for folder to pos=(w, h). Specify file by name or fsspec.
291 If pos=None, location will return the current position of the object."""
292 object = Carbon.File.FSRef(object)
293 object_alias = object.FSNewAliasMinimal()
294 if not pos:
295 return _getlocation(object_alias)
296 return _setlocation(object_alias, pos)
Tim Peters182b5ac2004-07-18 06:16:08 +0000297
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000298def _setlocation(object_alias, location):
Jack Jansen0ae32202003-04-09 13:25:43 +0000299 """_setlocation: Set the location of the icon for the object."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000300 x, y = location
Jack Jansen0ae32202003-04-09 13:25:43 +0000301 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000309 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000310 raise Error(aetools.decodeerror(args))
Jack Jansen0ae32202003-04-09 13:25:43 +0000311 return (x,y)
Tim Peters182b5ac2004-07-18 06:16:08 +0000312
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000322 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000323 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000324 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000325 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000337
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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000347 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000348 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000349 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000350 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000366 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000367 raise Error(aetools.decodeerror(args))
Jack Jansen0ae32202003-04-09 13:25:43 +0000368 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000381
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()
Tim Peters182b5ac2004-07-18 06:16:08 +0000393 aeobj_0 = aetypes.ObjectSpecifier(want = aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000394 form = 'alis', seld = folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000395 aeobj_1 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000396 form = 'prop', seld = aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000397 aeobj_2 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000398 form = 'prop', seld = aetypes.Type('pvew'), fr=aeobj_1)
Tim Peters182b5ac2004-07-18 06:16:08 +0000399 aeobj_3 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000400 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000406 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000407 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000408 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000409 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)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000423 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000424 raise Error(aetools.decodeerror(args))
Jack Jansen0ae32202003-04-09 13:25:43 +0000425 views = {'iimg':0, 'pnam':1, 'lgbu':2}
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000426 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000427 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)
Tim Peters182b5ac2004-07-18 06:16:08 +0000440
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000441def _setwindowsize(folder_alias, size):
Jack Jansen0ae32202003-04-09 13:25:43 +0000442 """Set the size of a Finder window for folder to (w, h)"""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000443 w, h = size
Jack Jansen0ae32202003-04-09 13:25:43 +0000444 finder = _getfinder()
445 args = {}
446 attrs = {}
447 _code = 'core'
448 _subcode = 'setd'
449 aevar00 = [w, h]
450 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
451 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000452 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000453 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000454 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000455 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
456 args['----'] = aeobj_2
457 args["data"] = aevar00
458 _reply, args, attrs = finder.send(_code, _subcode, args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000459 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000460 raise Error(aetools.decodeerror(args))
Jack Jansen0ae32202003-04-09 13:25:43 +0000461 return (w, h)
Tim Peters182b5ac2004-07-18 06:16:08 +0000462
Jack Jansen43935122001-04-07 12:53:45 +0000463def _getwindowsize(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000464 """Set the size of a Finder window for folder to (w, h)"""
465 finder = _getfinder()
466 args = {}
467 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000468 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000469 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000470 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000471 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000472 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000473 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
474 args['----'] = aeobj_2
475 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000476 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000477 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000478 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000479 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000480
481def windowposition(folder, pos=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000482 """Set the position of a Finder window for folder to pos=(w, h)."""
483 fsr = Carbon.File.FSRef(folder)
484 folder_alias = fsr.FSNewAliasMinimal()
485 openwindow(fsr)
486 if not pos:
487 return _getwindowposition(folder_alias)
Georg Brandl57b39e02007-04-11 19:24:50 +0000488 if aetypes.IsQDPoint(pos):
489 # QDPoint object as returned by _getwindowposition
Jack Jansen0ae32202003-04-09 13:25:43 +0000490 pos = (pos.h, pos.v)
491 return _setwindowposition(folder_alias, pos)
Tim Peters182b5ac2004-07-18 06:16:08 +0000492
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000493def _setwindowposition(folder_alias, position):
Jack Jansen0ae32202003-04-09 13:25:43 +0000494 """Set the size of a Finder window for folder to (w, h)."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000495 x, y = position
Jack Jansen0ae32202003-04-09 13:25:43 +0000496 finder = _getfinder()
497 args = {}
498 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000499 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000500 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000501 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000502 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000503 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000504 form="prop", seld=aetypes.Type('posn'), fr=aeobj_1)
505 args['----'] = aeobj_2
506 args["data"] = [x, y]
507 _reply, args, attrs = finder.send('core', 'setd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000508 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000509 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000510 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000511 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000512
513def _getwindowposition(folder_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000514 """Get the size of a Finder window for folder, Specify by path."""
515 finder = _getfinder()
516 args = {}
517 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000518 aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000519 form="alis", seld=folder_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000520 aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000521 form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0)
Tim Peters182b5ac2004-07-18 06:16:08 +0000522 aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000523 form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1)
524 args['----'] = aeobj_2
525 _reply, args, attrs = finder.send('core', 'getd', args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000526 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000527 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000528 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000529 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000530
531def icon(object, icondata=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000532 """icon sets the icon of object, if no icondata is given,
533 icon will return an AE object with binary data for the current icon.
534 If left untouched, this data can be used to paste the icon on another file.
535 Development opportunity: get and set the data as PICT."""
536 fsr = Carbon.File.FSRef(object)
537 object_alias = fsr.FSNewAliasMinimal()
538 if icondata == None:
539 return _geticon(object_alias)
540 return _seticon(object_alias, icondata)
Tim Peters182b5ac2004-07-18 06:16:08 +0000541
Jack Jansen43935122001-04-07 12:53:45 +0000542def _geticon(object_alias):
Jack Jansen0ae32202003-04-09 13:25:43 +0000543 """get the icondata for object. Binary data of some sort."""
544 finder = _getfinder()
545 args = {}
546 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000547 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000548 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000549 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000550 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
551 args['----'] = aeobj_01
552 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000553 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000554 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000555 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000556 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000557
558def _seticon(object_alias, icondata):
Jack Jansen0ae32202003-04-09 13:25:43 +0000559 """set the icondata for object, formatted as produced by _geticon()"""
560 finder = _getfinder()
561 args = {}
562 attrs = {}
Tim Peters182b5ac2004-07-18 06:16:08 +0000563 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000564 form="alis", seld=object_alias, fr=None)
Tim Peters182b5ac2004-07-18 06:16:08 +0000565 aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'),
Jack Jansen0ae32202003-04-09 13:25:43 +0000566 form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00)
567 args['----'] = aeobj_01
568 args["data"] = icondata
569 _reply, args, attrs = finder.send("core", "setd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000570 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000571 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000572 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000573 return args['----'].data
Jack Jansen43935122001-04-07 12:53:45 +0000574
575
576#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000577# Volumes and servers.
Tim Peters182b5ac2004-07-18 06:16:08 +0000578
Jack Jansen43935122001-04-07 12:53:45 +0000579def mountvolume(volume, server=None, username=None, password=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000580 """mount a volume, local or on a server on AppleTalk.
581 Note: mounting a ASIP server requires a different operation.
582 server is the name of the server where the volume belongs
583 username, password belong to a registered user of the volume."""
584 finder = _getfinder()
585 args = {}
586 attrs = {}
587 if password:
588 args["PASS"] = password
589 if username:
590 args["USER"] = username
591 if server:
592 args["SRVR"] = server
593 args['----'] = volume
594 _reply, args, attrs = finder.send("aevt", "mvol", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000595 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000596 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000597 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000598 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000599
600def unmountvolume(volume):
Jack Jansen0ae32202003-04-09 13:25:43 +0000601 """unmount a volume that's on the desktop"""
602 putaway(volume)
Tim Peters182b5ac2004-07-18 06:16:08 +0000603
Jack Jansen43935122001-04-07 12:53:45 +0000604def putaway(object):
Jack Jansen0ae32202003-04-09 13:25:43 +0000605 """puth the object away, whereever it came from."""
606 finder = _getfinder()
607 args = {}
608 attrs = {}
609 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('cdis'), form="name", seld=object, fr=None)
610 _reply, args, attrs = talker.send("fndr", "ptwy", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000611 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000612 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000613 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000614 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000615
616
617#---------------------------------------------------
Jack Jansen0ae32202003-04-09 13:25:43 +0000618# Miscellaneous functions
Jack Jansen43935122001-04-07 12:53:45 +0000619#
620
621def volumelevel(level):
Jack Jansen0ae32202003-04-09 13:25:43 +0000622 """set the audio output level, parameter between 0 (silent) and 7 (full blast)"""
623 finder = _getfinder()
624 args = {}
625 attrs = {}
626 if level < 0:
627 level = 0
628 elif level > 7:
629 level = 7
630 args['----'] = level
631 _reply, args, attrs = finder.send("aevt", "stvl", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000632 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000633 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000634 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000635 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000636
637def OSversion():
Jack Jansen0ae32202003-04-09 13:25:43 +0000638 """return the version of the system software"""
639 finder = _getfinder()
640 args = {}
641 attrs = {}
642 aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('ver2'), fr=None)
643 args['----'] = aeobj_00
644 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000645 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000646 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000647 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000648 return args['----']
Jack Jansen43935122001-04-07 12:53:45 +0000649
650def filesharing():
Jack Jansen0ae32202003-04-09 13:25:43 +0000651 """return the current status of filesharing and whether it is starting up or not:
652 -1 file sharing is off and not starting up
653 0 file sharing is off and starting up
654 1 file sharing is on"""
655 status = -1
656 finder = _getfinder()
657 # see if it is on
658 args = {}
659 attrs = {}
660 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fshr'), fr=None)
661 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000662 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000663 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000664 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000665 if args['----'] == 0:
666 status = -1
667 else:
668 status = 1
669 # is it starting up perchance?
670 args = {}
671 attrs = {}
672 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fsup'), fr=None)
673 _reply, args, attrs = finder.send("core", "getd", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000674 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000675 raise Error(aetools.decodeerror(args))
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000676 if '----' in args:
Jack Jansen0ae32202003-04-09 13:25:43 +0000677 if args['----'] == 1:
678 status = 0
679 return status
Tim Peters182b5ac2004-07-18 06:16:08 +0000680
Jack Jansen43935122001-04-07 12:53:45 +0000681def movetotrash(path):
Jack Jansen0ae32202003-04-09 13:25:43 +0000682 """move the object to the trash"""
683 fss = Carbon.File.FSSpec(path)
684 trashfolder = Carbon.Folder.FSFindFolder(fss.as_tuple()[0], 'trsh', 0)
685 move(path, trashfolder)
Jack Jansen43935122001-04-07 12:53:45 +0000686
687def emptytrash():
Jack Jansen0ae32202003-04-09 13:25:43 +0000688 """empty the trash"""
689 finder = _getfinder()
690 args = {}
691 attrs = {}
692 args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('trsh'), fr=None)
693 _reply, args, attrs = finder.send("fndr", "empt", args, attrs)
Neal Norwitzf1a69c12006-08-20 16:25:10 +0000694 if 'errn' in args:
Collin Wintere45be282007-08-23 00:01:55 +0000695 raise aetools.Error(aetools.decodeerror(args))
Jack Jansen43935122001-04-07 12:53:45 +0000696
697
698def _test():
Jack Jansen0ae32202003-04-09 13:25:43 +0000699 import EasyDialogs
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000700 print('Original findertools functionality test...')
701 print('Testing launch...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000702 pathname = EasyDialogs.AskFileForOpen('File to launch:')
703 if pathname:
704 result = launch(pathname)
705 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000706 print('Result: ', result)
707 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000708 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000709 print('Testing print...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000710 pathname = EasyDialogs.AskFileForOpen('File to print:')
711 if pathname:
712 result = Print(pathname)
713 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000714 print('Result: ', result)
715 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000716 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000717 print('Testing copy...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000718 pathname = EasyDialogs.AskFileForOpen('File to copy:')
719 if pathname:
720 destdir = EasyDialogs.AskFolder('Destination:')
721 if destdir:
722 result = copy(pathname, destdir)
723 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000724 print('Result:', result)
725 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000726 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000727 print('Testing move...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000728 pathname = EasyDialogs.AskFileForOpen('File to move:')
729 if pathname:
730 destdir = EasyDialogs.AskFolder('Destination:')
731 if destdir:
732 result = move(pathname, destdir)
733 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000734 print('Result:', result)
735 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000736 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000737 print('Testing sleep...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000738 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
739 result = sleep()
740 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000741 print('Result:', result)
742 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000743 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000744 print('Testing shutdown...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000745 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
746 result = shutdown()
747 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000748 print('Result:', result)
749 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000750 sys.stdin.readline()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000751 print('Testing restart...')
Jack Jansen0ae32202003-04-09 13:25:43 +0000752 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
753 result = restart()
754 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000755 print('Result:', result)
756 print('Press return-', end=' ')
Jack Jansen0ae32202003-04-09 13:25:43 +0000757 sys.stdin.readline()
Jack Jansen43935122001-04-07 12:53:45 +0000758
759def _test2():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000760 print('\nmorefindertools version %s\nTests coming up...' %__version__)
Jack Jansen0ae32202003-04-09 13:25:43 +0000761 import os
762 import random
Jack Jansen43935122001-04-07 12:53:45 +0000763
Jack Jansen0ae32202003-04-09 13:25:43 +0000764 # miscellaneous
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000765 print('\tfilesharing on?', filesharing()) # is file sharing on, off, starting up?
766 print('\tOS version', OSversion()) # the version of the system software
Jack Jansen43935122001-04-07 12:53:45 +0000767
Jack Jansen0ae32202003-04-09 13:25:43 +0000768 # set the soundvolume in a simple way
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000769 print('\tSystem beep volume')
Jack Jansen0ae32202003-04-09 13:25:43 +0000770 for i in range(0, 7):
Tim Peters182b5ac2004-07-18 06:16:08 +0000771 volumelevel(i)
Jack Jansen0ae32202003-04-09 13:25:43 +0000772 MacOS.SysBeep()
Jack Jansen43935122001-04-07 12:53:45 +0000773
Jack Jansen0ae32202003-04-09 13:25:43 +0000774 # Finder's windows, file location, file attributes
775 open("@findertoolstest", "w")
776 f = "@findertoolstest"
777 reveal(f) # reveal this file in a Finder window
778 select(f) # select this file
Jack Jansen43935122001-04-07 12:53:45 +0000779
Jack Jansen0ae32202003-04-09 13:25:43 +0000780 base, file = os.path.split(f)
781 closewindow(base) # close the window this file is in (opened by reveal)
782 openwindow(base) # open it again
783 windowview(base, 1) # set the view by list
Jack Jansen43935122001-04-07 12:53:45 +0000784
Jack Jansen0ae32202003-04-09 13:25:43 +0000785 label(f, 2) # set the label of this file to something orange
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000786 print('\tlabel', label(f)) # get the label of this file
Jack Jansen43935122001-04-07 12:53:45 +0000787
Jack Jansen0ae32202003-04-09 13:25:43 +0000788 # the file location only works in a window with icon view!
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000789 print('Random locations for an icon')
Jack Jansen0ae32202003-04-09 13:25:43 +0000790 windowview(base, 0) # set the view by icon
791 windowsize(base, (600, 600))
792 for i in range(50):
793 location(f, (random.randint(10, 590), random.randint(10, 590)))
Jack Jansen43935122001-04-07 12:53:45 +0000794
Jack Jansen0ae32202003-04-09 13:25:43 +0000795 windowsize(base, (200, 400))
796 windowview(base, 1) # set the view by icon
Jack Jansen43935122001-04-07 12:53:45 +0000797
Jack Jansen0ae32202003-04-09 13:25:43 +0000798 orgpos = windowposition(base)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000799 print('Animated window location')
Jack Jansen0ae32202003-04-09 13:25:43 +0000800 for i in range(10):
801 pos = (100+i*10, 100+i*10)
802 windowposition(base, pos)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000803 print('\twindow position', pos)
Jack Jansen0ae32202003-04-09 13:25:43 +0000804 windowposition(base, orgpos) # park it where it was before
Jack Jansen43935122001-04-07 12:53:45 +0000805
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000806 print('Put a comment in file', f, ':')
807 print('\t', comment(f)) # print the Finder comment this file has
Jack Jansen0ae32202003-04-09 13:25:43 +0000808 s = 'This is a comment no one reads!'
809 comment(f, s) # set the Finder comment
Tim Peters182b5ac2004-07-18 06:16:08 +0000810
Jack Jansen43935122001-04-07 12:53:45 +0000811def _test3():
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000812 print('MacOS9 or better specific functions')
Jack Jansen0ae32202003-04-09 13:25:43 +0000813 # processes
814 pr = processes() # return a list of tuples with (active_processname, creatorcode)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000815 print('Return a list of current active processes:')
Jack Jansen0ae32202003-04-09 13:25:43 +0000816 for p in pr:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000817 print('\t', p)
Tim Peters182b5ac2004-07-18 06:16:08 +0000818
Jack Jansen0ae32202003-04-09 13:25:43 +0000819 # get attributes of the first process in the list
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000820 print('Attributes of the first process in the list:')
Jack Jansen0ae32202003-04-09 13:25:43 +0000821 pinfo = processinfo(pr[0][0])
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000822 print('\t', pr[0][0])
823 print('\t\tmemory partition', pinfo.partition) # the memory allocated to this process
824 print('\t\tmemory used', pinfo.used) # the memory actuall used by this process
825 print('\t\tis visible', pinfo.visible) # is the process visible to the user
826 print('\t\tis frontmost', pinfo.frontmost) # is the process the front most one?
827 print('\t\thas scripting', pinfo.hasscripting) # is the process scriptable?
828 print('\t\taccepts high level events', pinfo.accepthighlevel) # does the process accept high level appleevents?
Jack Jansen43935122001-04-07 12:53:45 +0000829
Jack Jansen0585d411996-09-20 15:26:20 +0000830if __name__ == '__main__':
Jack Jansen0ae32202003-04-09 13:25:43 +0000831 _test()
832 _test2()
833 _test3()