Apple Address Book Cleaner – AppleScript
Had a small problem with some contacts’ name details in my Apple Address Book recently. For some reason there were a few non-printing characters that had crept in to some of the names (probably during an import from my old Samsung D600 that I did a while ago).
iPhone woes…
This hadn’t really caused a problem until I tried to sync with my iPhone. All the contacts with funny characters in there ended up under the ‘123…’ section in the iPhone’s address book listing, which was a nuisance.
So I wrote this AppleScript to clean up the non-printing characters from the name fields in my Address Book. It has a little splash of ruby code to do the actual character replacement. Please forgive the repetition in this script – it’s my first AppleScript and I just wanted it to work… which it did!
Hope this helps somebody out there. I’m sure I can’t be the only one with this problem.
tell application "Address Book" repeat with this_person in every person if exists (first name of this_person) then set first_name to first name of this_person as string set first_name_fixed to do shell script "/usr/local/bin/ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9 ]/,nil.to_s).strip' \"" & first_name & "\"" set first name of this_person to first_name_fixed end if if exists (last name of this_person) then set last_name to last name of this_person as string set last_name_fixed to do shell script "/usr/local/bin/ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9 ]/,nil.to_s).strip' \"" & last_name & "\"" set last name of this_person to last_name_fixed end if if exists (middle name of this_person) then set middle_name to middle name of this_person as string set middle_name_fixed to do shell script "/usr/local/bin/ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9 ]/,nil.to_s).strip' \"" & middle_name & "\"" set middle name of this_person to middle_name_fixed end if if exists (nickname of this_person) then set nick_name to nickname of this_person as string set nick_name_fixed to do shell script "/usr/local/bin/ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9 ]/,nil.to_s).strip' \"" & nick_name & "\"" set nickname of this_person to nick_name_fixed end if end repeat end tell
P.S. If any AppleScript expert would like to condense and re-factor this to make it more beautiful and DRY please do.