Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Mac / Lib / findertools.py
blobffc75f51e2f3389f2c847be429e08ed70a1c3e06
1 """Utility routines depending on the finder."""
3 import Finder_7_0_Suite
4 import aetools
5 import MacOS
6 import sys
7 import macfs
9 SIGNATURE='MACS'
11 class Finder(aetools.TalkTo, Finder_7_0_Suite.Finder_7_0_Suite):
12 pass
14 _finder_talker = None
16 def _getfinder():
17 global _finder_talker
18 if not _finder_talker:
19 _finder_talker = Finder(SIGNATURE)
20 return _finder_talker
22 def launch(file):
23 """Open a file thru the finder. Specify file by name or fsspec"""
24 finder = _getfinder()
25 fss = macfs.FSSpec(file)
26 vRefNum, parID, name = fss.as_tuple()
27 dir_fss = macfs.FSSpec((vRefNum, parID, ''))
28 file_alias = fss.NewAlias()
29 dir_alias = dir_fss.NewAlias()
30 return finder.open(dir_alias, items=[file_alias])
32 def Print(file):
33 """Print a file thru the finder. Specify file by name or fsspec"""
34 finder = _getfinder()
35 fss = macfs.FSSpec(file)
36 vRefNum, parID, name = fss.as_tuple()
37 dir_fss = macfs.FSSpec((vRefNum, parID, ''))
38 file_alias = fss.NewAlias()
39 dir_alias = dir_fss.NewAlias()
40 return finder._print(dir_alias, items=[file_alias])
42 def copy(src, dstdir):
43 """Copy a file to a folder"""
44 finder = _getfinder()
45 src_fss = macfs.FSSpec(src)
46 dst_fss = macfs.FSSpec(dstdir)
47 src_alias = src_fss.NewAlias()
48 dst_alias = dst_fss.NewAlias()
49 return finder.copy_to(dst_alias, _from=[src_alias])
51 def move(src, dstdir):
52 """Move a file to a folder"""
53 finder = _getfinder()
54 src_fss = macfs.FSSpec(src)
55 dst_fss = macfs.FSSpec(dstdir)
56 src_alias = src_fss.NewAlias()
57 dst_alias = dst_fss.NewAlias()
58 return finder.move_to(dst_alias, _from=[src_alias])
60 def sleep():
61 """Put the mac to sleep"""
62 finder = _getfinder()
63 finder.sleep()
65 def shutdown():
66 """Shut the mac down"""
67 finder = _getfinder()
68 finder.shut_down()
70 def restart():
71 """Restart the mac"""
72 finder = _getfinder()
73 finder.restart()
76 def main():
77 print 'Testing launch...'
78 fss, ok = macfs.PromptGetFile('File to launch:')
79 if ok:
80 result = launch(fss)
81 if result:
82 print 'Result: ', result
83 print 'Press return-',
84 sys.stdin.readline()
85 print 'Testing print...'
86 fss, ok = macfs.PromptGetFile('File to print:')
87 if ok:
88 result = Print(fss)
89 if result:
90 print 'Result: ', result
91 print 'Press return-',
92 sys.stdin.readline()
93 print 'Testing copy...'
94 fss, ok = macfs.PromptGetFile('File to copy:')
95 if ok:
96 dfss, ok = macfs.GetDirectory()
97 if ok:
98 result = copy(fss, dfss)
99 if result:
100 print 'Result:', result
101 print 'Press return-',
102 sys.stdin.readline()
103 print 'Testing move...'
104 fss, ok = macfs.PromptGetFile('File to move:')
105 if ok:
106 dfss, ok = macfs.GetDirectory()
107 if ok:
108 result = move(fss, dfss)
109 if result:
110 print 'Result:', result
111 print 'Press return-',
112 sys.stdin.readline()
113 import EasyDialogs
114 print 'Testing sleep...'
115 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
116 result = sleep()
117 if result:
118 print 'Result:', result
119 print 'Press return-',
120 sys.stdin.readline()
121 print 'Testing shutdown...'
122 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
123 result = shutdown()
124 if result:
125 print 'Result:', result
126 print 'Press return-',
127 sys.stdin.readline()
128 print 'Testing restart...'
129 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
130 result = restart()
131 if result:
132 print 'Result:', result
133 print 'Press return-',
134 sys.stdin.readline()
136 if __name__ == '__main__':
137 main()