How to deal with Spaces in Filenames?

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:

  1. My
  2. great
  3. ideas.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. 😉


  1. polocanada
    Posted December 15, 2012 at 3:49 am | Permalink

    Thank you. This is helpful. Was able to get the script working finally.

  2. Christopher Heckman
    Posted October 26, 2014 at 12:25 am | Permalink

    I’ve been working on learning how to use Applescript, and it seems like stuff like this ought to be put all in one place. There doesn’t seem to be a website (like for Java), and all the books that I can find either write “Hello, World” programs, or just list technical data without any explanation.

    I’m not asking for “Applescript for Dummies”; I’m asking for “Applescript for Programmers who Know Another Language”.

    In short, we need more webpages like this, which list all the possible commands and what they do.

  3. Posted November 26, 2014 at 1:41 pm | Permalink

    This is kind of a moving target, I’m afraid. The best book about AppleScript, albeit a bit dated, is “AppleScript 1-2-3”.

    “Moving target” because at least one of the examples in the mentioned book won’t work, until one of the commands shown has its parameters changed. I usually check all my scripts after an OS update. 😉

Post a Comment

Your email is never published nor shared. Required fields are marked *, comments are moderated.