Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single quote escaping in applescript/shell

I'm trying to process this shell script in applescript, but it keeps saying error EOF, because of the single quote in the folder name.

do shell script "cp -R " & a & " " & "'" & d & "/My 'Folder/java/" & "'"

The folder /My 'Folder/ is a legitimate directory.

The variable a = '/Applications/MyProgram/' (and includes the single quotes)

The variable d = /Folders (with no single quotes)

However, shell is getting stuck processing it, im guessing because the folder is enclosed in quotes.

Is there any way to escape this single quote, so it works in applescript using shell? Ive tried multiple backslashes but its not working.

Cheers

N

like image 916
Ke. Avatar asked Feb 06 '15 03:02

Ke.


Video Answer


1 Answers

Always use quoted form of when using arbitrary data as an argument to an command. This will always quote the value even if it doesn't need to be quoted but it's better safe than sorry. When you have a string single quoted, you can only unquote (turn substitution mode back on) with another quote. Unlike AppleScript strings, you can't escape characters inside single quoted strings. So you need to turn substitution mode on, escape a quote and then turn substitution mode back one. For instance "Joe's car" should be quoted as "'Joe'\\''s car'". It's a quoted string "Joe" + escaped quote character + quoted string "s car". But like I started you should use quoted form of, quoted form of "Joe's car" will return "'Joe'\\''s car'"

Your command using quoted form will look like:

do shell script "cp -R " & quoted form of a & space & quoted form of (d & "/My 'Folder/java/")
like image 92
dj bazzie wazzie Avatar answered Sep 29 '22 01:09

dj bazzie wazzie