BBEdit selection to Safari form

Hi all,

Trying to script out moving large blocks of text from BBEdit to a Safari form. Works fine until the text includes a return, which is a function I really need.

I’m using

tell application "BBEdit"
	set Bodytext to contents of text window 1 as string
end tell

tell application "Safari"
	do JavaScript "document.forms['mainform'].elements['body'].value = \"" & Bodytext & " \"" in document 1
end tell

I’ve tried changing BBEdit’s returns (\r) to Safari’s \n’s, but that doesn’t make any difference. If I don’t use the last Safari tell, the results window show the string I need to use.

How do I get this script to move something with a return in it?

Thanks much,

Perhaps the return characters need to be escaped in some way, beertigger.

I suppose one approach that might be worth trying is to replace the hard returns with soft ones:

on soft_returns(t) (* tid-based search/replace too unreliable here *)
	set d to «data utxt2028» as Unicode text
	set r to t's first paragraph as Unicode text
	repeat with p in rest of t's paragraphs
		set r to r & d & p
	end repeat
	r
end soft_returns

tell application "BBEdit" to set Bodytext to my soft_returns(contents of text window 1)

(* continue with Safari stuff *)

Thanks for the suggestion, Kai. Didn’t work, though. I get the proper result, up to the point where I need to move things back to a Safari form. Then everything breaks, and I get no result…

Anyone else got any sudden flashes of insight? Or a corrective measure for my rampant stupidity?

Thanks,

Hi, beertigger.

Just guessing here. I don’t know if you’ve tried this or if it makes any difference anyway.

Have you tried changing each return to the sequence “\r”? To do that in BBEdit or AppleScript, you have to use two backslashes ” the first one to escape the second: “\r”. You can either use BBEdit’s “Find & Replace” to replace “\r” with “\r”, or do it in the script:

tell application "BBEdit"
	set Bodytext to contents of text window 1 as string
end tell

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\\r"
set Bodytext to Bodytext's paragraphs as string
set AppleScript's text item delimiters to astid

tell application "Safari"
	do JavaScript "document.forms['mainform'].elements['body'].value = \"" & Bodytext & " \"" in document 1
end tell

On receiving the two characters in a string, JavaScript interprets them as an escaped “r” and substitutes a return. But I don’t know if that works in forms.

(Edited the following morning to reduce the amount of code, by using the text’s ‘paragraphs’ instead of getting its ‘text items’ with a delimiter of return.)

Nigel,

That did it! I’d experimented with the returns before, but didn’t quite know what code to use…

Thanks much!

Now… I’m running into a similar problem with code that contains quotes, which I need to transfer html and quotes in text back into Safari. I’m not quite sure of the correct syntax to have AS parse them in a similar way to the returns.

Any help is greatly appreciated.

Thanks again,

Hi, beertigger.

I’m glad the business with the returns actually works. The equivalent for quote characters (if you’re making the change in AppleScript) is probably “\"” ” the first backslash to escape the second and the third to escape the quoted quote!

tell application "BBEdit"
	set Bodytext to text of text window 1 as string
end tell

-- Replace all 'paragraph' endings in the text with the characters backslash and r.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\\r"
set Bodytext to Bodytext's paragraphs as string

-- Replace all quotes in the text with the characters backslash and quote.
set AppleScript's text item delimiters to "\""
set Bodytext to Bodytext's text items
set AppleScript's text item delimiters to "\\\""
set Bodytext to Bodytext as string
set AppleScript's text item delimiters to astid

tell application "Safari"
	do JavaScript ("document.forms['mainform'].elements['body'].value = \"" & Bodytext & " \"") in document 1
end tell

As you know, the complication arises from the fact that you’re creating, in an AppleScript string, a JavaScript command with its own string parameter. The AppleScript string therefore has to contain escaped quote characters, which JavaScript sees as the signs denoting a string in its command. (You’ve already done this.) If the JavaScript string itself is to contain quote characters, these have to be denoted as escaped quotes (backslash-quote) at the JavaScript level. To get the backslash and the quote in the AppleScript string, you have to write an escaped backslash (backslash-backslash) followed by an escaped quote (backslash-quote). These look like four characters in Script Editor, but are only two ” a backslash character and a quote character ” in the AppleScript string itself.

Easy really. :wink:

Nigel,

Works perfectly!

Thanks. Couldn’t quite wrap my head around the need to parse the quotes twice. I’d tried the triple-escape, but didn’t do the initial one prior. That little leap of logic proved a bit much for me; thanks for the help.

This is gonna save me a great deal of cutting and pasting back and forth… I owe you a virtual beer!

Gracias,