Files for 2.1b1 distribution.
[python/dscho.git] / Mac / Lib / findertools.py
blob45f3e9e87977ed9a8602ce24090c46a9a7559da5
1 """Utility routines depending on the finder."""
3 import Finder
4 import AppleEvents
5 import aetools
6 import MacOS
7 import sys
8 import macfs
10 _finder_talker = None
12 def _getfinder():
13 global _finder_talker
14 if not _finder_talker:
15 _finder_talker = Finder.Finder()
16 _finder_talker.send_flags = ( _finder_talker.send_flags |
17 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
18 return _finder_talker
20 def launch(file):
21 """Open a file thru the finder. Specify file by name or fsspec"""
22 finder = _getfinder()
23 fss = macfs.FSSpec(file)
24 return finder.open(fss)
26 def Print(file):
27 """Print a file thru the finder. Specify file by name or fsspec"""
28 finder = _getfinder()
29 fss = macfs.FSSpec(file)
30 return finder._print(fss)
32 def copy(src, dstdir):
33 """Copy a file to a folder"""
34 finder = _getfinder()
35 if type(src) == type([]):
36 src_fss = []
37 for s in src:
38 src_fss.append(macfs.FSSpec(s))
39 else:
40 src_fss = macfs.FSSpec(src)
41 dst_fss = macfs.FSSpec(dstdir)
42 return finder.duplicate(src_fss, to=dst_fss)
44 def move(src, dstdir):
45 """Move a file to a folder"""
46 finder = _getfinder()
47 if type(src) == type([]):
48 src_fss = []
49 for s in src:
50 src_fss.append(macfs.FSSpec(s))
51 else:
52 src_fss = macfs.FSSpec(src)
53 dst_fss = macfs.FSSpec(dstdir)
54 return finder.move(src_fss, to=dst_fss)
56 def sleep():
57 """Put the mac to sleep"""
58 finder = _getfinder()
59 finder.sleep()
61 def shutdown():
62 """Shut the mac down"""
63 finder = _getfinder()
64 finder.shut_down()
66 def restart():
67 """Restart the mac"""
68 finder = _getfinder()
69 finder.restart()
72 def main():
73 print 'Testing launch...'
74 fss, ok = macfs.PromptGetFile('File to launch:')
75 if ok:
76 result = launch(fss)
77 if result:
78 print 'Result: ', result
79 print 'Press return-',
80 sys.stdin.readline()
81 print 'Testing print...'
82 fss, ok = macfs.PromptGetFile('File to print:')
83 if ok:
84 result = Print(fss)
85 if result:
86 print 'Result: ', result
87 print 'Press return-',
88 sys.stdin.readline()
89 print 'Testing copy...'
90 fss, ok = macfs.PromptGetFile('File to copy:')
91 if ok:
92 dfss, ok = macfs.GetDirectory()
93 if ok:
94 result = copy(fss, dfss)
95 if result:
96 print 'Result:', result
97 print 'Press return-',
98 sys.stdin.readline()
99 print 'Testing move...'
100 fss, ok = macfs.PromptGetFile('File to move:')
101 if ok:
102 dfss, ok = macfs.GetDirectory()
103 if ok:
104 result = move(fss, dfss)
105 if result:
106 print 'Result:', result
107 print 'Press return-',
108 sys.stdin.readline()
109 import EasyDialogs
110 print 'Testing sleep...'
111 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
112 result = sleep()
113 if result:
114 print 'Result:', result
115 print 'Press return-',
116 sys.stdin.readline()
117 print 'Testing shutdown...'
118 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
119 result = shutdown()
120 if result:
121 print 'Result:', result
122 print 'Press return-',
123 sys.stdin.readline()
124 print 'Testing restart...'
125 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
126 result = restart()
127 if result:
128 print 'Result:', result
129 print 'Press return-',
130 sys.stdin.readline()
132 if __name__ == '__main__':
133 main()