blob: 34646c4ea5273eb568714e1d230450fd1ccff411 [file] [log] [blame]
Jack Jansen20ffa0e2001-12-03 00:11:35 +00001
2"""mpwsystem -
3A simple example of how to use Apple Events to implement a "system()"
4call that invokes ToolServer on the command.
5
6Contributed by Daniel Brotsky <dev@brotsky.com>.
7
8(renamed from aesystem to mpwsystem by Jack)
9
10system(cmd, infile = None, outfile = None, errfile = None)
11
121. Every call to system sets "lastStatus" and "lastOutput" to the
13status and output
14produced by the command when executed in ToolServer. (lastParameters
15and lastAttributes
16are set to the values of the AppleEvent result.)
17
182. system returns lastStatus unless the command result indicates a MacOS error,
19in which case os.Error is raised with the errnum as associated value.
20
213. You can specify ToolServer-understandable pathnames for
22redirection of input,
23output, and error streams. By default, input is Dev:Null, output is captured
24and returned to the caller, diagnostics are captured and returned to
25the caller.
26(There's a 64K limit to how much can be captured and returned this way.)"""
27
28import os
29import aetools
30
31try: server
32except NameError: server = aetools.TalkTo("MPSX", 1)
33
34lastStatus = None
35lastOutput = None
36lastErrorOutput = None
37lastScript = None
38lastEvent = None
39lastReply = None
40lastParameters = None
41lastAttributes = None
42
43def system(cmd, infile = None, outfile = None, errfile = None):
44 global lastStatus, lastOutput, lastErrorOutput
45 global lastScript, lastEvent, lastReply, lastParameters, lastAttributes
46 cmdline = cmd
47 if infile: cmdline += " <" + infile
48 if outfile: cmdline += " >" + outfile
49 if errfile: cmdline += " " + str(chr(179)) + errfile
50 lastScript = "set Exit 0\r" + cmdline + "\rexit {Status}"
51 lastEvent = server.newevent("misc", "dosc", {"----" : lastScript})
52 (lastReply, lastParameters, lastAttributes) = server.sendevent(lastEvent)
53 if lastParameters.has_key('stat'): lastStatus = lastParameters['stat']
54 else: lastStatus = None
55 if lastParameters.has_key('----'): lastOutput = lastParameters['----']
56 else: lastOutput = None
57 if lastParameters.has_key('diag'): lastErrorOutput = lastParameters['diag']
58 else: lastErrorOutput = None
59 if lastParameters['errn'] != 0:
60 raise os.Error, lastParameters['errn']
61 return lastStatus
62
63if __name__ == '__main__':
64 sts = system('alert "Hello World"')
65 print 'system returned', sts
66
67