run commands in new tab in Terminal.app

hello, i want to know is there a way to run commands and these opens a new tab in terminal.app.
i know i can open a new tab with cmd+t , but i have a folder with hundreds of files with the name username@server.com.inetloc and each file has the string ssh://username@server...

i use these folder in the dock like a bookmark of servers that i administrate, if i set that ssh default is the app iTerm.app then every of these file i execute opens in a new tab of iTerm, but if i set ssh default to Terminal.app for every file open a new windows of Terminal.

I would like to use Terminal.app if somebody help to find a way to open these conections in a new tab.

You can try to open the terminal.app, them in safari type ssh://localhost... and you see a new window of terminal.

Sorry for my bad english

Regards

Diego.

macbook 5,1, Mac OS X (10.6.2)

Posted on Feb 15, 2010 5:57 PM

Reply
12 replies

Feb 15, 2010 8:09 PM in response to diegonob

Personally, I would get iTerm, and populate iTerm's bookmarks with all the ssh sessions you wish to use. You can even create hotkeys that start a new Tab running your ssh command. This is how I use iTerm at work.
<http://iterm.sourceforge.net/>

If you really want to stay with Terminal, then I would suggest finding a way to have an AppleScript send a Command-T to the Terminal application. If you want to use a shell script for that, you could use osascript to invoke the the AppleScript.

You might be able to create an Automator workflow, maybe even one where you have it learn from what you are doing.

Feb 16, 2010 3:26 AM in response to BobHarris

yes, i'm using iTerm because his bookmark is what i need, but for the Terminal i like one thing and is the "plugin" Visor.bundle, i like invoke the terminal with a hotkey...because i'm used to using yakuake in linux. That's the only reason for i would like to use terminal.app

Anyway no problem to continue using iTerm, just wanted to know if there was some fast and easy way as we are used to solve our requirements in osx.
For the moment i haven't time to learn apple script, perhaps in future i will do so.
Thanks anyway brother!

Feb 16, 2010 12:09 PM in response to BobHarris

I found this:

tell application "System Events" to tell process "Terminal" to keystroke "t" using command down

But it seems not to work. FWIW, I'm trying to accomplish something very similar. I have a list of shell scripts named after servers that, when launched from spotlight, happily open a new terminal window and then ssh with my specified credentials. Works great, but I'd prefer tabs.

Feb 16, 2010 1:18 PM in response to fourfthawaiian


osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'

This half worked for me.

If I executed it from iTerm, it caused iTerm to open a new Tab.

If I executed it from Terminal, it caused Terminal to open a new Tab.

If I use ssh into the Mac from another Mac, then invoked the command, it caused the local iTerm to create a new Tab. I was running the script on the remote Mac. But somehow Applescript on the remote Mac sent the request back to my originating Mac. Very strange.

Feb 16, 2010 4:47 PM in response to fourfthawaiian

I would first like to thank all those who tried to give me
ideas, but I'm thinking I'm a problematic guy:)
the script you have shown me is very good but only works
running from terminal or iTerm.

if you want to see how i connect from the stack in the dock to my
servers can be seen here: http://www.youtube.com/watch?
v = ZKbB2CN4ut8
can you try create some connections and see if terminal open in new tab and how implement these script here?

one file .inetloc per connection

i have set that the url ssh:// is open by iterm.app and if i click in one of this file, open a a new connection in a new tab if iTerm is opened...this work out of the box

if i set the default app for url ssh:// to terminal i can reproduce the same like iTerm, i try many, many, many variants...

regards

Feb 16, 2010 6:36 PM in response to diegonob

I would think you could create a bunch of Applescripts or Automator workflows, one for each system you want to connect to, and then put those in a folder, and put that folder on the Dock. Yes it is more work to setup, but unless your remote systems constantly change, it seems that once they are setup, you do not need to do anything more, except launch the script you want.

If the approaches shown are not acceptable, especially the script fourfthawaiian found, then it seems you need to ask Apple for an option to open new sessions in a Tab instead of a new window.

<http://www.apple.com/feedback/macosx.html>
or
<http://bugreporter.apple.com>
Free ADC (Apple Developer Connection) account needed for BugReporter

Feb 16, 2010 7:21 PM in response to diegonob

I wonder if you're too tied to one solution. I do a very, very similar thing to what you're doing, except that I support 900 or so machines 🙂 (I work for a hosting provider -- so, I'm not kidding.. promise!).

The basics of my setup are as such:
I have the host names of all the servers in a text file. In my .bash_profile I have a routine that loops through each server name and, if needed, generates a bash script named for the server in a directory I've set up in my path (~/bin/hosts). That file has the command string for ssh that I use to connect to the server in it. For example, for a server named 'foo', I'd have a file named '~/bin/hosts/foo', and the contents would look something like this:

ssh -X username@foo

This is the part I really like. 🙂 I create each one as it's own bash script so that it can be found and indexed by spotlight, which I use as my app launcher. So, once each script has been created, all I have to do is cmd-space (to open spotlight) then type in the server name to continue. This works great except that it opens a new window every time. So annoying. As of today, though, I now have a new bash script:

tab ssh -x username@foo

A very small change, but with a huge effect. The other change is that I put the "tab" subroutine in a bash script in my path as well. So, now when it launches the ssh command it's in a new tab. Exactly as I wanted.

I'm hoping this will help you. Even if not, though, try to remember to keep an open mind. I know the "url" scheme you're using works well, but I actually prefer my way (of course.. I made it up 😉 ). Whichever way you go, I wish you the best of luck.

Cheers,

4

Feb 16, 2010 8:55 PM in response to diegonob

the script you have shown me is very good but only works running from terminal or iTerm.


You must activate Terminal.app before using "System Events".

set user to "foo"
set theHost to "host.domain"
activate application "Terminal"
tell application "System Events"
tell process "Terminal"
keystroke "t" using command down
end tell
end tell
delay 1 -- it seems we need this
tell application "Terminal"
do script "ssh " & user & "@" & theHost in the last tab of window 1
end tell

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

run commands in new tab in Terminal.app

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.