Just van Rossum | 8afa3a3 | 2003-01-04 21:44:21 +0000 | [diff] [blame] | 1 | """terminalcommand.py -- A minimal interface to Terminal.app. |
| 2 | |
| 3 | To run a shell command in a new Terminal.app window: |
| 4 | |
| 5 | import terminalcommand |
| 6 | terminalcommand.run("ls -l") |
| 7 | |
| 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 | """ |
| 11 | |
| 12 | # |
| 13 | # This module is a fairly straightforward translation of Jack Jansen's |
| 14 | # Mac/OSX/PythonLauncher/doscript.m. |
| 15 | # |
| 16 | |
Benjamin Peterson | 2368193 | 2008-05-12 21:42:13 +0000 | [diff] [blame^] | 17 | from warnings import warnpy3k |
| 18 | warnpy3k("In 3.x, the terminalcommand module is removed.") |
| 19 | |
Just van Rossum | 8afa3a3 | 2003-01-04 21:44:21 +0000 | [diff] [blame] | 20 | import time |
| 21 | import os |
| 22 | from Carbon import AE |
| 23 | from Carbon.AppleEvents import * |
| 24 | |
| 25 | |
| 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 |
| 29 | |
| 30 | |
| 31 | def run(command): |
| 32 | """Run a shell command in a new Terminal.app window.""" |
Ronald Oussoren | f2ef92c | 2008-05-02 21:42:35 +0000 | [diff] [blame] | 33 | termAddress = AE.AECreateDesc(typeApplicationBundleID, "com.apple.Terminal") |
Just van Rossum | ac8657b | 2003-06-21 14:49:14 +0000 | [diff] [blame] | 34 | theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress, |
| 35 | kAutoGenerateReturnID, kAnyTransactionID) |
| 36 | commandDesc = AE.AECreateDesc(typeChar, command) |
| 37 | theEvent.AEPutParamDesc(kAECommandClass, commandDesc) |
Just van Rossum | 8afa3a3 | 2003-01-04 21:44:21 +0000 | [diff] [blame] | 38 | |
| 39 | try: |
Just van Rossum | ac8657b | 2003-06-21 14:49:14 +0000 | [diff] [blame] | 40 | theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout) |
Just van Rossum | 8afa3a3 | 2003-01-04 21:44:21 +0000 | [diff] [blame] | 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) |
Just van Rossum | ac8657b | 2003-06-21 14:49:14 +0000 | [diff] [blame] | 46 | theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout) |
Just van Rossum | 8afa3a3 | 2003-01-04 21:44:21 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | run("ls -l") |