PDA

View Full Version : simple applescript question




kilpajr
Oct 4, 2004, 06:31 PM
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.



bousozoku
Oct 4, 2004, 06:44 PM
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".

kilpajr
Oct 4, 2004, 06:57 PM
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".

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. :o

bousozoku
Oct 4, 2004, 07:14 PM
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. :o

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:


#!/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:


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.

kilpajr
Oct 4, 2004, 07:42 PM
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?

bousozoku
Oct 4, 2004, 07:55 PM
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?

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.

kilpajr
Oct 4, 2004, 08:04 PM
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?

bousozoku
Oct 4, 2004, 09:03 PM
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?

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.

ravenvii
Oct 4, 2004, 10:24 PM
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?

bousozoku
Oct 4, 2004, 10:45 PM
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?

You should really create your own thread for this question.

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


do shell script "telnet www.somemudgame.com"

iMeowbot
Oct 5, 2004, 02:12 AM
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?
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.

iMeowbot
Oct 5, 2004, 02:31 AM
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.
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:

mkdir OpenTop.app OpenTop.app/Contents
cd OpenTop.app/Contents
echo '#!/bin/sh' > OpenTop
echo "open /" >> OpenTop
chmod ug+x OpenTop
cd ../..

kilpajr
Oct 6, 2004, 12:59 AM
OK, I created the shell script below named start_mysql.sh in /Documents/Scripts.
#!/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.
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?

iMeowbot
Oct 6, 2004, 07:08 PM
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.

bousozoku
Oct 6, 2004, 07:48 PM
OK, I created the shell script below named start_mysql.sh in /Documents/Scripts.
#!/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.
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?

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.