Terminal commands

We are looking for a way to execute multiple commands in the terminal, so far without success. This is what we tried
Tell terminal
do script “cd …”
do script “…”
end tell
The problem is that when the second command is being executed, a new terminal window is also being opened. How can we keep the focus on the first window?

Thanks

The best way to do this is to leave the Terminal out of the picture all together.
It isn’t necessary to employ the Terminal application in order to execute shell commands.

Try this in your Script Editor

do shell script "ls"

Of course, in the above example, I’m doing a plain old ‘list’ command like you would in the Terminal. But by using this line only and NOT directing it to the Terminal, I still get the same result, which is a list of items in the root of the startup disk.

So, you should be able to execute your two shell commands in sequence this way without the Terminal window problems getting in the way.

However, if you want the Terminal to be involved, you can always refer to the previous Terminal window by index. (number)

To execute multiple commands in the Terminal.app, you need to seperate each command with a semi-colon " ; ". Here’s an example…

do shell script "date;cal"

The above script executes the ‘date’ command followed by the ‘cal’ command in the Script Editor. The result should look something like this…

"Fri Apr 25 11:20:49 CDT 2003 --date command
     April 2003 -- cal command
 S  M Tu  W Th  F  S
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
"

If you want to open a new Terminal session using multiple commands, use a tell block…

tell application "Terminal"
	activate
	do script "date;cal"
end tell

The result will be a new Terminal window with the two commands executed.

Primo input, Greg.
I didn’t know about that. :slight_smile:

Another option is set the name of the terminal window and make sure you use that in your commands.


tell application "terminal"
do script "ls" in window "xxx"
do script "date" in window "xxx"
end tell

This of course assumes that xxx is the name you set for your terminal window.

Wayne

or use:

tell application "Terminal"
   do script "blah blah" in window 1
end tell

which runs it in the front window, without having to know the name of that window.

However, I agree with the earlier poster that ‘do script’ (without using Terminal.app) is usually a neater way to go.

I’ve tried a few of the suggestions above, but can’t seem to get them to work. I’d like to write a script that opens terminal and ssh’s into a preselect host. However, it always opens up two terminal windows instead of one (not a major deal, but incredibly sloppy)

Here is the script thus far:

==========
set ssh_login to do shell script (“/usr/bin/whoami”) as string

tell application “Terminal”
activate
tell window 1
set custom title to “SSH to ”
do script "ssh host -l " & ssh_login & “” in window 1
end tell

end tell

Currently it returns an error: “Terminal got an error: NSCannotCreateScriptCommandError”

I’ve tried including the full path of ssh but that doesn’t work either.

The previous version of the script was:

==========
set ssh_login to do shell script (“/usr/bin/whoami”) as string

tell application “Terminal”
activate
set ssh to "ssh host -l " & ssh_login & “”

do script with command ¬
	ssh

end tell

This script works wonderfully, but as I stated above it opens two terminal windows and I only need the one.

Any suggestions would be greatly appreciated!!!

Hi,

I came across a similar problem, where I had an AppleScript Studio app telling teminal to do SSH. I noticed it would open two windows when I hit my SSH button, but run the script just fine in the foremost window. First I wrestled with trying to set the name of the terminal (I assumed was ttyp1) to a variable and doing “do script in window variable” with no luck. I also tried grabbing the index of the terminal window (you will see “index” as a property of “window” in the terminal dictionary entry) but could never get “do script in window” to work … I even messed with closing the first window once it opened.

What I did not realize was that when terminal is running, the script would pop up in its own single window just like I wanted. Your script does not open two windows, terminal opens one when it is launched, then your script opens its own.

Try running your simplier script when terminal is already open and you’ll see it pop one window…

There has got to be a way to do it, maybe check if terminal is already open before you “do script”, and if not, “do script in window of index 0” ???

Good Luck, and LMK what you decide to do.

The reason for the “Terminal got an error: NSCannotCreateScriptCommandError” is most likely that you have some flavor of 10.1, which will not allow you to identify a window. This feature, along with the ability to use ‘do script’ without ‘with command’ and to put 2 commands separated by a semicolon in the same ‘do script’ call, came along with OS 10.2. In 10.1.x you can execute one terminal command. Period. I can’t find any way to get two or more commands to execute serially. This appears to make ftping via the Terminal impossible.

If I’m wrong about any of this, or anyone knows a way to work around it, I would be thrilled to hear about it.

I’m actually running 10.2.6. It seems like the ‘do script’ command works when aquiring strings

set ssh_login to do shell script (“/usr/bin/whoami”) as string

But it fails every time with ssh.

I think I’ve found a work around, but it’s a little buggy at the moment. I’ll post the new script as soon as I have it ironed out. :wink:

Thnx

Here is the final script I’ve developed. It’s a bit akward as I couldn’t figure out how to use the default window that opens when terminal is launched, so I worked around it. :wink:

Here is the final script:

==========
set ssh_login to do shell script (“/usr/bin/whoami”) as string
set targetApp to “Terminal”

tell application “System Events”
set processExists to exists process targetApp
end tell

if processExists is true then
tell application “Terminal”
activate
tell window 1
do script with command "ssh host.usc.edu -l " & ssh_login & “”
set custom title to “SSH to ”
end tell
end tell
end if

if processExists is false then
tell application “Terminal”
activate
tell window 1
close
end tell
do script with command "ssh host.usc.edu -l " & ssh_login & “”
tell window 1
set custom title to “SSH to ”
end tell
end tell
end if

==========

One of the problems I had was that I couldn’t just have the script close any open terminal windows as this is for a lab and someone might be using terminal. I got the idea to use System Events from a post by Rob’s response in the followig thread and took it from there.

http://bbs.applescript.net/viewtopic.php?t=4312&highlight=running+application

Even simpler:


tell application "Terminal"
	activate
	set custom title of window 1 to "SSH to <host>"
	do script "ssh host -l" & ssh_login in window 1
end tell

The problem was directing the custom title to the appropriate window. Custom title is child of the object window. This language seems to work. Also, I do not understand why you had the extra “” in the do script line? It works fine without.

Hope this helps.
SA
:smiley:

xnetzero,

thats what I thought you should do… I hadn’t used apple events yet … grabbed your script for reference. surely there is a way to get it to run in that initial window …

my app is also part of our campus loadset, and I had just resolved to let terminal open 2 windows if it wasn’t running. Might add this bit in there :slight_smile: Thanks and take it easy !