Mac .sh script ( closing terminal when script is completed)

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
I have an applescript calling a .sh script.

It opens up a termin al window and then executes the script.
all works ok, but I would like the terminal to close once the script is done
executing.
is there any way to close the Terminal window via applescript or the .sh script ?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,643
61
Harrogate
Open Terminal.app

Go (Menu) Terminal->Window Settings

Choose Shell from the popup-menu.

Change "When the shell exits" to "Clost the window"

Hit the "Use Settings as Defaults" button

Try running your script.
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
nah, its giving me problems.
i dont know why.?

It wont close on exit. hm, It executes the script fine,but
It will leave the terminal window running, and if I type exit then it will close
I appended an exit command at the end of the script but it still doenst work.

weird.
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
its actually just a test script right now, so I can work out the bugs with closing the terminal window. I will be using slightly more advanced scripting later on.

Here is the apple script I am using.
_
tell application "Terminal"
activate
do script "./test.sh"
end tell
_


test.sh
_
#!/bin/sh
clear
echo Test
sleep 3
exit
_


I am using Filmaker PRO 8, to execute the applescript, which in turn executes a shell script.

I am going to use this so I can sync database's over our network via ssh.
and other handy things that might be useful through a filemaker button. :D

Just a little pinch?
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
what would be even better is if I could bypass the terminal app completely. or at least have it run with out the window.
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
I guess, applescript executes the .sh file before even applying the .profile to the shell, because it wont let me use alias' in the script, but when I type the alias in the remaining terminal it works fine.

_
./test.sh: line 3: nsm: command not found <nsm alias doesnt work
Mac1:~ mac1$ nsm < now it does....
Last login: Wed May 10 16:16:36 2006
Welcome to Darwin!
SERVER:~ nsm$
_
./test.sh: line 3: nsm: command not found
< so it is running the sh script here, and then starting the terminal prog. ???
mac1:~ mac1$ nsm
Last login: Wed May 10 16:16:36 2006
Welcome to Darwin!
SERVER:~ nsm$
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2
forbade said:
Here is the apple script I am using.
_
tell application "Terminal"
activate
do script "./test.sh"
end tell
_
Why are you activating Terminal at all?

According to http://developer.apple.com/technotes/tn2002/tn2065.html it appears that you should just be able to use:

Code:
do shell script "./test.sh"
My quick test of this indicates that it works. The above tech note may be helpful if you decide that you want to pass arguments to your shell script. Note that you may need to specify the full path to your script in the applescript... I'm not certain what environment applescripts are executed under.
 

theBB

macrumors 68020
Jan 3, 2006
2,453
3
mrichmon said:
Why are you activating Terminal at all?
My thoughts exactly. I use "do shell script ..." as well and it works without any need for Terminal.

If you read the tech note, it warns you about your aliases etc. I believe Applescript just executes the shell scripts in the default shell environment. Tech note tells you how to tell Applescript to execute the commands inside a different shell, but if you need your own aliases, I think you could do something like

do shell script "~/set_aliases.sh; ~/test.sh"
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
Thanks for all the help

I havent had time to research Applescript too much, It really helps to bank off your guys knowledge. I appreciate it.
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
robbieduncan said:
Since your calling from Applescript why can't you close the window from there?
How do you tell applescript to close the window?
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
The problem I have been encountering with
- do shell script "~/lcr/test.sh"
is that it won't let me use ssh, so it works ok for local commands but when I want to rsync via ssh, it gives me problems.
So it looks like I am going to have to invoke the terminal window anyway.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2
forbade said:
The problem I have been encountering with
- do shell script "~/lcr/test.sh"
is that it won't let me use ssh, so it works ok for local commands but when I want to rsync via ssh, it gives me problems.
So it looks like I am going to have to invoke the terminal window anyway.
Yeah, looks like ssh isn't being allowed to access some of the root services it uses when invoked through Applescript.

Reading back over your original post I'm confused as to why you would be using ssh in the shell script. Is there some reason why you cannot just keep an ssh tunnel open between the two machines and then rsync localhost:sshtunnelport?
 

forbade

macrumors member
Original poster
Mar 31, 2006
33
0
US
kingjr3 said:
or the cron daemon?

I see Apple advertises the use of launchd instead..

Yes, but.
I need be able to update specific laptops that are mobile. They are not always on the network, so I want to be able to have a button within Filemaker that will update automaticly before the Person leaves for home etc etc.
I just want a manual option available. Several of our office personal are in and out of the office constantly, its more for them than anything else.
 

Krevnik

macrumors 68040
Sep 8, 2003
3,388
720
Well, how is ssh configured currently? If you are using keyboard authentication, it will fail. If you use public-key authentication, you will be in a much better situation.

For example, I just ran the following script:

do shell script "touch ~/mymonkey;scp ~/mymonkey <host-deleted>:~/mymonkey"

It worked just fine... the host now had the file: ~/mymonkey.

So, your script /will/ work, but you will have to setup those portable machines with public key authentication for SSH to work 'simply'. That or ask for the username/password in the applescript, and then pass that along to the script to use and pass to SSH.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2
Krevnik said:
For example, I just ran the following script:

do shell script "touch ~/mymonkey;scp ~/mymonkey <host-deleted>:~/mymonkey"
If you try that using "ssh <some host> uname" to execute "uname" on the remote system you may find that it doesn't work. At least it didn't work on my OS X systems.