3

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!

bias
  • 1,467
  • 3
  • 19
  • 39
  • I would guess that it is pretty easy to do from Applescript ("Tell application Terminal to...", look at the dictionary for Terminal to see what language it accepts), o now you need to know how to run an aplescript. Not my field, but Stack Overflow might know... http://stackoverflow.com/questions/2091278/what-applescript-will-a-particular-application-accept – dmckee --- ex-moderator kitten Jan 29 '10 at 19:18
  • Related: http://stackoverflow.com/questions/1870270/sending-commands-and-strings-to-terminal-app-with-applescript – dmckee --- ex-moderator kitten Jan 29 '10 at 19:24
  • @Rob: Yes it is, but in this case both the application in question and the answer are Mac specific. I think I'll nix the unix tag to make it better... – dmckee --- ex-moderator kitten Jan 30 '10 at 23:52

2 Answers2

2

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

Community
  • 1
  • 1
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • Damn! I just ran this down a few minutes late. – dmckee --- ex-moderator kitten Jan 29 '10 at 19:29
  • @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 Jan 29 '10 at 19:32
  • @dmckee, I just used Google. *grin* `apple terminal.app arguments` and then after scanning a few of those `terminal.app applescript`. – Omnifarious Jan 29 '10 at 19:46
  • 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 Feb 07 '10 at 08:48
  • 1
    @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 Feb 07 '10 at 08:54
-2

There are three ways to do this:

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 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! – t0mm13b Jan 29 '10 at 19:13
  • This isn't really responsive, because the OPs problem is he doesn't know what command he would use if typing it himself... – dmckee --- ex-moderator kitten Jan 29 '10 at 19:14
  • 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 Jan 29 '10 at 19:16
  • 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 Jan 29 '10 at 19:18
  • 1
    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 – t0mm13b Jan 29 '10 at 19:19
  • Understood - thanks Omnifarious for your input...no problem there...sorry if my answer is wrong. – t0mm13b Jan 29 '10 at 19:23
  • 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 --- ex-moderator kitten Jan 29 '10 at 19:25
  • 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 Jan 29 '10 at 20:06