Problem: You want to export your Address Book database as a Comma Separated Values (CSV) file for import into Gmail Contacts. Address Book can export a vCard, but has no CSV export feature. CategoryApplications CategoryApplescript CategoryInternet

In addition to my Applescript, supposedly these applications do the job:

Download: Click on the "Attachments" link on this page.

Solution: Here is an Applescript I wrote for just that purpose. It's a little rough but all your main info is translated to Gmail fields. Do not use this script for a "backup", as it drops a lot of fields, especially if your address book was imported from Entourage.
I have been the only one to use this, so reports/complaints are welcome. Script beeps when finished, and the file appears on your desktop.

#!Applescript (-)
-- a little rough but all your main info is translated to Gmail fields. Do not use this script for a "backup", as it drops a lot of fields.
-- I have been the only one to use this, so reports/complaints are welcome. Script beeps when finished, and the file appears on your desktop. 

-- Export to Gmail.scpt
-- version 23 May 2006
-- http://Gnarlodious.com/
-- version 23 May 2006

set separator to ","

set gmailElements to {"Name", "E-mail", "Notes", "Section 1 - Description", "Section 1 - Email", "Section 1 - IM", "Section 1 - Phone", "Section 1 - Mobile", "Section 1 - Pager", "Section 1 - Fax", "Section 1 - Company", "Section 1 - Title", "Section 1 - Other", "Section 1 - Address", "Section 2 - Description", "Section 2 - Email", "Section 2 - IM", "Section 2 - Phone", "Section 2 - Mobile", "Section 2 - Pager", "Section 2 - Fax", "Section 2 - Company", "Section 2 - Title", "Section 2 - Other", "Section 2 - Address"}

set exportedFile to (path to desktop as string) & "Gmail Upload.csv"

set indexLine to ""
set elementsCount to count of gmailElements
repeat with loopVar from 1 to elementsCount
        set indexLine to indexLine & item loopVar of gmailElements & separator
end repeat
set indexLine to text 1 thru -2 of indexLine

tell application "Address Book"
        
        --try
        open for access file exportedFile with write permission
        write indexLine & return to alias exportedFile
        
        repeat with loopVar from 1 to (get count of every person)
                set thisRecord to person loopVar
                
                try -- name
                        -- this method does not fail if text is already plain
                        set {text:result} to (name of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to the result
                
                try --  primary email
                        set {text:result} to (value of email 1 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                -- the "notes" field cannot be deleted, so lets use it for something informative like nickname
                try
                        set {text:result} to (nickname of thisRecord)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & result
                
                (*      set {text:notes} to (note of thisRecord as string) -- note
                        if notes is "missing value" then set notes to ""                 *)
                
                try -- description
                        set {text:result} to (value of related name 1 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                try -- alt email
                        set {text:result} to (value of email 2 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                try -- AIM
                        set {text:result} to (value of AIM Handle 1 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                try -- phone 1 (same as phone)
                        set {text:result} to (value of phone 1 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                
                try -- phone mobile
                        set {text:result} to (value of phone 2 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                try -- pager
                        set {text:result} to (value of phone 3 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                try -- FAX
                        set {text:result} to (value of phone 4 of thisRecord as string)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                if company of thisRecord is true then -- company
                        --      set {text:result} to (name of thisRecord)
                        "company"
                else
                        ""
                end if
                set csvLine to csvLine & separator & the result
                
                try -- city
                        set {text:result} to (city of address 1 of thisRecord)
                on error
                        ""
                end try
                set csvLine to csvLine & separator & the result
                
                --              -- skip title as I do not use them
                --              set csvLine to csvLine & separator
                --              
                --              try -- Other field will be URL
                --                      set {text:result} to (value of url of thisRecord)
                --              on error
                --                      ""
                --              end try
                --              set csvLine to csvLine & separator --& the result (error, commas in Google Maps URLs need to be escaped)
                --              
                
                write («class ktxt» of ((csvLine as string) as record)) & return to alias exportedFile
                --              write csvLine & return to alias exportedFile
                
        end repeat
        
        
        --      on error
        --              close access file exportedFile
        --      end try
        
        close access file exportedFile
        
end tell

beep

Smiley:

Computer/AddressBook/ContactsToGmail (last edited 2007-08-03 11:09:00 by Gnarlodious)