summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJust van Rossum <just@letterror.com>2003-01-04 21:44:21 (GMT)
committerJust van Rossum <just@letterror.com>2003-01-04 21:44:21 (GMT)
commit8afa3a3092b5f8ca37d9dca0823edd3d3253ffc9 (patch)
tree6e3c3a01076e07855cb0b959188c2b7ee0cb38a0 /Lib
parent75a6e3bd1a91ae1d7e4bb30f73052d1b89bca524 (diff)
downloadcpython-8afa3a3092b5f8ca37d9dca0823edd3d3253ffc9.zip
cpython-8afa3a3092b5f8ca37d9dca0823edd3d3253ffc9.tar.gz
cpython-8afa3a3092b5f8ca37d9dca0823edd3d3253ffc9.tar.bz2
module to run commands in a Terminal.app window
Diffstat (limited to 'Lib')
-rw-r--r--Lib/plat-mac/terminalcommand.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/Lib/plat-mac/terminalcommand.py b/Lib/plat-mac/terminalcommand.py
new file mode 100644
index 0000000..835a644
--- /dev/null
+++ b/Lib/plat-mac/terminalcommand.py
@@ -0,0 +1,54 @@
+"""terminalcommand.py -- A minimal interface to Terminal.app.
+
+To run a shell command in a new Terminal.app window:
+
+ import terminalcommand
+ terminalcommand.run("ls -l")
+
+No result is returned; it is purely meant as a quick way to run a script
+with a decent input/output window.
+"""
+
+#
+# This module is a fairly straightforward translation of Jack Jansen's
+# Mac/OSX/PythonLauncher/doscript.m.
+#
+
+import time
+import os
+from Carbon import AE
+from Carbon.AppleEvents import *
+
+
+TERMINAL_SIG = "trmx"
+START_TERMINAL = "/usr/bin/open /Applications/Utilities/Terminal.app"
+SEND_MODE = kAENoReply # kAEWaitReply hangs when run from Terminal.app itself
+
+
+def run(command):
+ """Run a shell command in a new Terminal.app window."""
+ termAddress = AE.AECreateDesc(typeApplSignature, TERMINAL_SIG)
+ theEvent = AE.AECreateAppleEvent(kAEMiscStandards, kAEActivate,
+ termAddress, kAutoGenerateReturnID,
+ kAnyTransactionID)
+
+ try:
+ theEvent.AESend(SEND_MODE, kAENormalPriority,
+ kAEDefaultTimeout)
+ except AE.Error, why:
+ if why[0] != -600: # Terminal.app not yet running
+ raise
+ os.system(START_TERMINAL)
+ time.sleep(1)
+ theEvent.AESend(SEND_MODE, kAENormalPriority,
+ kAEDefaultTimeout)
+
+ theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress,
+ kAutoGenerateReturnID, kAnyTransactionID)
+ commandDesc = AE.AECreateDesc(typeChar, command)
+ theEvent.AEPutParamDesc(kAECommandClass, commandDesc)
+ theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
+
+
+if __name__ == "__main__":
+ run("ls -l")