Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kilpajr

macrumors regular
Original poster
Aug 24, 2004
122
0
Auburn, AL
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

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
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

macrumors regular
Original poster
Aug 24, 2004
122
0
Auburn, AL
bousozoku said:
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. :eek:
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
kilpajr said:
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:

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.
 

kilpajr

macrumors regular
Original poster
Aug 24, 2004
122
0
Auburn, AL
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

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
kilpajr said:
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

macrumors regular
Original poster
Aug 24, 2004
122
0
Auburn, AL
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

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
kilpajr said:
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
Raven VII said:
So, if I want to make a exectable file that basically automatically does this command: telnet http://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:

Code:
do shell script "telnet www.somemudgame.com"
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Raven VII said:
So, if I want to make a exectable file that basically automatically does this command: telnet http://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

macrumors G3
Aug 30, 2003
8,634
0
kilpajr said:
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:
Code:
mkdir OpenTop.app OpenTop.app/Contents
cd OpenTop.app/Contents
echo '#!/bin/sh' > OpenTop
echo "open /" >> OpenTop
chmod ug+x OpenTop
cd ../..
 

kilpajr

macrumors regular
Original poster
Aug 24, 2004
122
0
Auburn, AL
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?
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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

Moderator emeritus
Jun 25, 2002
15,603
1,761
Lard
kilpajr said:
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?

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.
 

Attachments

  • SaveScriptAs.jpg
    SaveScriptAs.jpg
    13.4 KB · Views: 262
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.