3 A simple example of how to use Apple Events to implement a "system()"
4 call that invokes ToolServer on the command.
6 Contributed by Daniel Brotsky <dev@brotsky.com>.
8 (renamed from aesystem to mpwsystem by Jack)
10 system(cmd, infile = None, outfile = None, errfile = None)
12 1. Every call to system sets "lastStatus" and "lastOutput" to the
14 produced by the command when executed in ToolServer. (lastParameters
16 are set to the values of the AppleEvent result.)
18 2. system returns lastStatus unless the command result indicates a MacOS error,
19 in which case os.Error is raised with the errnum as associated value.
21 3. You can specify ToolServer-understandable pathnames for
23 output, and error streams. By default, input is Dev:Null, output is captured
24 and returned to the caller, diagnostics are captured and returned to
26 (There's a 64K limit to how much can be captured and returned this way.)"""
32 except NameError: server
= aetools
.TalkTo("MPSX", 1)
36 lastErrorOutput
= None
43 def system(cmd
, infile
= None, outfile
= None, errfile
= None):
44 global lastStatus
, lastOutput
, lastErrorOutput
45 global lastScript
, lastEvent
, lastReply
, lastParameters
, lastAttributes
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']
63 if __name__
== '__main__':
64 sts
= system('alert "Hello World"')
65 print 'system returned', sts