Cobook deleted all blank lines in Contacts' notes Cobook, a relatively new Mac &amp; iPhone contacts / address book replacement, recently trashed my address book. Specifically, the iPhone app deleted every blank line in every note of every contact. The issue was mentioned in passing as fixed in version 1.1.1, but that didn't repair my address book. It's very tough to read notes with no paragraph separation.

How can I get the blank lines back? I didn't notice the problem right away, so while I could restore from back up, I'll lose some recent data.
I wrote an AppleScript to re-create the blank lines that were deleted. It's only approximate – it has to make certain imperfect guesses about when to insert a line, but it's better than nothing.

-- Contacts newline reflow
-- Reintroduces paragraph separation into contacts' notes
-- after a bug in Cobook deleted all blank lines.
-- (Fixed in Cobook v. 1.1.1: http://blog.cobook.co/post/43633583273/cobook-with-google-maps)

-- Based on Nigel Garvey's work at MacScripter
-- http://macscripter.net/viewtopic.php?pid=153540#p153540

-- Get all the people IDs and notes in one go, for efficiency.
tell application "Contacts" to set {theIDs, theFirstNames, theLastNames, theCompanies, theOrgs, theNotes} to {id, first name, last name, company, organization, 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 -- this may now be unnecessary

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 (item i of theCompanies is false) then
            set theName to (item i of theFirstNames) &amp; " " &amp; (item i of theLastNames)
        else
            set theName to (item i of theOrgs)
        end if

        if (thisNote is not missing value) then
            -- Split the note text into paragraphs.
            set theParagraphs to thisNote's paragraphs
            set newParagraphs to {}

            -- PGP block state machine:
            -- 0 not in PGP block
            -- 1 First line of PGP block (always "-----BEGIN PGP MESSAGE-----")
            -- 2 In PGP block header, not first line
            -- 3 In PGP block body, first line
            -- 4 In PGP block body, not first line
            -- 5 Last line of PGP block (always "-----END PGP MESSAGE-----")

            set pgpBlockState to 0

            repeat with k from 1 to (count theParagraphs)
                set thisParagraph to (item k of theParagraphs)
                set appendEmptyParagraph to true

                if pgpBlockState is 0 then
                    if (thisParagraph contains "BEGIN PGP MESSAGE") then
                        set pgpBlockState to 1
                    end if
                else if pgpBlockState is 1 then
                    if thisParagraph contains ":" then
                        set pgpBlockState to 2
                    else
                        set pgpBlockState to 3
                    end if
                else if pgpBlockState is 2 then
                    if thisParagraph does not contain ":" then
                        set pgpBlockState to 3
                    end if
                else if pgpBlockState is 3 then
                    set pgpBlockState to 4
                else if pgpBlockState is 4 then
                    if (thisParagraph contains "END PGP MESSAGE") then set pgpBlockState to 5
                else if pgpBlockState is 5 then
                    set pgpBlockState to 0
                end if

                -- Alter output according to pgpBlockState
                if (pgpBlockState is not 0) then set appendEmptyParagraph to false
                if (pgpBlockState is 3) then set end of newParagraphs to "" -- this is a hack, not clean

                -- Conditions under which we don't want a newline
                if (length of thisParagraph &lt; 30) then set appendEmptyParagraph to false
                if (length of thisParagraph &gt; 1 and first character of thisParagraph = "-") then set appendEmptyParagraph to false
                if (length of thisParagraph &gt; 1 and first character of thisParagraph = "•") then set appendEmptyParagraph to false

                if (thisParagraph is not equal to "") then set end of newParagraphs to thisParagraph -- delete any manual paragraph separation
                if (appendEmptyParagraph) then set end of newParagraphs to "" -- remember, newParagraphs is a list, not a string
            end repeat

            if (count of newParagraphs) &gt; 0 then
                -- Get the number of the last "non-empty" one.
                repeat with j from (count newParagraphs) to 1 by -1
                    if ((count item j of newParagraphs) &gt; 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 newParagraphs) as text

                tell application "Contacts" to set note of person id (item i of theIDs) to editedNote
                log "Updated entry for: " &amp; theName
            else
                log "Skipping / no note for: " &amp; theName -- handles Exchange-based notes, which don't equal "missing value" even when blank
            end if

        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 Contacts changes.
tell application "Contacts" to save
--&gt; Returns 'missing value'. No known significance.


There's a long section in the middle which handles PGP blocks that might appear in your address book. It may seem like overkill, but those blocks are super-sensitive to having correct newline placement. If you lack the proper newlines, GPGTools (in its current state) will simply fail when you try to decrypt them and give no useful error indicating the problem.

To run this just paste it in AppleScript Editor and click Run. Pro forma warning: Back up your contacts beforehand.