.sh script ( closing terminal when script is completed)

Discussion in 'Mac Programming' started by forbade, May 10, 2006.

  1. macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #1
    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 ?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #3
    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.
     
  4. Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #4
    What is your script?
     
  5. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #5
    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?
     
  6. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #6
    what would be even better is if I could bypass the terminal app completely. or at least have it run with out the window.
     
  7. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #7
    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$
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Since your calling from Applescript why can't you close the window from there?
     
  9. macrumors 6502a

    Joined:
    Jun 17, 2003
    #9
    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.
     
  10. macrumors 68020

    theBB

    Joined:
    Jan 3, 2006
    #10
    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"
     
  11. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #11
    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.
     
  12. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #12
    How do you tell applescript to close the window?
     
  13. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #13
    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.
     
  14. macrumors 6502a

    Joined:
    Jun 17, 2003
    #14
    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?
     
  15. Moderator emeritus

    yellow

    Joined:
    Oct 21, 2003
    Location:
    Portland, OR
    #15
    Is there some reason you can just make a shell script and put it in launchd to periodically sync your data?
     
  16. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #16
    or the cron daemon?

    I see Apple advertises the use of launchd instead..
     
  17. thread starter macrumors member

    forbade

    Joined:
    Mar 31, 2006
    Location:
    US
    #17

    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.
     
  18. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #18
    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.
     
  19. macrumors 6502a

    Joined:
    Jun 17, 2003
    #19
    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.
     

Share This Page