Split first and last names in Address Book via AppleScript

Aug 16, '07 07:30:00AM

Contributed by: thomas_witt

Lots of mobile phones do not support the possibility of entering a first and a last name for a contact. When first syncing the phone with the Mac, all names like "John Doe" are contained in the last name field, and have an empty first name. The following AppleScript splits the names correctly:

tell application "Address Book"
  repeat with this_person in every person
    set first_name to first name of this_person as string
    set last_name to last name of this_person as string
    if first_name = "missing value" then
      set myname to words of last_name
      set newFirstName to 1st word of last_name
      set newLastName to ""
      try
        set newLastName to 2nd word of last_name
      end try
      set first name of this_person to newFirstName
      set last name of this_person to newLastName
    end if
  end repeat
end tell
[robg adds: I haven't tested this one. If you're going to try it, please back up your Address Book database first. You can do this within the program from the File menu, or in the Finder by duplicating your user's Library » Application Support » Address Book folder.]

Comments (4)


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