Carriage Returns v Linefeeds

If the issue of carriage return versus linefeed never causes you any issues, you can probably stop reading now. Otherwise, you may need to be aware of the change to line-break behavior in Script Debugger 8.

First, a bit of history. Before OS X, Macs used carriage returns as line-breaks, Windows (and DOS) used a carriage return followed by a linefeed, while Unix, then largely confined to expensive workstations and minicomputers, used linefeeds. When Apple adopted OS X, which was based on Unix, there was a clash.

The answer was to be flexible, and much of the OS accepted any of the three options. The AppleScript compiler, which had been built around carriage returns, was also modified to accept them.

In the early stages, script editors still used text editors based on Carbon (pre-OS X) code, which were based on using carriage returns as line-breaks. Updating to newer Cocoa text editors was mostly straight forward, because they happily accepted input with any of the line-break options. So although the AppleScript compiler still produced output with carriage returns as line-breaks — and does to this day — it presented no display problem in the new editors.

There was one area that could cause pain: line-breaks within literal text. Again, passing such text to many apps made no difference because they too were flexible, but for others, using a linefeed instead of a carriage return (or vice-versa) could be disastrous. And spotting the difference in an editing window was impossible.

In the days of Carbon-based editors, hitting the return key added a carriage return. But in Cocoa text editors, it normally enters a linefeed. So suddenly hitting the same key within literal text gave a different result.

Script Debugger responded in a couple of ways. One was an option to display invisible characters, so you can see whether a line-break is a carriage return or linefeed, and the other was a preference to control what was entered by the return key — so you could set it to enter a carriage return, and it behaved like nothing had changed.

But there was another issue. Some people wanted to store their code elsewhere, in version-control systems, and most of them do not recognize carriage returns as line-breaks. The answer was the ability to specify the line ending when saving a script as text. Unfortunately the implementation Script Editor used, and then Script Debugger matched, was a bit of a sledge-hammer: if you chose to use linefeeds, even carriage returns in literal text were replaced.

(There was a subtle third issue: you could click in some code, hit return, and nothing appeared to happen, because the linefeed you just entered was straight after a carriage return supplied by the compiler, so the editor treated the two characters as a single line-break pair.)

Time has moved on, and the carriage return’s role as a line-break is almost gone. Most scripters have developed workable solutions and learned to live with things, but it’s not a situation you could call ideal. We took a fresh look and came up with a different approach for Script Debugger 8.

In Script Debugger 8, linefeeds rule. Although the AppleScript compiler still produces code with carriage returns as line-breaks, Script Debugger 8 replaces them with linefeeds before passing the source to the document. (Of course no changes are made within literal text, or comments.) And when you hit the return key, a linefeed is entered.

That does not mean you cannot have a carriage return in literal text. To enter one, you type \r. And when you compile, you will see the carriage return symbol that was normally only visible with show invisibles turned on. So you can always enter a carriage return, and you will always be able to see where one was entered, avoiding any question of is it or isn’t it.

(The safest thing to do in literal text is to use the defined return and linefeed variables and concatenate, although that’s not always convenient.)

This has also allowed us to simplify saving as text. There are now just two options: macOS (LF), which is effectively what you see in the document on screen, and Legacy (CR), which is same as the old Mac (CR) format, with all linefeeds changed to carriage returns. The new macOS option a more sensible setting for text that be stored in a source-control system.

When opening text documents saved by earlier versions, the characters used in the file will be honored. This means you may see lots of CR symbols at first, but those outside literal text and comments will disappear when the code is compiled, replaced by (invisible) linefeeds.

Lastly, Script Debugger 8 introduces an option for compiled code to display tabs, linefeeds and carriage returns within literal text as escaped characters (\t, \n and \r, respectively). This is controlled by a checkbox in the Editor panel of Preferences.