ยปCore Development>Code coverage>Lib/plat-mac/findertools.py

Python code coverage for Lib/plat-mac/findertools.py

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