problem with directory and filenames containing spaces

This may be more of a OSX issue then applescript but how do I issue ‘ssh’ commands via ‘do shell’ with arguments, particularily directory and filename, that contain spaces? The traditional Unix methods of quotes or back slash don’t seem to work:


do shell "ssh username@myhost.mydomain.com ls "Documents:Business Forms""

The result is an error message saying Documents:Business Forms: No such file or directory. Which is untrue. TIA.

  1. Your script shouldn’t even complie, as far as I know “do shell” is not an Applescript command.
  2. You are combining two commands in the code “ssh & ls”, they would need to be separated by a semi-colon. But ssh needs to authorize your password before you can issue any commands.
  3. I’m not sure that “ssh username@myhost.mydomain.com” would work even from the terminal, but I’ve never used it in that form. I use “ssh IPAddress -l username” then I’m prompted for my password. Actually, the form you are using is an email address???

Read the following web page from Apple (by Chris Nebel)…
http://developer.apple.com/technotes/tn2002/tn2065.html#Section5

Before jumping into an Applescript, you should test the things in the terminal.app to be sure they are working. If they do work, then you can save them in an Applescript file.

Actually Greg, everything looks good with the SSH command. There shouldn’t be a semi-colon between the SSH and the ls command because the latter is actually a parameter to the SSH command. It’s telling SSH to execute ls on the “other side.” If you put a semi-colon to separate them, then the ls will be executed (eventually) on the local machine, not the remote. Also, userID@server.domain.name is a legal way to specify a remote host.

I think the only thing gw1500se did wrong was to use “do shell” instead of do shell script. Quoting looks to be good. Although it is possible that when you SSH to a remote machine, the default directory might not be the same as when you log in locally. In other words, a full path to Directory may be necessary (in the ls command). YMMV.

I forgot to mention: if public key pair authentication has been set up between these two machines, there would be no password prompt.

One more thing…

As previously noted, you need to use do shell script. As regards quoting arguments, like filenames with spaces, use the AppleScript quoted form of.

For example, if you have an AppleScript variable containing a Finder alias to a local file, you can transform it into a UNIX style path with POSIX path of, and then use quoted form of to properly quote it for UNIX, like this:

set theFile to choose file with prompt "Please choose a file with a space in its name"
return do shell script "ls -al " & quoted form of (POSIX path of theFile)

Hope this helps.

Thanks for the reply. The missing ‘script’ keyword was a typo. The syntax in my actual script was correct with the exception of the ‘quoted form of’. Unfortunately, I must be misunderstanding your syntax. I still get the same error. Here is what I have now:


set remote_command to "ls -l " & quoted form of (POSIX path of "Documents:Business Forms")
do shell script "ssh userid:10.0.0.1 " & remote_command

P.S. The following works, however it has the obvious problem with uniqueness:


set remote_command to "ls -l Documents:Business*")
do shell script "ssh userid:10.0.0.1 " & remote_command

Oops. I overlooked the colon in the pathname originally. That is wrong and is probably the source of the error. Should be a slash. Try:

do shell script “ssh username@myhost.mydomain.com ls “Documents/Business Forms””

and see if that works. If it doesn’t, try:

do shell script “ssh username@myhost.mydomain.com ls”

to see where you are in the directory structure.

Regarding your most recent post, POSIX path of “Documents:Business Forms” will return: “/Documents/Business Forms”. Note the leading slash. Not what you want. You have to feed POSIX path of a complete MacOS filepath (i.e. starting with the HD name).

But you could do something like this:

set theFile to “Documents/Business Forms”
do shell script "ssh username@myhost.mydomain.com ls " & quoted form of theFile

or simply:

do shell script "ssh username@myhost.mydomain.com ls " & quoted form of “Documents/Business Forms”

Thanks for the suggestions. Did you actually try them? I thought of using the backslash some time ago (although I tried to escape the spaces) but when I try it won’t compile. At the backslash I get: ‘Expected “”" but found unknown token.’

I did the ‘ls’ a long time ago. I’ve pretty much hit a wall other then using wildcards.

Yes, they all work for me. However, a i didn’t try them with a space in the directory path, and when i tried that, i got an error. So i dropped into Terminal to debug it and found that, for whatever reason (which i don’t fully understand), the entire ls command needs to be quoted if the pathname contains a space. So try this command:

set theFile to "Documents/Business Forms" 
do shell script "ssh username@myhost.mydomain.com "ls " & quoted form of dirPath & """

In certain situations, you have to backslash the backslash. The reason for this is because you want don’t want AppleScript interpreting the backslash, but rather to pass it along to the shell. A space in a pathname is one case where you sometimes need to do this double backslash (depending on the quoting).

Thanks for the suggestion but it doesn’t seem to help. However, I have to build the command so my command looks like:


do shell script "ssh username@myhost.mydomain.com "" & remote_command & """

It stills seems odd that “”" is OK but " " gives an error.

The backslash is an escape character in the shell and in AppleScript. This can sometimes make it very interesting and frustrating, even for experienced scripters.

In “”", the backslash is required by AppleScript to escape the second quote. Result passed to shell: "

In " ", the backslash needs to be passed to the shell as well, to escape the space for the shell, so it needs to be escaped too, requiring another AppleScript required backslash to do so. So, "\ " would result in space being passed to the shell.

– Rob

Here’s a tip in debugging do shell script commands: use display dialog to preview the command; all the quotes and everything should appear as you would type it into the Terminal. That way you can see that you’ve got everything properly backslashed, etc.

What is it you’re trying to do anyhow? There might be a simpler way to go about it.