Applescript text delimiter replace line to /n

applescripttext;

I'm trying to remove line and replace them by "\n" (to them add the final text in javascript with actual line)

set myCaseNote to "e.g
here
and
there"

set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set myCaseNote to text items of myCaseNote
set AppleScript's text item delimiters to {" "}
set myCaseNote to myCaseNote as text

I can manage to remove the line but I can't replace them with the text "\n", how can I proceed ?

Best Answer

Since you didn't actually say what's going wrong when you try to insert \n into your string, I had a little think and then I remembered that there's a setting in Script Editor:

Script Editor settings on macOS

So, if I'm guessing along the right lines here, I'm suspecting you currently have this option unchecked, which means that whenever you try to type an escaped line-break like "\n", Script Editor automatically replaces it with a physical line-break whilst you're editing, which then leads to your syntax error issues with JavaScript.

[ Don't forget to escape the newline character twice before sending it to your JavaScript code if doing so within AppleScript: \\n. If you are entering it directly into the developer console inside the web browser, the single escape \n is fine. ]

So, Option 1, is to adjust this formatting setting in Script Editor. Option 2 is to stop using Script Editor and use something like Script Debugger or Atom.