Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 1 | """Utility routines depending on the finder, |
| 2 | a combination of code by Jack Jansen and erik@letterror.com. |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 3 | |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 4 | Most events have been captured from |
| 5 | Lasso Capture AE and than translated to python code. |
| 6 | |
| 7 | IMPORTANT |
| 8 | Note that the processes() function returns different values |
| 9 | depending on the OS version it is running on. On MacOS 9 |
| 10 | the Finder returns the process *names* which can then be |
| 11 | used to find out more about them. On MacOS 8.6 and earlier |
| 12 | the Finder returns a code which does not seem to work. |
| 13 | So bottom line: the processes() stuff does not work on < MacOS9 |
| 14 | |
| 15 | Mostly written by erik@letterror.com |
| 16 | """ |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 17 | import Finder |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 18 | from Carbon import AppleEvents |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 19 | import aetools |
| 20 | import MacOS |
| 21 | import sys |
| 22 | import macfs |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 23 | import aetypes |
| 24 | from types import * |
| 25 | |
| 26 | __version__ = '1.1' |
| 27 | Error = 'findertools.Error' |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 28 | |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 29 | _finder_talker = None |
| 30 | |
| 31 | def _getfinder(): |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 32 | """returns basic (recyclable) Finder AE interface object""" |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 33 | global _finder_talker |
| 34 | if not _finder_talker: |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 35 | _finder_talker = Finder.Finder() |
Jack Jansen | 0e0d3e7 | 1998-10-15 14:02:54 +0000 | [diff] [blame] | 36 | _finder_talker.send_flags = ( _finder_talker.send_flags | |
| 37 | AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 38 | return _finder_talker |
| 39 | |
| 40 | def launch(file): |
| 41 | """Open a file thru the finder. Specify file by name or fsspec""" |
| 42 | finder = _getfinder() |
| 43 | fss = macfs.FSSpec(file) |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 44 | return finder.open(fss) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 45 | |
| 46 | def Print(file): |
| 47 | """Print a file thru the finder. Specify file by name or fsspec""" |
| 48 | finder = _getfinder() |
| 49 | fss = macfs.FSSpec(file) |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 50 | return finder._print(fss) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 51 | |
| 52 | def copy(src, dstdir): |
| 53 | """Copy a file to a folder""" |
| 54 | finder = _getfinder() |
Jack Jansen | fdd2269 | 2000-09-22 12:17:14 +0000 | [diff] [blame] | 55 | if type(src) == type([]): |
| 56 | src_fss = [] |
| 57 | for s in src: |
| 58 | src_fss.append(macfs.FSSpec(s)) |
| 59 | else: |
| 60 | src_fss = macfs.FSSpec(src) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 61 | dst_fss = macfs.FSSpec(dstdir) |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 62 | return finder.duplicate(src_fss, to=dst_fss) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 63 | |
| 64 | def move(src, dstdir): |
| 65 | """Move a file to a folder""" |
| 66 | finder = _getfinder() |
Jack Jansen | fdd2269 | 2000-09-22 12:17:14 +0000 | [diff] [blame] | 67 | if type(src) == type([]): |
| 68 | src_fss = [] |
| 69 | for s in src: |
| 70 | src_fss.append(macfs.FSSpec(s)) |
| 71 | else: |
| 72 | src_fss = macfs.FSSpec(src) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 73 | dst_fss = macfs.FSSpec(dstdir) |
Jack Jansen | 8bcd471 | 2000-08-20 19:56:13 +0000 | [diff] [blame] | 74 | return finder.move(src_fss, to=dst_fss) |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 75 | |
| 76 | def sleep(): |
| 77 | """Put the mac to sleep""" |
| 78 | finder = _getfinder() |
| 79 | finder.sleep() |
| 80 | |
| 81 | def shutdown(): |
| 82 | """Shut the mac down""" |
| 83 | finder = _getfinder() |
| 84 | finder.shut_down() |
| 85 | |
| 86 | def restart(): |
| 87 | """Restart the mac""" |
| 88 | finder = _getfinder() |
| 89 | finder.restart() |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 90 | |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 91 | |
| 92 | #--------------------------------------------------- |
| 93 | # Additional findertools |
| 94 | # |
| 95 | |
| 96 | def reveal(file): |
| 97 | """Reveal a file in the finder. Specify file by name or fsspec.""" |
| 98 | finder = _getfinder() |
| 99 | fss = macfs.FSSpec(file) |
| 100 | file_alias = fss.NewAlias() |
| 101 | return finder.reveal(file_alias) |
| 102 | |
| 103 | def select(file): |
| 104 | """select a file in the finder. Specify file by name or fsspec.""" |
| 105 | finder = _getfinder() |
| 106 | fss = macfs.FSSpec(file) |
| 107 | file_alias = fss.NewAlias() |
| 108 | return finder.select(file_alias) |
| 109 | |
| 110 | def update(file): |
| 111 | """Update the display of the specified object(s) to match |
| 112 | their on-disk representation. Specify file by name or fsspec.""" |
| 113 | finder = _getfinder() |
| 114 | fss = macfs.FSSpec(file) |
| 115 | file_alias = fss.NewAlias() |
| 116 | return finder.update(file_alias) |
| 117 | |
| 118 | |
| 119 | #--------------------------------------------------- |
| 120 | # More findertools |
| 121 | # |
| 122 | |
| 123 | def comment(object, comment=None): |
Jack Jansen | 6f84ed5 | 2001-05-17 12:45:13 +0000 | [diff] [blame] | 124 | """comment: get or set the Finder-comment of the item, displayed in the Get Info window.""" |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 125 | object = macfs.FSSpec(object) |
| 126 | fss = macfs.FSSpec(object) |
| 127 | object_alias = fss.NewAlias() |
| 128 | if comment == None: |
| 129 | return _getcomment(object_alias) |
| 130 | else: |
| 131 | return _setcomment(object_alias, comment) |
| 132 | |
| 133 | def _setcomment(object_alias, comment): |
| 134 | finder = _getfinder() |
| 135 | args = {} |
| 136 | attrs = {} |
| 137 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None) |
| 138 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00) |
| 139 | args['----'] = aeobj_01 |
| 140 | args["data"] = comment |
| 141 | _reply, args, attrs = finder.send("core", "setd", args, attrs) |
| 142 | if args.has_key('errn'): |
| 143 | raise Error, aetools.decodeerror(args) |
| 144 | if args.has_key('----'): |
| 145 | return args['----'] |
| 146 | |
| 147 | def _getcomment(object_alias): |
| 148 | finder = _getfinder() |
| 149 | args = {} |
| 150 | attrs = {} |
| 151 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), form="alis", seld=object_alias, fr=None) |
| 152 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('comt'), fr=aeobj_00) |
| 153 | args['----'] = aeobj_01 |
| 154 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 155 | if args.has_key('errn'): |
| 156 | raise Error, aetools.decodeerror(args) |
| 157 | if args.has_key('----'): |
| 158 | return args['----'] |
| 159 | |
| 160 | |
| 161 | #--------------------------------------------------- |
| 162 | # Get information about current processes in the Finder. |
| 163 | |
| 164 | def processes(): |
| 165 | """processes returns a list of all active processes running on this computer and their creators.""" |
| 166 | finder = _getfinder() |
| 167 | args = {} |
| 168 | attrs = {} |
| 169 | processnames = [] |
| 170 | processnumbers = [] |
| 171 | creators = [] |
| 172 | partitions = [] |
| 173 | used = [] |
| 174 | ## get the processnames or else the processnumbers |
| 175 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None) |
| 176 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
| 177 | if args.has_key('errn'): |
| 178 | raise Error, aetools.decodeerror(args) |
| 179 | p = [] |
| 180 | if args.has_key('----'): |
| 181 | p = args['----'] |
| 182 | for proc in p: |
| 183 | if hasattr(proc, 'seld'): |
| 184 | # it has a real name |
| 185 | processnames.append(proc.seld) |
| 186 | elif hasattr(proc, 'type'): |
| 187 | if proc.type == "psn ": |
| 188 | # it has a process number |
| 189 | processnumbers.append(proc.data) |
| 190 | ## get the creators |
| 191 | args = {} |
| 192 | attrs = {} |
| 193 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="indx", seld=aetypes.Unknown('abso', "all "), fr=None) |
| 194 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fcrt'), fr=aeobj_0) |
| 195 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
| 196 | if args.has_key('errn'): |
| 197 | raise Error, aetools.decodeerror(_arg) |
| 198 | if args.has_key('----'): |
| 199 | p = args['----'] |
| 200 | creators = p[:] |
| 201 | ## concatenate in one dict |
| 202 | result = [] |
| 203 | if len(processnames) > len(processnumbers): |
| 204 | data = processnames |
| 205 | else: |
| 206 | data = processnumbers |
| 207 | for i in range(len(creators)): |
| 208 | result.append((data[i], creators[i])) |
| 209 | return result |
| 210 | |
| 211 | class _process: |
| 212 | pass |
| 213 | |
| 214 | def isactiveprocess(processname): |
| 215 | """Check of processname is active. MacOS9""" |
| 216 | all = processes() |
| 217 | ok = 0 |
| 218 | for n, c in all: |
| 219 | if n == processname: |
| 220 | return 1 |
| 221 | return 0 |
| 222 | |
| 223 | def processinfo(processname): |
| 224 | """Return an object with all process properties as attributes for processname. MacOS9""" |
| 225 | p = _process() |
| 226 | |
| 227 | if processname == "Finder": |
| 228 | p.partition = None |
| 229 | p.used = None |
| 230 | else: |
| 231 | p.partition = _processproperty(processname, 'appt') |
| 232 | p.used = _processproperty(processname, 'pusd') |
| 233 | p.visible = _processproperty(processname, 'pvis') #Is the process' layer visible? |
| 234 | p.frontmost = _processproperty(processname, 'pisf') #Is the process the frontmost process? |
| 235 | p.file = _processproperty(processname, 'file') #the file from which the process was launched |
| 236 | p.filetype = _processproperty(processname, 'asty') #the OSType of the file type of the process |
| 237 | p.creatortype = _processproperty(processname, 'fcrt') #the OSType of the creator of the process (the signature) |
| 238 | p.accepthighlevel = _processproperty(processname, 'revt') #Is the process high-level event aware (accepts open application, open document, print document, and quit)? |
| 239 | p.hasscripting = _processproperty(processname, 'hscr') #Does the process have a scripting terminology, i.e., can it be scripted? |
| 240 | return p |
| 241 | |
| 242 | def _processproperty(processname, property): |
| 243 | """return the partition size and memory used for processname""" |
| 244 | finder = _getfinder() |
| 245 | args = {} |
| 246 | attrs = {} |
| 247 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prcs'), form="name", seld=processname, fr=None) |
| 248 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type(property), fr=aeobj_00) |
| 249 | args['----'] = aeobj_01 |
| 250 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 251 | if args.has_key('errn'): |
| 252 | raise Error, aetools.decodeerror(args) |
| 253 | if args.has_key('----'): |
| 254 | return args['----'] |
| 255 | |
| 256 | |
| 257 | #--------------------------------------------------- |
| 258 | # Mess around with Finder windows. |
| 259 | |
| 260 | def openwindow(object): |
| 261 | """Open a Finder window for object, Specify object by name or fsspec.""" |
| 262 | finder = _getfinder() |
| 263 | object = macfs.FSSpec(object) |
| 264 | fss = macfs.FSSpec(object) |
| 265 | object_alias = fss.NewAlias() |
| 266 | args = {} |
| 267 | attrs = {} |
| 268 | _code = 'aevt' |
| 269 | _subcode = 'odoc' |
| 270 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
| 271 | args['----'] = aeobj_0 |
| 272 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
| 273 | if args.has_key('errn'): |
| 274 | raise Error, aetools.decodeerror(args) |
| 275 | |
| 276 | def closewindow(object): |
| 277 | """Close a Finder window for folder, Specify by path.""" |
| 278 | finder = _getfinder() |
| 279 | fss = macfs.FSSpec(object) |
| 280 | object_alias = fss.NewAlias() |
| 281 | args = {} |
| 282 | attrs = {} |
| 283 | _code = 'core' |
| 284 | _subcode = 'clos' |
| 285 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), form="alis", seld=object_alias, fr=None) |
| 286 | args['----'] = aeobj_0 |
| 287 | _reply, args, attrs = finder.send(_code, _subcode, args, attrs) |
| 288 | if args.has_key('errn'): |
| 289 | raise Error, aetools.decodeerror(args) |
| 290 | |
| 291 | def location(object, pos=None): |
| 292 | """Set the position of a Finder window for folder to pos=(w, h). Specify file by name or fsspec. |
| 293 | If pos=None, location will return the current position of the object.""" |
| 294 | fss = macfs.FSSpec(object) |
| 295 | object_alias = fss.NewAlias() |
| 296 | if not pos: |
| 297 | return _getlocation(object_alias) |
| 298 | return _setlocation(object_alias, pos) |
| 299 | |
| 300 | def _setlocation(object_alias, (x, y)): |
| 301 | """_setlocation: Set the location of the icon for the object.""" |
| 302 | 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) |
| 310 | if args.has_key('errn'): |
| 311 | raise Error, aetools.decodeerror(args) |
| 312 | return (x,y) |
| 313 | |
| 314 | def _getlocation(object_alias): |
| 315 | """_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) |
| 323 | if args.has_key('errn'): |
| 324 | raise Error, aetools.decodeerror(args) |
| 325 | if args.has_key('----'): |
| 326 | pos = args['----'] |
| 327 | return pos.h, pos.v |
| 328 | |
| 329 | def label(object, index=None): |
| 330 | """label: set or get the label of the item. Specify file by name or fsspec.""" |
| 331 | fss = macfs.FSSpec(object) |
| 332 | object_alias = fss.NewAlias() |
| 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) |
| 338 | |
| 339 | def _getlabel(object_alias): |
| 340 | """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) |
| 348 | if args.has_key('errn'): |
| 349 | raise Error, aetools.decodeerror(args) |
| 350 | if args.has_key('----'): |
| 351 | return args['----'] |
| 352 | |
| 353 | def _setlabel(object_alias, index): |
| 354 | """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) |
| 367 | if args.has_key('errn'): |
| 368 | raise Error, aetools.decodeerror(args) |
| 369 | return index |
| 370 | |
| 371 | def windowview(folder, view=None): |
| 372 | """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 | fss = macfs.FSSpec(folder) |
| 378 | folder_alias = fss.NewAlias() |
| 379 | if view == None: |
| 380 | return _getwindowview(folder_alias) |
| 381 | return _setwindowview(folder_alias, view) |
| 382 | |
| 383 | def _setwindowview(folder_alias, view=0): |
| 384 | """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() |
| 394 | aeobj_0 = aetypes.ObjectSpecifier(want = aetypes.Type('cfol'), |
| 395 | form = 'alis', seld = folder_alias, fr=None) |
| 396 | aeobj_1 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
| 397 | form = 'prop', seld = aetypes.Type('cwnd'), fr=aeobj_0) |
| 398 | aeobj_2 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
| 399 | form = 'prop', seld = aetypes.Type('pvew'), fr=aeobj_1) |
| 400 | aeobj_3 = aetypes.ObjectSpecifier(want = aetypes.Type('prop'), |
| 401 | 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) |
| 407 | if args.has_key('errn'): |
| 408 | raise Error, aetools.decodeerror(args) |
| 409 | if args.has_key('----'): |
| 410 | return args['----'] |
| 411 | |
| 412 | def _getwindowview(folder_alias): |
| 413 | """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) |
| 424 | if args.has_key('errn'): |
| 425 | raise Error, aetools.decodeerror(args) |
| 426 | views = {'iimg':0, 'pnam':1, 'lgbu':2} |
| 427 | if args.has_key('----'): |
| 428 | return views[args['----'].enum] |
| 429 | |
| 430 | def windowsize(folder, size=None): |
| 431 | """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 | fss = macfs.FSSpec(folder) |
| 436 | folder_alias = fss.NewAlias() |
| 437 | openwindow(fss) |
| 438 | if not size: |
| 439 | return _getwindowsize(folder_alias) |
| 440 | return _setwindowsize(folder_alias, size) |
| 441 | |
| 442 | def _setwindowsize(folder_alias, (w, h)): |
| 443 | """Set the size of a Finder window for folder to (w, h)""" |
| 444 | 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) |
| 452 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 453 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
| 454 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 455 | 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) |
| 459 | if args.has_key('errn'): |
| 460 | raise Error, aetools.decodeerror(args) |
| 461 | return (w, h) |
| 462 | |
| 463 | def _getwindowsize(folder_alias): |
| 464 | """Set the size of a Finder window for folder to (w, h)""" |
| 465 | finder = _getfinder() |
| 466 | args = {} |
| 467 | attrs = {} |
| 468 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
| 469 | form="alis", seld=folder_alias, fr=None) |
| 470 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 471 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
| 472 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 473 | form="prop", seld=aetypes.Type('posn'), fr=aeobj_1) |
| 474 | args['----'] = aeobj_2 |
| 475 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
| 476 | if args.has_key('errn'): |
| 477 | raise Error, aetools.decodeerror(args) |
| 478 | if args.has_key('----'): |
| 479 | return args['----'] |
| 480 | |
| 481 | def windowposition(folder, pos=None): |
| 482 | """Set the position of a Finder window for folder to pos=(w, h).""" |
| 483 | fss = macfs.FSSpec(folder) |
| 484 | folder_alias = fss.NewAlias() |
| 485 | openwindow(fss) |
| 486 | if not pos: |
| 487 | return _getwindowposition(folder_alias) |
| 488 | if type(pos) == InstanceType: |
| 489 | # pos might be a QDPoint object as returned by _getwindowposition |
| 490 | pos = (pos.h, pos.v) |
| 491 | return _setwindowposition(folder_alias, pos) |
| 492 | |
| 493 | def _setwindowposition(folder_alias, (x, y)): |
| 494 | """Set the size of a Finder window for folder to (w, h).""" |
| 495 | finder = _getfinder() |
| 496 | args = {} |
| 497 | attrs = {} |
| 498 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
| 499 | form="alis", seld=folder_alias, fr=None) |
| 500 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 501 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
| 502 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 503 | form="prop", seld=aetypes.Type('posn'), fr=aeobj_1) |
| 504 | args['----'] = aeobj_2 |
| 505 | args["data"] = [x, y] |
| 506 | _reply, args, attrs = finder.send('core', 'setd', args, attrs) |
| 507 | if args.has_key('errn'): |
| 508 | raise Error, aetools.decodeerror(args) |
| 509 | if args.has_key('----'): |
| 510 | return args['----'] |
| 511 | |
| 512 | def _getwindowposition(folder_alias): |
| 513 | """Get the size of a Finder window for folder, Specify by path.""" |
| 514 | finder = _getfinder() |
| 515 | args = {} |
| 516 | attrs = {} |
| 517 | aeobj_0 = aetypes.ObjectSpecifier(want=aetypes.Type('cfol'), |
| 518 | form="alis", seld=folder_alias, fr=None) |
| 519 | aeobj_1 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 520 | form="prop", seld=aetypes.Type('cwnd'), fr=aeobj_0) |
| 521 | aeobj_2 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 522 | form="prop", seld=aetypes.Type('ptsz'), fr=aeobj_1) |
| 523 | args['----'] = aeobj_2 |
| 524 | _reply, args, attrs = finder.send('core', 'getd', args, attrs) |
| 525 | if args.has_key('errn'): |
| 526 | raise Error, aetools.decodeerror(args) |
| 527 | if args.has_key('----'): |
| 528 | return args['----'] |
| 529 | |
| 530 | def icon(object, icondata=None): |
| 531 | """icon sets the icon of object, if no icondata is given, |
| 532 | icon will return an AE object with binary data for the current icon. |
| 533 | If left untouched, this data can be used to paste the icon on another file. |
| 534 | Development opportunity: get and set the data as PICT.""" |
| 535 | fss = macfs.FSSpec(object) |
| 536 | object_alias = fss.NewAlias() |
| 537 | if icondata == None: |
| 538 | return _geticon(object_alias) |
| 539 | return _seticon(object_alias, icondata) |
| 540 | |
| 541 | def _geticon(object_alias): |
| 542 | """get the icondata for object. Binary data of some sort.""" |
| 543 | finder = _getfinder() |
| 544 | args = {} |
| 545 | attrs = {} |
| 546 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), |
| 547 | form="alis", seld=object_alias, fr=None) |
| 548 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 549 | form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00) |
| 550 | args['----'] = aeobj_01 |
| 551 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 552 | if args.has_key('errn'): |
| 553 | raise Error, aetools.decodeerror(args) |
| 554 | if args.has_key('----'): |
| 555 | return args['----'] |
| 556 | |
| 557 | def _seticon(object_alias, icondata): |
| 558 | """set the icondata for object, formatted as produced by _geticon()""" |
| 559 | finder = _getfinder() |
| 560 | args = {} |
| 561 | attrs = {} |
| 562 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('cobj'), |
| 563 | form="alis", seld=object_alias, fr=None) |
| 564 | aeobj_01 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), |
| 565 | form="prop", seld=aetypes.Type('iimg'), fr=aeobj_00) |
| 566 | args['----'] = aeobj_01 |
| 567 | args["data"] = icondata |
| 568 | _reply, args, attrs = finder.send("core", "setd", args, attrs) |
| 569 | if args.has_key('errn'): |
| 570 | raise Error, aetools.decodeerror(args) |
| 571 | if args.has_key('----'): |
| 572 | return args['----'].data |
| 573 | |
| 574 | |
| 575 | #--------------------------------------------------- |
| 576 | # Volumes and servers. |
| 577 | |
| 578 | def mountvolume(volume, server=None, username=None, password=None): |
| 579 | """mount a volume, local or on a server on AppleTalk. |
| 580 | Note: mounting a ASIP server requires a different operation. |
| 581 | server is the name of the server where the volume belongs |
| 582 | username, password belong to a registered user of the volume.""" |
| 583 | finder = _getfinder() |
| 584 | args = {} |
| 585 | attrs = {} |
| 586 | if password: |
| 587 | args["PASS"] = password |
| 588 | if username: |
| 589 | args["USER"] = username |
| 590 | if server: |
| 591 | args["SRVR"] = server |
| 592 | args['----'] = volume |
| 593 | _reply, args, attrs = finder.send("aevt", "mvol", args, attrs) |
| 594 | if args.has_key('errn'): |
| 595 | raise Error, aetools.decodeerror(args) |
| 596 | if args.has_key('----'): |
| 597 | return args['----'] |
| 598 | |
| 599 | def unmountvolume(volume): |
| 600 | """unmount a volume that's on the desktop""" |
| 601 | putaway(volume) |
| 602 | |
| 603 | def putaway(object): |
| 604 | """puth the object away, whereever it came from.""" |
| 605 | finder = _getfinder() |
| 606 | args = {} |
| 607 | attrs = {} |
| 608 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('cdis'), form="name", seld=object, fr=None) |
| 609 | _reply, args, attrs = talker.send("fndr", "ptwy", args, attrs) |
| 610 | if args.has_key('errn'): |
| 611 | raise Error, aetools.decodeerror(args) |
| 612 | if args.has_key('----'): |
| 613 | return args['----'] |
| 614 | |
| 615 | |
| 616 | #--------------------------------------------------- |
| 617 | # Miscellaneous functions |
| 618 | # |
| 619 | |
| 620 | def volumelevel(level): |
| 621 | """set the audio output level, parameter between 0 (silent) and 7 (full blast)""" |
| 622 | finder = _getfinder() |
| 623 | args = {} |
| 624 | attrs = {} |
| 625 | if level < 0: |
| 626 | level = 0 |
| 627 | elif level > 7: |
| 628 | level = 7 |
| 629 | args['----'] = level |
| 630 | _reply, args, attrs = finder.send("aevt", "stvl", args, attrs) |
| 631 | if args.has_key('errn'): |
| 632 | raise Error, aetools.decodeerror(args) |
| 633 | if args.has_key('----'): |
| 634 | return args['----'] |
| 635 | |
| 636 | def OSversion(): |
| 637 | """return the version of the system software""" |
| 638 | finder = _getfinder() |
| 639 | args = {} |
| 640 | attrs = {} |
| 641 | aeobj_00 = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('ver2'), fr=None) |
| 642 | args['----'] = aeobj_00 |
| 643 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 644 | if args.has_key('errn'): |
| 645 | raise Error, aetools.decodeerror(args) |
| 646 | if args.has_key('----'): |
| 647 | return args['----'] |
| 648 | |
| 649 | def filesharing(): |
| 650 | """return the current status of filesharing and whether it is starting up or not: |
| 651 | -1 file sharing is off and not starting up |
| 652 | 0 file sharing is off and starting up |
| 653 | 1 file sharing is on""" |
| 654 | status = -1 |
| 655 | finder = _getfinder() |
| 656 | # see if it is on |
| 657 | args = {} |
| 658 | attrs = {} |
| 659 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fshr'), fr=None) |
| 660 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 661 | if args.has_key('errn'): |
| 662 | raise Error, aetools.decodeerror(args) |
| 663 | if args.has_key('----'): |
| 664 | if args['----'] == 0: |
| 665 | status = -1 |
| 666 | else: |
| 667 | status = 1 |
| 668 | # is it starting up perchance? |
| 669 | args = {} |
| 670 | attrs = {} |
| 671 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('fsup'), fr=None) |
| 672 | _reply, args, attrs = finder.send("core", "getd", args, attrs) |
| 673 | if args.has_key('errn'): |
| 674 | raise Error, aetools.decodeerror(args) |
| 675 | if args.has_key('----'): |
| 676 | if args['----'] == 1: |
| 677 | status = 0 |
| 678 | return status |
| 679 | |
| 680 | def movetotrash(path): |
| 681 | """move the object to the trash""" |
| 682 | fss = macfs.FSSpec(path) |
| 683 | trashfolder = macfs.FSSpec(macfs.FindFolder(fss.as_tuple()[0], 'trsh', 0) + ("",)).as_pathname() |
Jack Jansen | 5bb6ff9 | 2001-07-24 11:37:23 +0000 | [diff] [blame] | 684 | move(path, trashfolder) |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 685 | |
| 686 | def emptytrash(): |
| 687 | """empty the trash""" |
| 688 | finder = _getfinder() |
| 689 | args = {} |
| 690 | attrs = {} |
| 691 | args['----'] = aetypes.ObjectSpecifier(want=aetypes.Type('prop'), form="prop", seld=aetypes.Type('trsh'), fr=None) |
| 692 | _reply, args, attrs = finder.send("fndr", "empt", args, attrs) |
| 693 | if args.has_key('errn'): |
| 694 | raise aetools.Error, aetools.decodeerror(args) |
| 695 | |
| 696 | |
| 697 | def _test(): |
| 698 | print 'Original findertools functionality test...' |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 699 | print 'Testing launch...' |
| 700 | fss, ok = macfs.PromptGetFile('File to launch:') |
| 701 | if ok: |
| 702 | result = launch(fss) |
| 703 | if result: |
| 704 | print 'Result: ', result |
| 705 | print 'Press return-', |
| 706 | sys.stdin.readline() |
| 707 | print 'Testing print...' |
| 708 | fss, ok = macfs.PromptGetFile('File to print:') |
| 709 | if ok: |
| 710 | result = Print(fss) |
| 711 | if result: |
| 712 | print 'Result: ', result |
| 713 | print 'Press return-', |
| 714 | sys.stdin.readline() |
| 715 | print 'Testing copy...' |
| 716 | fss, ok = macfs.PromptGetFile('File to copy:') |
| 717 | if ok: |
| 718 | dfss, ok = macfs.GetDirectory() |
| 719 | if ok: |
| 720 | result = copy(fss, dfss) |
| 721 | if result: |
| 722 | print 'Result:', result |
| 723 | print 'Press return-', |
| 724 | sys.stdin.readline() |
| 725 | print 'Testing move...' |
| 726 | fss, ok = macfs.PromptGetFile('File to move:') |
| 727 | if ok: |
| 728 | dfss, ok = macfs.GetDirectory() |
| 729 | if ok: |
| 730 | result = move(fss, dfss) |
| 731 | if result: |
| 732 | print 'Result:', result |
| 733 | print 'Press return-', |
| 734 | sys.stdin.readline() |
| 735 | import EasyDialogs |
| 736 | print 'Testing sleep...' |
| 737 | if EasyDialogs.AskYesNoCancel('Sleep?') > 0: |
| 738 | result = sleep() |
| 739 | if result: |
| 740 | print 'Result:', result |
| 741 | print 'Press return-', |
| 742 | sys.stdin.readline() |
| 743 | print 'Testing shutdown...' |
| 744 | if EasyDialogs.AskYesNoCancel('Shut down?') > 0: |
| 745 | result = shutdown() |
| 746 | if result: |
| 747 | print 'Result:', result |
| 748 | print 'Press return-', |
| 749 | sys.stdin.readline() |
| 750 | print 'Testing restart...' |
| 751 | if EasyDialogs.AskYesNoCancel('Restart?') > 0: |
| 752 | result = restart() |
| 753 | if result: |
| 754 | print 'Result:', result |
| 755 | print 'Press return-', |
| 756 | sys.stdin.readline() |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 757 | |
| 758 | def _test2(): |
Jack Jansen | 6f84ed5 | 2001-05-17 12:45:13 +0000 | [diff] [blame] | 759 | print '\nmorefindertools version %s\nTests coming up' %__version__ |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 760 | import os |
| 761 | import random |
| 762 | |
| 763 | # miscellaneous |
| 764 | print '\tfilesharing on?', filesharing() # is file sharing on, off, starting up? |
| 765 | print '\tOS version', OSversion() # the version of the system software |
| 766 | |
| 767 | # set the soundvolume in a simple way |
| 768 | print '\tSystem beep volume' |
| 769 | for i in range(0, 7): |
| 770 | volumelevel(i) |
| 771 | MacOS.SysBeep() |
| 772 | |
| 773 | # Finder's windows, file location, file attributes |
| 774 | open("@findertoolstest", "w") |
| 775 | f = macfs.FSSpec("@findertoolstest").as_pathname() |
| 776 | reveal(f) # reveal this file in a Finder window |
| 777 | select(f) # select this file |
| 778 | |
| 779 | base, file = os.path.split(f) |
| 780 | closewindow(base) # close the window this file is in (opened by reveal) |
| 781 | openwindow(base) # open it again |
| 782 | windowview(base, 1) # set the view by list |
| 783 | |
| 784 | label(f, 2) # set the label of this file to something orange |
| 785 | print '\tlabel', label(f) # get the label of this file |
| 786 | |
| 787 | # the file location only works in a window with icon view! |
| 788 | print 'Random locations for an icon' |
| 789 | windowview(base, 0) # set the view by icon |
| 790 | windowsize(base, (600, 600)) |
| 791 | for i in range(50): |
| 792 | location(f, (random.randint(10, 590), random.randint(10, 590))) |
| 793 | |
| 794 | windowsize(base, (200, 400)) |
| 795 | windowview(base, 1) # set the view by icon |
| 796 | |
| 797 | orgpos = windowposition(base) |
| 798 | print 'Animated window location' |
| 799 | for i in range(10): |
| 800 | pos = (100+i*10, 100+i*10) |
| 801 | windowposition(base, pos) |
| 802 | print '\twindow position', pos |
Jack Jansen | 6f84ed5 | 2001-05-17 12:45:13 +0000 | [diff] [blame] | 803 | windowposition(base, orgpos) # park it where it was before |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 804 | |
| 805 | print 'Put a comment in file', f, ':' |
| 806 | print '\t', comment(f) # print the Finder comment this file has |
| 807 | s = 'This is a comment no one reads!' |
| 808 | comment(f, s) # set the Finder comment |
| 809 | |
| 810 | def _test3(): |
| 811 | print 'MacOS9 or better specific functions' |
| 812 | # processes |
| 813 | pr = processes() # return a list of tuples with (active_processname, creatorcode) |
| 814 | print 'Return a list of current active processes:' |
| 815 | for p in pr: |
| 816 | print '\t', p |
| 817 | |
| 818 | # get attributes of the first process in the list |
| 819 | print 'Attributes of the first process in the list:' |
| 820 | pinfo = processinfo(pr[0][0]) |
| 821 | print '\t', pr[0][0] |
| 822 | print '\t\tmemory partition', pinfo.partition # the memory allocated to this process |
| 823 | print '\t\tmemory used', pinfo.used # the memory actuall used by this process |
| 824 | print '\t\tis visible', pinfo.visible # is the process visible to the user |
| 825 | print '\t\tis frontmost', pinfo.frontmost # is the process the front most one? |
| 826 | print '\t\thas scripting', pinfo.hasscripting # is the process scriptable? |
| 827 | print '\t\taccepts high level events', pinfo.accepthighlevel # does the process accept high level appleevents? |
| 828 | |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 829 | if __name__ == '__main__': |
Jack Jansen | 4393512 | 2001-04-07 12:53:45 +0000 | [diff] [blame] | 830 | _test() |
| 831 | _test2() |
| 832 | _test3() |
Jack Jansen | 0585d41 | 1996-09-20 15:26:20 +0000 | [diff] [blame] | 833 | |