tags:

views:

166

answers:

2

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!

A: 

There are three ways to do this:

Hope this helps, Best regards, Tom.

tommieb75
Can you please explain why the downvote when your question failed to highlight whether it was a programming language or not? Otherwise your question should be moved to superuser.com!
tommieb75
This isn't really responsive, because the OPs problem is he doesn't know what command he would use if typing it himself...
dmckee
I didn't down vote you - I was reading your link on popen. I put it in stackoverflow since I don't think UNIX IPC qualifies as superuser tweeks and since this is code in a programming application.
bias
I downvoted you because it doesn't answer the OPs question. The OP doesn't need to know how to launch a new process, the OP needs to know how to get Terminal.app to create a new tab programatically.
Omnifarious
No problem! :) @bias: Have you checked out the screen utility or is that available on Mac OSX? http://www.kuro5hin.org/story/2004/3/9/16838/14935
tommieb75
Understood - thanks Omnifarious for your input...no problem there...sorry if my answer is wrong.
tommieb75
Getting screen to create a new shell is different from getting Terminal.app to create a new tab. It might be as good a solution (or even better) but it is not what is requested.
dmckee
I was thinking about how I could use screen, since that would be preferable. I'll have to look deeper into linux IPC itself and I'll likely need to use screen since I can't see how I can open a new tab in Terminal (and, to my google-ability, Terminal isn't extensively documented).
bias
+2  A: 

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.

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. :-)

Omnifarious
Damn! I just ran this down a few minutes late.
dmckee
@dmckee, thanks for hunting down the SO links. The external pages I link to have similar information, but the SO links present it much more nicely and have a bit more information.
Omnifarious
It was the `osascript` that took me a while...had to find it on SuperUser...
dmckee
@dmckee, I just used Google. *grin* `apple terminal.app arguments` and then after scanning a few of those `terminal.app applescript`.
Omnifarious
Alright - I worked up an answer using some info from these links. However, I wold have preferred not having to use AppleScript (can't always get what you want).
bias
@bias, I'm sad it was so ugly. It would be nice if it weren't, but I don't think pretty is possible here.
Omnifarious