Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using Applescript to run terminal command

I am completing something at the moment to allow me to automatically rip all my home DVDs, which are quite extensive, quickly and automatically using applescript. I am using handbrake to encode to a file, and handbrake has a command line interface which is very useful and quick. The command I am attempting to run is:

./HandBrakeCLI/HandBrakeCLI -i /Users/bedroom/Ripped Raw Dvds/THE SIMPSONS_MOVIE_SEF1/ -o /Users/home/Movies/The Simpsons/The Simpsons.m4v -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;

But every time I try and use do shell script, or 'do shell script quoted form of' it throws up an error either about the spaces, or the \ I put in to allow the spaces, which terminal needs if I run the command normally through terminal.

Is anyone able to tell me how to send that command from applescript to terminal/run the command? I cant use the GUI version of handbrake, it is too much of a pain.

Thanks in advance

Mac OS X (10.5.1)

Posted on Dec 29, 2007 12:48 PM

Reply
Question marked as Best reply

Posted on Dec 29, 2007 1:27 PM

You would need to escape the spaces or use "quoted form of" for each of the individual file names, for example '/Users/bedroom/Ripped Raw Dvds/THESIMPSONS_MOVIE_SEF1/' or /Users/home/Movies/The\ Simpsons/The\ Simpsons.m4v
5 replies

Dec 29, 2007 1:43 PM in response to red_menace

Thanks for getting back to me so fast. The problem with that is that it doesnt seem to work, I ran the following:

set test to "./HandBrakeCLI/HandBrakeCLI -i '/Users/bedroom/Ripped Raw Dvds/THE SIMPSONS_MOVIE_SEF1/' -o '/Users/bedroom/Movies/The Simpsons/The Simpsons.m4v' -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;" as string
do shell script quoted form of test

and it did not like that, it came up with the error:

sh: line 1: ./HandBrakeCLI/HandBrakeCLI -i '/Users/bedroom/Ripped Raw Dvds/THE SIMPSONS_MOVIE_SEF1/' -o '/Users/bedroom/Movies/The Simpsons/The Simpsons.m4v' -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;: No such file or directory

I also ran:

set test to "./HandBrakeCLI/HandBrakeCLI -i /Users/bedroom/Ripped\ Raw\ Dvds/THE SIMPSONS_MOVIE_SEF1/ -o '/Users/bedroom/Movies/The\ Simpsons/The\ Simpsons.m4v' -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;" as string
do shell script quoted form of test

with the \ in front of the space. It gave the error:

Expected “"” but found unknown token.

Any thoughts?

Dec 29, 2007 2:05 PM in response to Robert Elkin

If you are going to escape the spaces with the backslash ("\"), you need to escape that as well since it is also used as an escape character in AppleScript.

In both of your examples, you are quoting the already quoted form, which changes the quoting (to quote a phrase). It is trying to execute the whole string as a single command - try using just do shell script test

Dec 29, 2007 2:26 PM in response to red_menace

Thanks again. Is there any way to not have to add the \ characters, as it would be a real pain to have to add it in every time there is a space since I am trying to make it automated?

Also, this is how the script looks:

set test to "./HandBrakeCLI/HandBrakeCLI -i /Users/bedroom/Ripped\\ Raw\\ Dvds/THE SIMPSONS_MOVIE_SEF1/ -o '/Users/bedroom/Movies/The\\ Simpsons/The\\ Simpsons.m4v' -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;" as string
display dialog test
do shell script test

it displays the correct string in the dialog, and then gives the error:

"sh: line 1: ./HandBrakeCLI/HandBrakeCLI: No such file or directory"

and the event log shows:
do shell script "./HandBrakeCLI/HandBrakeCLI -i /Users/bedroom/Ripped\\ Raw\\ Dvds/THE SIMPSONS_MOVIE_SEF1/ -o '/Users/bedroom/Movies/The\\ Simpsons/The\\ Simpsons.m4v' -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;"

running the script directly in the terminal, removing the extra \, makes it run absolutly fine. Any thoughts?

Dec 29, 2007 2:54 PM in response to Robert Elkin

Depending on how you are going to automate it (Finder items, filenames from a shell script, etc), you can just use the quoted form of the file name, for example:

set SourceFIle to quoted form of "/source/file/path name"
set DestinationFile to quoted form of "destination/file/path name"
set ScriptText to "/Applications/HandBrakeCLI/HandBrakeCLI -i " & SourceFIle & " -o " & DestinationFile & " -L -f mp4 -m -e x264 -b 2500 -2 -d -B 160 -T;"
do shell script ScriptText


Using the "./" in front of the executable name is a shortcut for the current directory in Terminal. The shell script probably doesn't have that path in it's shell, so you should use the full path name for the executable or use additional commands to set the current directory.

Using Applescript to run terminal command

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.