image

What’s The Big Deal With Line Endings?

image

AppleScript makes line endings a complicated issue. Script Debugger does a lot to make them simple, or at least transparent. But sooner or later you may run into problems with line endings, so here’s an explanation.

Imagine that two mighty forces are at work, tussling with line endings:

So, the AppleScript compiler wants your line endings to be CR, but Mac OS X wants your line endings to be LF. The conflict between these forces is always going on. Sometimes this conflict works behind the scenes — for example, the AppleScript compiler will change your line endings to CR, no matter what they were before, and that’s that. But the conflict also arises up front, every time you press the Return key while you’re typing in a script file. At that moment, some character needs to be entered, so what should it be?

Script Debugger helps you deal with this conflict, in two main ways:

Script Debugger also lets you choose what line endings should be used when you save a script as text. This, however, introduces a further complicating factor.

The problem stems from the fact that in AppleScript, a literal string can span multiple lines:

  set s to "	
  "

The integrity of the line-end character within this string (which might be LF or CR) is preserved through decompilation, because behind the scenes the compiled script is just bytecode (and the byte in question is part of a literal string). But now suppose you elect to save this script as text. What should Script Debugger do? The character after the first " is a line ending. If you tell Script Debugger to save with CR line endings or LF line endings, this character will become CR or LF, respectively, regardless of what it is “really” supposed to be (thus possibly altering the functionality of your script).

This is why Script Debugger provides the As Is option (the default), which leaves all line endings in the resulting text file the same as in your compiled script. The totally safe solution, though, is not to use any literal line endings within quoted strings. Don’t use the “escaped” literals "\r" or "\n", because the AppleScript decompiler turns these into actual line endings. Instead, construct your strings in code, generating line endings at runtime (using, for example, the return or linefeed global variable). Of course, if you never save as text, then the issue doesn’t arise in the first place.

Finally, be aware that pasting (or dragging) from a text file into a script can raise line-end character issues. What is pasted is text, which can have any kind of line ending. Script Debugger does not magically convert these as the paste is performed. If there are multi-line string literals in what is pasted, or if the target script is text, incorrect line endings may now be present in the script. Again, show invisibles will be a great help here.



image
Is Script Debugger’s AppleScript the Same as AppleScript Editor’s? Why Do Applications Open Spontaneously?
image