开发者

In OSX can I use a system call to pass a command to a running terminal in a new tab?

开发者 https://www.devze.com 2022-12-18 17:49 出处:网络
Specifically, In OSX 10.6 from a system call, I want to open a file for editing with VIM in a preexisting terminal (i.e. Terminal.app) by opening a new tab.

Specifically,

In OSX 10.6 from a system call, I want to open a file for editing with VIM in a preexisting terminal (i.e. Terminal.app) by opening a new tab.

Of course I can open a new instance of terminal

/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -e vim MyFile

And, of course I can figure out the PID of the running instance of Terminal but I don't know how to pass a command into that running program. Or, if Terminal supports receiving commands and if it will open a new tab.

If someone knows how to do this with a similar system (e.g. linux and xterm) it could help me figure it out with OSX and Terminal - or, is there some other technique to prevent opening so many terminals instances?

EDIT: CHEAP SOLUTION

I created an AppleAcript script

on run app_arg
    tell application "System Events"
        tell application process "Terminal"
            key code {55, 36}
            set frontmost to true
            key code {55, 17}
            keystroke item 1 of app_arg
            keystroke return
        end tell
    end tell
end run

and run it via the system call like开发者_开发问答 so

/usr/bin/osascript NEWSCRIPT.scpt "args"

It's dirty but it gets the job done - thanks!


The way to accomplish the is with applescript. You can send applescript to things in OS X with the osascript command. I wasn't able to find anything quickly that directly shows how to open a new tab with a command running in it, but I was able to find a couple of references to automating Terminal.app in various other ways with applescript, and I think they may point you in the right direction.

  • Various random Terminal.app applescript hacks mostly centered around changing colors.
  • An applescript hack that opens a new terminal window without creating a new Terminal process.
  • A nice StackOverflow question about how to query an application to discover what applescript it supports (stolen from a comment on your question by @dmckee)
  • And this nice StackOverflow question concerning Terminal.app's applescript specifically (again stolen from a comment by @dmckee)
  • An even better exploration of the Leopard Terminal.app's applescript from Ruby no less

And from that last link, it looks like the only way to do it is to use applescript to send the Command-T keystroke to the terminal. That's ugly, but it'll work. And then you can send the command you want to execute. :-)


There are three ways to do this:

  • Use popen
  • Use system
  • Use exec family
0

精彩评论

暂无评论...
验证码 换一张
取 消