As an AppleScripter, you always have to deal with the creativity of your users regarding filenames. A simple fact of life. AppleScript is handing you the tools on a silver platter. So, let’s deal with it.
Why is a Space character a problem?
A short detour is in order, I presume. The Mac (and the iPhone/iPad) encourage you to name things naturally, so instead of MYGRIDEA.TXT Mac users tend to use My great ideas.txt as filename, the same holds true for directory names. OS X is able to deal with spaces in filenames or paths without problems, the UNIX-underpinnings not so much. This means, should you try to feed the latter filename to a shell command, interesting things will happen.
Open AppleScript Editor and type the following line
set myFileName to "My great ideas.txt"
and click “Run” to see the result in the lower pane of the editor window. I should look like this: "My great ideas.txt".
Now if you want to hand this filename over to a command like print, grep, etc. then these programs will see not one single argument but three arguments:
Mygreatideas.txt
because the space character is used as a so called delimiter, thus separating (or splitting) your filename into three arguments to the program.
How does AppleScript helps me dealing with Spaces?
Good question, simple answer: Just tell AppleScript to do so. The magic happens by using quoted form of. For example
set myFileName to quoted form of "My great ideas.txt"
gives you the following result: "'My great ideas.txt'". The key here is the quoting done by ', these single quotes will be handed over to the program as part of the argument, thus telling the program to leave the spaces inside the quotes alone.
I told you, you just have to ask.