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