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?
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)
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.
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” ???
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.
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.
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.
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.
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 Thanks and take it easy !