Friday, May 27, 2022

#1 2012-01-19 07:51:47 am

magsm5
Member
Registered: 2012-01-19
Posts: 5

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.

Applescript:



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


Filed under: find, return, replace

Offline

 

#2 2012-01-19 08:14:49 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5514

Re: Trouble replacing carriage return in find/replace script

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:

Applescript:


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.

Last edited by Nigel Garvey (2012-01-19 08:21:13 am)


NG

Offline

 

#3 2012-01-19 08:25:48 am

magsm5
Member
Registered: 2012-01-19
Posts: 5

Re: Trouble replacing carriage return in find/replace script

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!

Offline

 

#4 2012-01-19 11:37:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Trouble replacing carriage return in find/replace script

May you run this piece of code ?

Applescript:


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

Offline

 

#5 2012-01-19 12:17:33 pm

magsm5
Member
Registered: 2012-01-19
Posts: 5

Re: Trouble replacing carriage return in find/replace script

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...

Offline

 

#6 2012-01-19 12:42:58 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Trouble replacing carriage return in find/replace script

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

Offline

 

#7 2012-01-19 02:16:29 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5514

Re: Trouble replacing carriage return in find/replace script

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.

Applescript:

character id 8233
ASCII number result --> 13

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

Applescript:

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.


NG

Offline

 

#8 2012-01-19 03:01:05 pm

magsm5
Member
Registered: 2012-01-19
Posts: 5

Re: Trouble replacing carriage return in find/replace script

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!

Offline

 

#9 2012-01-19 03:27:51 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5514

Re: Trouble replacing carriage return in find/replace script

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:

Applescript:

id of delAddress

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

Applescript:

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

Last edited by Nigel Garvey (2012-01-19 03:41:14 pm)


NG

Offline

 

#10 2012-01-19 05:41:11 pm

magsm5
Member
Registered: 2012-01-19
Posts: 5

Re: Trouble replacing carriage return in find/replace script

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

smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)