simple applescript question

Discussion in 'Mac Programming' started by kilpajr, Oct 4, 2004.

  1. macrumors regular

    Joined:
    Aug 24, 2004
    Location:
    Auburn, AL
    #1
    I have searched but was unable to find previous posts on this. I want to create a script that will start mysql. Currently, I have to open the terminal, navigate to the directory, use sudo to get admin rights, and then run the command to start it. I know this is probably very simple but I would also like to create other types of scripts. Where can I find information/tutorials on creating scripts? Thanks in advance.
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    I'd suggest putting the commands into a shell script and then calling that shell script from AppleScript with "do shell script" or "do script".
     
  3. thread starter macrumors regular

    Joined:
    Aug 24, 2004
    Location:
    Auburn, AL
    #3
    OK, I am really new to this. I have the commands in a text file right now. How do I create a shell script with these commands? When I have this created what do I do to create the applescript? Sorry for my ignorance. :eek:
     
  4. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    Normally, I start with a plain text file--if you start editing in TextEdit, you need to make sure that it's converted to plain text.

    Here is an example:

    Code:
    #!/bin/tcsh
    # run the folding at home client
    ./FAH5 -local -advmethods -forceasm
    
    The first line signals which shell is to be used to execute the script. I've selected tcsh. Any line starting with a # will not be executed usually. After that, simply enter the commands.

    I usually save my scripts with an extension of .sh to remind me that they're shell scripts. Once it's saved, you must make it executable. Go to that directory (or folder even :)) and type chmod + x and leave a space and type the name of the script's file name.

    After that, use the Script Editor found in /Applications/AppleScript to enter something like the following:

    Code:
    tell application "Terminal"
    	activate
    	do script "cd fahproc1; ./fah.sh"
    	do script "cd fahproc2; ./fah.sh"
    end tell
    
    Substitute your folder for fahproc1 and your script file name for fah.sh.
     
  5. thread starter macrumors regular

    Joined:
    Aug 24, 2004
    Location:
    Auburn, AL
    #5
    Thanks, that's really not that difficult. I am going to try this out now. When I create my shell script, can I put it in a directory like /Documents/Scripts or do I need to put it in the directory where the command is?
     
  6. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #6
    I would suggest putting it in a directory in your user area, such as the one you've suggested and doing the change directory as necessary. That way, when you do your regular backups (you do backups, don't u?), it will be saved and when things in the other directory are totally deleted by an upgrade, your work is still there.
     
  7. thread starter macrumors regular

    Joined:
    Aug 24, 2004
    Location:
    Auburn, AL
    #7
    So the shell script will start executing from whatever directory you put it in? When I create this script, can I just click on it from Finder to start it or is that what the applescript will allow me to do?
     
  8. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #8
    Yes, it will execute from whatever directory.

    I would put the AppleScript in the dock or wherever it's convenient so that you can find it again. In the AppleScript, make sure that it changes to the directory where the shell script exists or you enter the path of the shell script (~/Documents/scripts/thescriptname.sh) into the AppleScript source. Remember that ~ is the character for your home directory.
     
  9. macrumors 604

    ravenvii

    Joined:
    Mar 17, 2004
    Location:
    Melenkurion Skyweir
    #9
    So, if I want to make a exectable file that basically automatically does this command: telnet www.somemudgame.com, yeah just that one line, do I still need Applescript?
     
  10. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #10
    You should really create your own thread for this question.

    However, you would probably be better off only creating an AppleScript with only:

    Code:
    do shell script "telnet www.somemudgame.com"
    
     
  11. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #11
    No.

    Just give your little script the extension .command and make it executable. Double-clicking such a file will automatically open a terminal window and run the commands.
     
  12. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #12
    If you installed the Developer Tools you can avoid Applescript altogether.

    Make your script executable, such as chmod ug+x ./foo.sh
    Then, /Developer/Tools/SetFile -t APPL ./foo.sh

    =-=-=-=

    A second way, if you don't have the Developer Tools:

    Make a directory and give it a name ending with .app (has to be .app), and inside there, make a directory called Contents. inside the Contents directory, put your script file (which needs to have the samee name as the first directory you created, but without the .app extension).

    For example, the following makes a trivial script application that opens your top directory in the Finder:
    Code:
    mkdir OpenTop.app OpenTop.app/Contents
    cd OpenTop.app/Contents
    echo '#!/bin/sh' > OpenTop
    echo "open /" >> OpenTop
    chmod ug+x OpenTop
    cd ../..
    
     
  13. thread starter macrumors regular

    Joined:
    Aug 24, 2004
    Location:
    Auburn, AL
    #13
    OK, I created the shell script below named start_mysql.sh in /Documents/Scripts.
    Code:
    #!/bin/bash
    cd /usr/local/mysql
    ./bin/mysqld_safe &
    I made the file executable by executing: chmod +x start_mysql.sh. Then I created an applescript in the same folder named start_mysql. This file has the following.
    Code:
    tell application "Terminal"
    	activate
    	do script "./start_mysql.sh"
    end tell
    When I run the applescript, terminal opens and attempts to run the command but it does not change directory so the command doesn't run.

    Also, when I click on the applescript file, it only opens applescript. How can I make it just run when I click on it?

    When I run it, terminal stays open. How can I make it just run and close terminal when it is done?
     
  14. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #14
    Again, you don't really need AppleScript for this, changing your shell script's name to start_mysql.command ought to be enough to make it double-clickable.

    If you really want to do it from Applescript, you'll want to use the absolute, rather than relative, path to the shell script.
     
  15. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #15
    Instead of do script "./start_mysql.sh" use do shell script "cd CommandDirectory; ./start_mysql.sh" where CommandDirectory is the directory in which the command is located.

    You may save the script as an application, as well as, a script. Save it as both since you can't edit it as an application.
     

    Attached Files:

Share This Page