This should finally fix #6896. Let's watch the buildbots.
[python.git] / Lib / plat-mac / terminalcommand.py
blobef8cb6c4101a43868107d0ee28600fc8229f98b0
1 """terminalcommand.py -- A minimal interface to Terminal.app.
3 To run a shell command in a new Terminal.app window:
5 import terminalcommand
6 terminalcommand.run("ls -l")
8 No result is returned; it is purely meant as a quick way to run a script
9 with a decent input/output window.
10 """
13 # This module is a fairly straightforward translation of Jack Jansen's
14 # Mac/OSX/PythonLauncher/doscript.m.
17 from warnings import warnpy3k
18 warnpy3k("In 3.x, the terminalcommand module is removed.", stacklevel=2)
20 import time
21 import os
22 from Carbon import AE
23 from Carbon.AppleEvents import *
26 TERMINAL_SIG = "trmx"
27 START_TERMINAL = "/usr/bin/open /Applications/Utilities/Terminal.app"
28 SEND_MODE = kAENoReply # kAEWaitReply hangs when run from Terminal.app itself
31 def run(command):
32 """Run a shell command in a new Terminal.app window."""
33 termAddress = AE.AECreateDesc(typeApplicationBundleID, "com.apple.Terminal")
34 theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress,
35 kAutoGenerateReturnID, kAnyTransactionID)
36 commandDesc = AE.AECreateDesc(typeChar, command)
37 theEvent.AEPutParamDesc(kAECommandClass, commandDesc)
39 try:
40 theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
41 except AE.Error, why:
42 if why[0] != -600: # Terminal.app not yet running
43 raise
44 os.system(START_TERMINAL)
45 time.sleep(1)
46 theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
49 if __name__ == "__main__":
50 run("ls -l")