Spaces in Paths seen as \ in Terminal ??

Hi

I searched the forums for this unsuccessfully but I am guessing its been posted before.

I am trying to use the do shell script command but my path contains a space. When I get the exact path in Terminal it see’s the space as: /Library/Application\ Support/ (so spaces as \ ) but when I add this I get the following error: Expected “” but found unknown token. I read that \ is an Applescript control characer.

Please can anyone tell me the way around this - I’ve tried a ton of combinations unsuccessfully.

Thanks…

Yup. Get the ‘quoted form’ property of your path string. Example:

set posixPath to "/Users/Foo/some folder"
do shell script ("ls " as Unicode text) & quoted form of posixPath

Thanks - I half get that.

Can you give me an example? (I should have mentioned that today is day 2 of Applescripting)…

ie set posixpath to “/Library/Application Support/”
do shell script “sudo whatever” password “xxx” with administator privilges of posixpath?

Many thanks…

ie set posixpath to quoted form of “/Library/Application Support/”
do shell script “sudo whatever” password “xxx” with administator privilges of posixpath?

quoted form will add a ’ before and after whatever you are quoting, so in do shell script if you do, lets say

set posixpath to "/Library/Application Support/"
do shell script "sudo whatever password xxx with administrator privilges of" & quoted form of posixpath

the result in the command line would be

a good way to play around with quoted form is replace “do shell script” with “log” and se for yourself in the log how it comes out.

I think the exact path was “/Library/Application/\ Support/”, wasn’t it? I’m not sure this thread is getting to the core of your question. Both UNIX and Applescript use the backslash as an escape character. If you try this in the terminal:
ls /Library/Application/ Support/
UNIX will list the contents of /Library/Application/, because when it sees the un-escaped space, that is the end of the string because a space is a delimiter to UNIX. By adding the backslash, your are telling UNIX, “don’t use this space as the end of my path”. So we need to have:
/Library/Application/\ Support/
to keep UNIX happy.

So when you try to concatenate the string “/Library/Application/\ Support/” with "ls " as in:

set posix_path to "/Library/Application/\ Support/"
set UNIX_cmd to "ls " & posix_path

It won’t compile because Applescript uses backslash as an escape character just like UNIX but it doesn’t see a space as a delimiter. The “found unknown token” is Applescript’s way of saying, “Why is this backslash here?”. You need to tell Applescript to ignore the backslash; we need another backslash!


set posix_path to "/Library/Application/\\ Support/"
set UNIX_cmd to "ls " & posix_path

This yields “ls /Library/Application/\ Support/” which will work in the “do shell scirpt” step.

I recommend that you use hhas’s solution but I thought an explanation of why you were getting the error was in order. Also, don’t forget that “POSIX path” will convert a Mac path string to UNIX.

There’s an say way to get the backslash you want. Either put in a second backslash or use the ASCII character. I had a Unix path name with a folder that had a space in the name. Of course, you need a backslash for that. I handled it this way:

set nameSlash to ASCII character 92
set cleanPath to " /Volumes/IMAGES/Test" & nameSlash & " Folder"
set cleanUp to (“cd” & cleanPath & “;” & “find . -name "._*" -exec rm ‘{}’ \; -print” & “;” & “find . -name ".DS_Store" -exec rm ‘{}’ \; -print”)
do shell script cleanUp

This works perfectly.

Just as an aside, do not:

Mixing ‘sudo’ and ‘…with administrator privileges’ is not required and will actually break under some versions of the OS.

‘with administrator privileges’ takes care of the authentication element for you. Under pre-10.4 systems it would invoke sudo to call the script, under 10.4 it uses AuthenticationServices to handle it, either way you should not include ‘sudo’ in any ‘do shell script’ command.