blob: 8a1bf44608638ee738e3bed87ce70e1e41c4fbbb [file] [log] [blame]
Jack Jansen0585d411996-09-20 15:26:20 +00001"""Utility routines depending on the finder."""
2
Jack Jansen8bcd4712000-08-20 19:56:13 +00003import Finder
Jack Jansen0e0d3e71998-10-15 14:02:54 +00004import AppleEvents
Jack Jansen0585d411996-09-20 15:26:20 +00005import aetools
6import MacOS
7import sys
8import macfs
9
Jack Jansen0585d411996-09-20 15:26:20 +000010_finder_talker = None
11
12def _getfinder():
13 global _finder_talker
14 if not _finder_talker:
Jack Jansen8bcd4712000-08-20 19:56:13 +000015 _finder_talker = Finder.Finder()
Jack Jansen0e0d3e71998-10-15 14:02:54 +000016 _finder_talker.send_flags = ( _finder_talker.send_flags |
17 AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer)
Jack Jansen0585d411996-09-20 15:26:20 +000018 return _finder_talker
19
20def launch(file):
21 """Open a file thru the finder. Specify file by name or fsspec"""
22 finder = _getfinder()
23 fss = macfs.FSSpec(file)
Jack Jansen8bcd4712000-08-20 19:56:13 +000024 return finder.open(fss)
Jack Jansen0585d411996-09-20 15:26:20 +000025
26def Print(file):
27 """Print a file thru the finder. Specify file by name or fsspec"""
28 finder = _getfinder()
29 fss = macfs.FSSpec(file)
Jack Jansen8bcd4712000-08-20 19:56:13 +000030 return finder._print(fss)
Jack Jansen0585d411996-09-20 15:26:20 +000031
32def copy(src, dstdir):
33 """Copy a file to a folder"""
34 finder = _getfinder()
35 src_fss = macfs.FSSpec(src)
36 dst_fss = macfs.FSSpec(dstdir)
Jack Jansen8bcd4712000-08-20 19:56:13 +000037 return finder.duplicate(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000038
39def move(src, dstdir):
40 """Move a file to a folder"""
41 finder = _getfinder()
42 src_fss = macfs.FSSpec(src)
43 dst_fss = macfs.FSSpec(dstdir)
Jack Jansen8bcd4712000-08-20 19:56:13 +000044 return finder.move(src_fss, to=dst_fss)
Jack Jansen0585d411996-09-20 15:26:20 +000045
46def sleep():
47 """Put the mac to sleep"""
48 finder = _getfinder()
49 finder.sleep()
50
51def shutdown():
52 """Shut the mac down"""
53 finder = _getfinder()
54 finder.shut_down()
55
56def restart():
57 """Restart the mac"""
58 finder = _getfinder()
59 finder.restart()
60
61
62def main():
63 print 'Testing launch...'
64 fss, ok = macfs.PromptGetFile('File to launch:')
65 if ok:
66 result = launch(fss)
67 if result:
68 print 'Result: ', result
69 print 'Press return-',
70 sys.stdin.readline()
71 print 'Testing print...'
72 fss, ok = macfs.PromptGetFile('File to print:')
73 if ok:
74 result = Print(fss)
75 if result:
76 print 'Result: ', result
77 print 'Press return-',
78 sys.stdin.readline()
79 print 'Testing copy...'
80 fss, ok = macfs.PromptGetFile('File to copy:')
81 if ok:
82 dfss, ok = macfs.GetDirectory()
83 if ok:
84 result = copy(fss, dfss)
85 if result:
86 print 'Result:', result
87 print 'Press return-',
88 sys.stdin.readline()
89 print 'Testing move...'
90 fss, ok = macfs.PromptGetFile('File to move:')
91 if ok:
92 dfss, ok = macfs.GetDirectory()
93 if ok:
94 result = move(fss, dfss)
95 if result:
96 print 'Result:', result
97 print 'Press return-',
98 sys.stdin.readline()
99 import EasyDialogs
100 print 'Testing sleep...'
101 if EasyDialogs.AskYesNoCancel('Sleep?') > 0:
102 result = sleep()
103 if result:
104 print 'Result:', result
105 print 'Press return-',
106 sys.stdin.readline()
107 print 'Testing shutdown...'
108 if EasyDialogs.AskYesNoCancel('Shut down?') > 0:
109 result = shutdown()
110 if result:
111 print 'Result:', result
112 print 'Press return-',
113 sys.stdin.readline()
114 print 'Testing restart...'
115 if EasyDialogs.AskYesNoCancel('Restart?') > 0:
116 result = restart()
117 if result:
118 print 'Result:', result
119 print 'Press return-',
120 sys.stdin.readline()
121
122if __name__ == '__main__':
123 main()
124