8

How would I go about creating an AppleScript command that when I just run the script (or double click it in Finder?), it would run a set of terminal commands? The set of commands completely remove MySQL, and it has become a pain to constantly write them out. The commands are:

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf

There is also another command sudo nano /etc/hostconfig that opens a file and I need to delete a line from the file, but that seems like it'd be too hard to code, so I guess I can do that by hand. But it would be a huge help to do this automatically with a single script.

Would it just be a bunch of these commands?

do shell script (...)

Thanks, Hristo

Hristo
  • 45,559
  • 65
  • 163
  • 230

3 Answers3

16

Yes, you would use do shell script.

However, for the commands where you execute as super user (sudo) you would instead use with administrator privileges. So, for sudo rm /usr/local/mysql you'd do:

do shell script "rm /usr/local/mysql" with administrator privileges

If your list of commands is long, then it might just be easier to put all your commands into a single shell script file, and then execute that shell script using do shell script:

do shell script "/path/to/shell/script" with administrator privileges
indragie
  • 18,002
  • 16
  • 95
  • 164
  • when I do `do shell script "rm /usr/local/mysql" with administrator privileges`, it gives me an error and tells me that `/usr/local/mysql` is a directory. That is actually a symbolic link to a different directory... what is the problem? – Hristo Jul 01 '10 at 01:34
  • try `rm -rf /usr/local/mysql` – indragie Jul 01 '10 at 02:21
16

You don't actually need to use AppleScript for this - just put all the shell commands in a text file and give it a .command suffix and make sure it's executable (e.g. chmod +x my_script.command) - this will make it double-clickable in the Finder.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 4
    The `.command` suffix isn't quite enough -- you also have to include a shebang (i.e. add `#!/bin/bash` as the first line of the text file) and add execute permission to the file (i.e. run `chmod +x /path/to/file.command`). BTW, the big advantage of using this vs. an AppleScript is that it runs in Terminal, so you can actually interact with the script (very important if you want to use e.g. `nano` to edit a file). – Gordon Davisson Jul 01 '10 at 19:58
  • @Gordon: yes, good point, I should have said that it needs to be an executable shell script with a shebang line. – Paul R Jul 01 '10 at 20:26
  • 1
    note, though, that when using `sudo` in a .command you'll still have to enter your password. if you want the script to just run on its own, this link has some good tips on how to do that: http://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password – ericsoco May 15 '13 at 00:13
0

I find that .scpt files work best but this is just a preference thing.

Open "Script Editor" and add the following command:

sudo rm -rf /usr/local/mysql; sudo rm -rf /usr/local/mysql*; sudo rm -rf /Library/StartupItems/MySQLCOM; sudo rm -rf /Library/PreferencePanes/My*; sudo rm -rf /Library/Receipts/mysql*; sudo rm -rf /Library/Receipts/MySQL*; sudo rm /etc/my.cnf; say job completed successfully" with administrator privileges
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Beejster
  • 1
  • 4