/Lib/plat-mac/terminalcommand.py

http://unladen-swallow.googlecode.com/ · Python · 50 lines · 30 code · 10 blank · 10 comment · 4 complexity · b0dc29de82c5f6d6953db2e036f8b48e MD5 · raw file

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