Batch append data to the Notes field on Address Book cards

Mar 22, '10 07:30:00AM

Contributed by: robleach

I've been going crazy with Address Book lately -- ever since I started to Sync with Mobile Me. One thing I've been doing is creating Smart Group, but I've run into some pretty buggy behaviors; this was especially true for rules. I eventually discovered that the most reliable thing to do is to use one rule that looks for a special string in the Note field. However, adding that string to each card's Note field can be tedious, and I wanted to do this repeatedly for tons of cards, so I struggled with AppleScript until I had the following solution.

display dialog "Enter value to append to Notes of selected Address Book Cards" default answer "" buttons {"Cancel", "OK"} default button 2
set the append_string to the text returned of the result

tell application "Address Book"
  activate
  set thePeople to selection
  repeat with thePerson in thePeople
    set noteValue to note of thePerson
    
    try
      set theLastCharPos to length of noteValue
    on error
      set noteValue to ""
      set theLastCharPos to length of noteValue
    end try
    
    if theLastCharPos is greater than 0 then
      set theCharacter to character theLastCharPos of noteValue
      if "
" contains theCharacter then
        set newNoteValue to noteValue & append_string
      else if theLastCharPos is greater than 1 then
        set newNoteValue to noteValue & ";" & append_string
      else if theLastCharPos is equal to 1 and " " contains theCharacter then
        set newNoteValue to append_string
      else
        set newNoteValue to noteValue & ";" & append_string
      end if
    else
      set newNoteValue to append_string
    end if
    set note of thePerson to newNoteValue
  end repeat
end tell
Here's how to use this script: Paste this script into Script Editor. In Address Book, select the cards you want to append a note to, then run the script. Enter the string you want to append in the dialog box and click OK. (One feature I did not add is the ability to see if the string had already been added.)

If there's a script out there that does this already, I could not find it. Also, I'm not very familiar with AppleScript, so I know that there are some pretty dumb things I did in this script (improvements are encouraged in replies).

[robg adds: This worked as described in my testing.]

Comments (20)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100318152934603