This is quick and dirty. As I note in my comments, the handler “Replace_Double” only deals with a double set of returns found inside the string. It would more proper to handle both beginning and ending double returns. In addition, this does not deal with the possiblity of more than two consecutive returns. In any event, this should start you on your way.
set org_text to "I don't know
how to do it
I need
some help"
set new_text to Replace_Double(org_text) -- Replace all double returns
set old_delimits to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set text_list to text items of new_text
set AppleScript's text item delimiters to {"_"}
set new_text to text_list as string -- returns have been replaced by underscores
set AppleScript's text item delimiters to old_delimits
(* Recursively replace all double returns found within a string
This will not deal with a string that starts with or ends with a double set of returns *)
to Replace_Double(this_text)
local this_text
set double_return_offset to offset of (return & return) in this_text
if double_return_offset is not 0 then
set this_text to ((characters 1 thru (double_return_offset - 1) of this_text) as string) & ¬
"¢" & ¬
(characters (double_return_offset + 2) thru -1 of this_text) as string
set this_text to Replace_Double(this_text) of me
else
return this_text
end if
end Replace_Double
set theText to "I don't know
how to do it
I need
some help
"
set returnCharacter to ASCII character 10
set text item delimiters to returnCharacter & returnCharacter
set theText to text items of theText
set text item delimiters to "¢"
set theText to theText as string
set text item delimiters to returnCharacter
set theText to text items of theText
set text item delimiters to "_"
set theText to theText as string
set text item delimiters to ""
theText
set oldText to "I don't know
how to do it
I need
some help"
set newText to do shell script ("echo " & quoted form of oldText & " | tr '\\n' \\ | sed 's|\\ \\ |¢|'")
Qwerty Denzel:
ASCII 10 is the UNIX return. Shouldn’t you use ASCII 13? I did not know it was possible to use a string as a text item delimiter. That does simplify things.
set myText to "I don't know
how to do it
I need
some help"
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_"}
set myText to (paragraphs of myText) as text
set AppleScript's text item delimiters to {"__"}
set myText to text items of myText
set AppleScript's text item delimiters to {"¢"}
set myText to myText as text
set AppleScript's text item delimiters to ASTID
return myText