Do shell script help

I found a good tip on speeding up Firefox here, using the Terminal to vacuum the sqlite databases. The commands as given on the page are:

for f in ~/Library/Application\ Support/Firefox/Profiles//.sqlite; do sqlite3 “$f” ‘VACUUM;’; done

or

cd ~/Library/Application\ Support/Firefox/Profiles/ for f in /.sqlite; do sqlite3 $f ‘VACUUM;’; done

So I thought I’d put it in an Applescript which I could just double click and run. So far I have:

display dialog "Vacuum Firefox SQLite Database?" buttons {"Cancel", "OK"} default button 2
if the button returned of the result is "OK" then
	do shell script "cd ~/Library/Application\\ Support/Firefox/Profiles/ for f in */*.sqlite; do sqlite3 $f 'VACUUM;'; done"
end if

However I’m getting an error:

“sh: -c: line 0: syntax error near unexpected token do' sh: -c: line 0: cd ~/Library/Application\ Support/Firefox/Profiles/ for f in /.sqlite; do sqlite3 $f ‘VACUUM;’; done’”

If I use the other Terminal command under do shell script I get the error “SQL error: near “Support”: syntax error” repeated a few times. The code I’m using for that is:

display dialog "Vacuum Firefox SQLite Database?" buttons {"Cancel", "OK"} default button 2
if the button returned of the result is "OK" then
	do shell script "for f in ~/Library/Application\\ Support/Firefox/Profiles/*/*.sqlite; do sqlite3 $f 'VACUUM;'; done"
end if

I’m a total novice at Applescripting or any scripting for that matter, but I think it’s to do with the space between Application Support coupled with the “for f” command. I thought a double escape would fix that but I’m clearly missing something here.

Any help is most welcome.

Thanks.

First try running the command in the terminal and find out if it works there. On my setup,

for f in ~/Library/Application\ Support/Firefox/Profiles//.sqlite; do sqlite3 “$f” ‘VACUUM;’; done

typed in the terminal has the desired result. It is slightly different from your command. Note the quotes around the $F that allows the for - do - done loop to function properly with file names with spaces in them.

When you translate that to an applescript, you are correct in adding an extra '' and you also have to escape the double quote marks. Finally, you have to give the absolute path to the sqlite3 program as your environment may be different in do shell script than from the terminal.

So, try this:

set the_script to “for f in ~/Library/Application\ Support/Firefox/Profiles//.sqlite; do /usr/bin/sqlite3 "$f" ‘VACUUM;’; done”
do shell script the_script

Andy

Browser: Firefox 3.0.7
Operating System: Mac OS X (10.5)

Yeah, I removed them from the original Terminal command whilst I was trying to get it working, I didn’t realise it had such an implication. However, escaping the quotation marks makes perfect sense and I feel a bit dumb not realising that.

The script now works perfectly, thanks!