Problems with carriage returns in Script Editor

Is there any way to stop the Script Editor from automatically converting ‘r’ to a new line? In my script I want BBEdit to do a Find/Replace finding ‘r’ and replacing with

r

essentially adding paragraph tags to plain text. However, when the script compiles it looks like this:


replace "
" using "</p>
<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

Then when I run the code nothing happens. I think BBEdit ends up looking for ‘n’ or something like that. I’m not sure. I just want a command that looks like this:


replace "r" using "</p>r<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

Thank you for any help you can give.

You want to pass the literal text:

r

to BBEdit, however, in an AppleScript string:

"r"

also means “carraige return,” so you have to escape the escape character:

"\r"

in order for BBEdit to “see” r.

I tried


replace "\r" using "</p>\r<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

but still nothing happens.

I tried


replace "\r" using "</p>
<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

and still nothing.

I then tried


replace "
" using "</p>\r<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}

and there ended up being "r"s throughout the text.

There’s another thing I found out. This is kind of interesting. If I put ‘r’ in the Script Editor and compile, the Editor as usual replaces the ‘r’ with new lines, but then in BBEdit everything works great. However, if I change something and compile again, it doesn’t work. So, maybe I should make sure the script does everything it’s supposed to, and then put in the 'r’s and never touch it again.

Yeah, it sounds like there is a descrepency between Script Editor and BBEdit over which line-ending character r should indicate. Try a regular expression:


replace "(\r|\n)" using "</p>\1<p>" searching in text 1 of text window 1 options {search mode:grep}

That is:


(				start a subpattern
	r			find a carriage return
		|		or
	n			a new line
)				end subpattern

1				means replace with whatever was
				found in subpattern 1

Actually, we don’t have to bother with BBEdit’s replace command at all:


tell application "BBEdit" to set str to contents of window 1

set everyLine to paragraphs of str -- should get any line-ending char

set astids to AppleScript's text item delimiters -- save

-- be explicit about which line-ending character you want,
-- ascii 10 or 13:
--
set AppleScript's text item delimiters to "<p>" & (ASCII character 13) & "</p>"

set str to everyLine as string

set AppleScript's text item delimiters to astids -- restore

tell application "BBEdit" to set contents of window 1 to str

I’m having issues with Carriage returns as well. I made some alterations to a script of mine for Palm Desktop that takes info out of address card fields and uses it to format a new memo.

After the changes the memo’s turn out with gremlin characters instead of actual carriage returns. I tried using r, but applescript just reformats on compile. If I copy and past the text from the Palm memo into another app the gremlins characters turn back into real carriage returns so it I suspect that it’s something about Palm Desktop, but I can’t figure out why its behaviour would have changed after the script was edited…

Any suggestions?

Yes. Script editor 2 uses line feeds instead of returns for its line endings. Traditionally, any instance of “r” in an AppleScript string is replaced with a return character in Script Editor’s display. As far as I can tell from copy/pasting into BBEdit Lite and doing a text search, this is till the case. The “r” is correctly compiled into the running code and into the displayed source code. The next time the script’s compiled, though, the return in the source code is replaced and compiled as a line feed, as though it had just been typed in at the keyboard. I think this has to be regarded as a bug.

I don’t have the full, scriptable version of BBEdit to test this, but you may find using AppleScript’s ‘return’ constant more reliable:

replace return using "</p>" & return & "<p>" searching in text 1 of text window 1 options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}