/Lib/plat-mac/findertools.py

http://unladen-swallow.googlecode.com/ · Python · 835 lines · 779 code · 17 blank · 39 comment · 41 complexity · 6493631fe6d38990e6848f5ca837b893 MD5 · raw file

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