Tuesday, October 27, 2020

#1 2012-07-26 07:45:13 pm

jfaughnan
Member
From:: MSP
Registered: 2012-07-27
Posts: 9
Website

Address Book notes with Windows line terminator to Mac - how to?

I'm looking for guidance on an AppleScript that will change Windows CRLF line terminators in Address Book/Contacts Notes to Mac LF terminators.

I've seen script examples here to do many notes manipulations, but not this one. There are OS X services that will transform line terminators, so if there's a way to invoke them I could do that. I believe I can also invoke TextWrangler to make the changes.

The script would walk through every Contact with a Note and apply the transform.

If you're wondering why I'd need to do this, the full (tedious) story is in a blog post [1]. Briefly, if you used MobileMe with both XP/Outlook and OS X/Mail (worked pretty well actually) you could end up with Contact Notes that had windows style line terminations. MobileMe had no trouble with them, Address Book was fine, but iCloud is a mess.

I do have another kludgy way to fix this bug, but an AppleScript solution would be far more elegant. I can run the script on SL, L or ML.

Thanks!

[1] http://tech.kateva.org/2012/06/icloud-t … ected.html

Offline

 

#2 2012-07-27 02:48:42 am

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

Re: Address Book notes with Windows line terminator to Mac - how to?

Hi. Welcome to MacScripter.

AppleScript itself uderstands LFs, CRs, and/or CRLFs as paragraph separators, so it's just a matter of splitting each note text into 'paragraphs' and rejoining these with a 'linefeed' delimiter:

Applescript:

-- Get all the people IDs and notes in one go, for efficiency.
tell application "Address Book" to set {theIDs, theNotes} to {id, note} of people

-- Store the current text item delimiter(s) and set the delimiter to linefeed.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed

try
   -- Work through the parallel lists of IDs and notes obtained above.
   repeat with i from 1 to (count theIDs)
       -- Get the next value from the note list.
       set thisNote to item i of theNotes
       -- If it's not 'missing value', there IS a note for this person.
       if (thisNote is not missing value) then
           -- Split the note text into paragraphs.
           set theParagraphs to thisNote's paragraphs
           -- Get the number of the last "non-empty" one.
           repeat with j from (count theParagraphs) to 1 by -1
               if ((count item j of theParagraphs) > 0) then exit repeat
           end repeat
           -- Coerce the paragraphs (up to the last non-empty one) to a single text, using the linefeed delimiter.
           set editedNote to (items 1 thru j of theParagraphs) as text
           
           -- Set the person's note in Address Book to the edited text.
           tell application "Address Book" to set note of person id (item i of theIDs) to editedNote
       end if
   end repeat
on error msg
   display dialog msg buttons {"OK"} default button 1
end try

-- Restore the old delimiter value.
set AppleScript's text item delimiters to astid

-- Save the Address Book changes.
tell application "Address Book" to save
--> Returns 'missing value'. No known significance.

Edit: Script modified to omit any superfluous line endings at the ends of notes.

Last edited by Nigel Garvey (2012-07-28 04:38:20 am)


NG

Offline

 

#3 2012-07-27 12:02:21 pm

jfaughnan
Member
From:: MSP
Registered: 2012-07-27
Posts: 9
Website

Re: Address Book notes with Windows line terminator to Mac - how to?

Nigel, this is more than I could have hoped for. I will digest and test it, and then share it on my blog and Apple Discussions with a pointer to your work. There aren't many of us with this problem, but we will all be grateful.

Offline

 

#4 2012-07-27 09:38:08 pm

jfaughnan
Member
From:: MSP
Registered: 2012-07-27
Posts: 9
Website

Re: Address Book notes with Windows line terminator to Mac - how to?

Nigel, this seems to have worked.

I wrote up my results here:

http://tech.kateva.org/2012/07/an-apple … -crlf.html

I didn't quote the code as I assume you'd prefer people to see it here and learn about MacSccripter
The only optimizations I could see was that it terminated with "missing value" but perhaps that's expected.

I do have some residual extra line feeds at the ends of notes, but they appear to be benign LF terminations, not CRLF. I'll play around with the script and see if I can eliminate those. In any case, they don't cause a problem so far.

Thank you again, you did a good deed with this one!

Offline

 

#5 2012-07-28 04:41:12 am

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

Re: Address Book notes with Windows line terminator to Mac - how to?

jfaughnan wrote:

I do have some residual extra line feeds at the ends of notes, but they appear to be benign LF terminations, not CRLF.


Hi.

There will have been residual line endings in the note(s) anyway, which the script simply replaced with linefeeds. I've now modified the script in my post above to omit these.


NG

Offline

 

#6 2012-07-28 04:03:09 pm

jfaughnan
Member
From:: MSP
Registered: 2012-07-27
Posts: 9
Website

Re: Address Book notes with Windows line terminator to Mac - how to?

Thanks Nigel, even nicer results now.

I'm an AppleScript fan.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)