+4  A: 

You have a trailing space on the end of a line, after the final backslash. It thinks you're trying to escape a space, not join lines.

edit: I'll even tell you how I knew, so you know for next time. From the error text, '040' is an octal number. Convert to decimal, 4 * 8 = 32, and ASCII 32 = space. A space is not a valid escape character. Some editors have a 'show whitespace' mode which'll show you tabs and spaces to help weed out issues like this.

Graham Perks
Graham, I had already checked every line and there was no trailing space. The problem actually occured in the middle of a line rather than at the end.
Ian Turner
Ah, OK. I figured it was a line end since the error was showing near the line end. ANyway, glad you figured it out! Those whitespace issues are nearly invisible.
Graham Perks
A: 

Found the problem, there was a rogue space on the line starting set value of cell. The working version is:

set value of cell \"%@\" to \%@\\n\
Ian Turner
Told you that... ;)
Graham Perks
+4  A: 

Don't open yourself up to this sort of problem in the first place.

Objective-C, like all modern C compilers, automatically concatenates string literals if they are seperated by whitespace. Rather than your multi-line horror with its trailing slashes, use:

 NSString *theCellRefScript = [NSString stringWithFormat:@"tell application \"Numbers\"\n"
                                                "tell document 1\n"
                                                "tell sheet \"%@\ \n"
                                                "tell table \"%@\ \n"
                                                "set value of cell \"%@\" to \%@ \n"
                                                "end tell\n"
                                                "end tell\n"
                                                "end tell\n"
                                                "end tell", theSheet, theTable, theCell, [theDistributionValues objectAtIndex:k]];
Jeff

related questions