Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 4, 2004, 06:31 PM   #1
kilpajr
macrumors regular
 
Join Date: Aug 2004
Location: Auburn, AL
Send a message via AIM to kilpajr
simple applescript question

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.
__________________
15" Powerbook: 1.5 GHz G4 / 1024 MB RAM / 128 MB 9700M / 5400 80GB HDD
4G 20GB iPod

What % of your CPU are you using right now? Donate the rest to a good cause: Folding@Home. Join team 3446.
kilpajr is offline   0 Reply With Quote
Old Oct 4, 2004, 06:44 PM   #2
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
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".
bousozoku is offline   0 Reply With Quote
Old Oct 4, 2004, 06:57 PM   #3
kilpajr
Thread Starter
macrumors regular
 
Join Date: Aug 2004
Location: Auburn, AL
Send a message via AIM to kilpajr
Quote:
Originally Posted by bousozoku
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.
__________________
15" Powerbook: 1.5 GHz G4 / 1024 MB RAM / 128 MB 9700M / 5400 80GB HDD
4G 20GB iPod

What % of your CPU are you using right now? Donate the rest to a good cause: Folding@Home. Join team 3446.
kilpajr is offline   0 Reply With Quote
Old Oct 4, 2004, 07:14 PM   #4
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by kilpajr
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.
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.
bousozoku is offline   0 Reply With Quote
Old Oct 4, 2004, 07:42 PM   #5
kilpajr
Thread Starter
macrumors regular
 
Join Date: Aug 2004
Location: Auburn, AL
Send a message via AIM to kilpajr
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?
__________________
15" Powerbook: 1.5 GHz G4 / 1024 MB RAM / 128 MB 9700M / 5400 80GB HDD
4G 20GB iPod

What % of your CPU are you using right now? Donate the rest to a good cause: Folding@Home. Join team 3446.
kilpajr is offline   0 Reply With Quote
Old Oct 4, 2004, 07:55 PM   #6
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by kilpajr
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.
bousozoku is offline   0 Reply With Quote
Old Oct 4, 2004, 08:04 PM   #7
kilpajr
Thread Starter
macrumors regular
 
Join Date: Aug 2004
Location: Auburn, AL
Send a message via AIM to kilpajr
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?
__________________
15" Powerbook: 1.5 GHz G4 / 1024 MB RAM / 128 MB 9700M / 5400 80GB HDD
4G 20GB iPod

What % of your CPU are you using right now? Donate the rest to a good cause: Folding@Home. Join team 3446.
kilpajr is offline   0 Reply With Quote
Old Oct 4, 2004, 09:03 PM   #8
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by kilpajr
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 is offline   0 Reply With Quote
Old Oct 4, 2004, 10:24 PM   #9
ravenvii
macrumors 604
 
ravenvii's Avatar
 
Join Date: Mar 2004
Location: Melenkurion Skyweir
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?
__________________
59 6F 75 20 73 70 6F 6F 6E 79 20 62 61 72 64 21
ravenvii is offline   0 Reply With Quote
Old Oct 4, 2004, 10:45 PM   #10
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by Raven VII
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:

Code:
do shell script "telnet www.somemudgame.com"
bousozoku is offline   0 Reply With Quote
Old Oct 5, 2004, 02:12 AM   #11
iMeowbot
macrumors 601
 
iMeowbot's Avatar
 
Join Date: Aug 2003
Quote:
Originally Posted by Raven VII
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 is offline   0 Reply With Quote
Old Oct 5, 2004, 02:31 AM   #12
iMeowbot
macrumors 601
 
iMeowbot's Avatar
 
Join Date: Aug 2003
Quote:
Originally Posted by kilpajr
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 ../..

Last edited by iMeowbot; Oct 5, 2004 at 02:47 AM.
iMeowbot is offline   0 Reply With Quote
Old Oct 6, 2004, 12:59 AM   #13
kilpajr
Thread Starter
macrumors regular
 
Join Date: Aug 2004
Location: Auburn, AL
Send a message via AIM to kilpajr
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?
__________________
15" Powerbook: 1.5 GHz G4 / 1024 MB RAM / 128 MB 9700M / 5400 80GB HDD
4G 20GB iPod

What % of your CPU are you using right now? Donate the rest to a good cause: Folding@Home. Join team 3446.
kilpajr is offline   0 Reply With Quote
Old Oct 6, 2004, 07:08 PM   #14
iMeowbot
macrumors 601
 
iMeowbot's Avatar
 
Join Date: Aug 2003
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.
iMeowbot is offline   0 Reply With Quote
Old Oct 6, 2004, 07:48 PM   #15
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by kilpajr
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.
Attached Thumbnails
Click image for larger version

Name:	SaveScriptAs.jpg
Views:	17
Size:	13.4 KB
ID:	17883  
bousozoku is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Help needed for simple Applescript duckdealer Mac Programming 2 Apr 7, 2014 10:54 PM
Simple AppleScript for mail.app JosCiv OS X 0 Jul 26, 2013 11:32 AM
Simple applescript question Pecans Mac Programming 2 Jun 6, 2013 02:58 AM
Why doesn't this simple applescript work any more in ML mail.app (as a rule) wesleyh Mac Programming 0 Jan 26, 2013 09:25 AM
looking for a simple code in automator or applescript for copy and paste duran118 Mac Applications and Mac App Store 0 Jul 25, 2012 01:24 AM

Forum Jump

All times are GMT -5. The time now is 02:26 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC