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-transition-went-as-expected.html
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:
-- 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.
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.
Nigel, this seems to have worked.
I wrote up my results here:
http://tech.kateva.org/2012/07/an-applescript-to-prevent-icloud-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!
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.
Thanks Nigel, even nicer results now.
I’m an AppleScript fan.