Trouble replacing carriage return in find/replace script

Hi

I am new to Applescript and using examples on the forums here I have put together a little find/replace script that I wish to use to replace carriage returns with semi-colons in a list of addresses

The script works perfectly when I search for a character or space or tab etc but when I try to find the carriage returns (eg ASCII character 13, return, “\r” etc.) it does not find these in the chunk of text.



display dialog delAddress                                                  --displays my address
set d to AppleScript's text item delimiters                          --get original delimiters
set AppleScript's text item delimiters to ASCII character 13   -- (a carriage return)
set delAddress to delAddress's text items
set AppleScript's text item delimiters to ";"
set delAddress to delAddress as text
set AppleScript's text item delimiters to d                          --replace original delimiters
delAddress
display dialog delAddress


Does anyone have any ideas how I could fix this?

Thanks

mags

Hi. Welcome to MacScripter.

It may be that your text contains linefeeds (ASCII character 10) rather than returns. You can cover this eventuality by getting the text’s ‘paragraphs’ rather than its text items. This should work with either returns or linefeeds:


display dialog delAddress --displays my address
set delAddress to delAddress's paragraphs
set d to AppleScript's text item delimiters --get original delimiters
set AppleScript's text item delimiters to ";"
set delAddress to delAddress as text
set AppleScript's text item delimiters to d --replace original delimiters
delAddress
display dialog delAddress

By the way, "ASCII character’ and ‘ASCII number’ are deprecated as from Mac OS 10.5. You should use ‘character id’ and ‘id of’ instead. However, AppleScript has ‘return’ and ‘linefeed’ keywords to represent those particular characters.

Thanks for your reply Nigel

Sadly I have tried with linefeeds (ASCII 10) also and actually made a wee script which returned the ASCII code of each character which confirmed that there are definitely carriage returns (ASCII 13) in my text.

Frustrating!

May you run this piece of code ?


display dialog delAddress --displays my address
display dialog delAddress & return & "contain Returns : " & (delAddress contains return) & return & "contain LineFeeds : " & (delAddress contains linefeed)

I’m wondering if your address use the couple CR + LF.

Yvan KOENIG (VALLAURIS, France) jeudi 19 janvier 2012 18:36:42

Thank you Yvan for your reply

Trying that piece of code returns false for both return and linefeed, which is obviously why it isn’t working.

Strangely when I return the ASCII code for each character in turn, I get a 13 (return) at the correct place. Also when I display the delAddress, it has returns on the screen too. Very confused now…

May you send the piece of data to my mailbox ?
With the datas available it would be easier to understand what is wrong.

koenigyvan at mac period com

Yvan KOENIG (VALLAURIS, France) jeudi 19 janvier 2012 19:42:33

Hi.

There’s a Unicode character called a paragraph separator (character id 8233) which the ‘ASCII number’ command thinks is ASCII character 13 ” which is no doubt one of the reasons it’s been deprecated.

character id 8233
ASCII number result --> 13

Setting AppleScript’s text item delimiters to ASCII character 13 doesn’t catch it:

set delAddress to "13a Railway Cuttings" & character id 8233 & "Pring"

display dialog delAddress --displays my address
set d to AppleScript's text item delimiters --get original delimiters
set AppleScript's text item delimiters to ASCII character 13 -- (a carriage return)
set delAddress to delAddress's text items
set AppleScript's text item delimiters to ";"
set delAddress to delAddress as text
set AppleScript's text item delimiters to d --replace original delimiters
delAddress
display dialog delAddress

However, the ‘paragraphs’ method does work.

Maybe you’ve got paragraph separators or something similar in your text.

Thanks again Nigel - appreciate your thoughts.

I’ve tried with the paragraphs option and still having no joy. I’m wondering if part of the problem is that the text originated in the Mail application? I am basically pulling in all the text from emails and trying to output the addresses from there into a csv file.

It does sound like it could be to do with that Unicode separator though or something similatr. I may have to rethink things!

Well there’s also the line separator (character id 8232). ASCII number returns 13 for that and ‘paragraphs’ doesn’t catch it. You can see what’s in your text with:

id of delAddress

PS. Since Leopard (I think), you can hedge your bets by including all likely suspects in the delimiters:

set d to AppleScript's text item delimiters --get original delimiters
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set delAddress to delAddress's text items
set AppleScript's text item delimiters to ";"
set delAddress to delAddress as text
set AppleScript's text item delimiters to d --replace original delimiters

Thanks so much Nigel
The line separator (character id 8232) was exactly the problem and I’ve now been able to use it as the delimiter to grab each line of the addresses that I need.
Learned a few new things in the process too!

Thank you Yvan for your help too

:slight_smile: