blob: 292f09c7d80a633fc975eb16a0b4dd29d7cecf39 [file] [log] [blame]
Just van Rossum8afa3a32003-01-04 21:44:21 +00001"""terminalcommand.py -- A minimal interface to Terminal.app.
2
3To run a shell command in a new Terminal.app window:
4
5 import terminalcommand
6 terminalcommand.run("ls -l")
7
8No result is returned; it is purely meant as a quick way to run a script
9with a decent input/output window.
10"""
11
12#
13# This module is a fairly straightforward translation of Jack Jansen's
14# Mac/OSX/PythonLauncher/doscript.m.
15#
16
17import time
18import os
19from Carbon import AE
20from Carbon.AppleEvents import *
21
22
23TERMINAL_SIG = "trmx"
24START_TERMINAL = "/usr/bin/open /Applications/Utilities/Terminal.app"
25SEND_MODE = kAENoReply # kAEWaitReply hangs when run from Terminal.app itself
26
27
28def run(command):
29 """Run a shell command in a new Terminal.app window."""
30 termAddress = AE.AECreateDesc(typeApplSignature, TERMINAL_SIG)
Just van Rossumac8657b2003-06-21 14:49:14 +000031 theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress,
32 kAutoGenerateReturnID, kAnyTransactionID)
33 commandDesc = AE.AECreateDesc(typeChar, command)
34 theEvent.AEPutParamDesc(kAECommandClass, commandDesc)
Just van Rossum8afa3a32003-01-04 21:44:21 +000035
36 try:
Just van Rossumac8657b2003-06-21 14:49:14 +000037 theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
Just van Rossum8afa3a32003-01-04 21:44:21 +000038 except AE.Error, why:
39 if why[0] != -600: # Terminal.app not yet running
40 raise
41 os.system(START_TERMINAL)
42 time.sleep(1)
Just van Rossumac8657b2003-06-21 14:49:14 +000043 theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
Just van Rossum8afa3a32003-01-04 21:44:21 +000044
45
46if __name__ == "__main__":
47 run("ls -l")