How do I use AppleScript to export Address Book contacts?

Here's what I want to do. I want to execute a series of AppleScript commands which will automatically export every contact I have in my Address Book to a vCards.vcf file (which will be in my Dropbox folder). The reason I want to do this is so I don't have to remember to manually export the contacts every time I change one.
What are the AppleScript commands I need to run?

quick&dirty, but it works 8-)
-- hubionmac.com 06.02.2011
-- quick&dirty script for exporting contacts as single vCards
--exisiting vCards will not be overwritten by the script, instead new files will 
--get an index like "John Smith 1.vcf"
set doThese to getitem_2process(true)
tell application "Address Book"
set destination_path to choose folder with prompt "Destination folder for export:"
repeat with doThis in doThese
if first name of doThis is missing value then
set firstname to ""
else
set firstname to first name of doThis
end if
if last name of doThis is missing value then
set lastname to ""
else
set lastname to last name of doThis
end if
if (lastname & firstname) as text = "" then
set namestring to "nobody.vcf"
else
set namestring to (first name of doThis & " " & last name of doThis & ".vcf") as text
end if
set namestring to my checknamewith_pdfsuffix(namestring, destination_path, false)
my writeToFile((destination_path as text) & namestring as text, (get vcard of doThis), false)
end repeat
my display_message(((count of doThese) & " contact(s) exported") as text, 3)
end tell
on writeToFile(MacFilePathTxt, txt, add2eof)
--lastedit 18.01.2011
if add2eof is false then
try
do shell script "rm " & quoted form of POSIX path of (MacFilePathTxt as alias)
end try
end if
set RefNum to (open for access file MacFilePathTxt with write permission)
try
if add2eof is false then
write txt to RefNum
else
write txt to RefNum starting at ((get eof RefNum) + 1)
end if
close access RefNum
return true
on error
close access RefNum
return false
end try
end writeToFile
to getitem_2process(askwhat)
try
tell application "Address Book"
activate
tell application "System Events"
keystroke "0" using command down --show address-window not matter what...
end tell
if askwhat is true then
set theaction to button returned of (display dialog "Process all or just selection?" buttons {"All", "Selection", "cancel"} default button {"Selection"})
if theaction = "Selection" then
set a to every person whose selected is true
else
set a to people
end if
else
set a to people
end if
end tell
on error msg
error "error on getitem_2process" & return & msg
end try
end getitem_2process
to checknamewith_pdfsuffix(n, D, looped)
--check if filename exists in D
-- so if "A File.pdf" exists it names it "A File 1.pdf","A File 2.pdf",...
tell application "Finder"
set thefiles to name of every item of (D as alias)
end tell
if thefiles contains n then
if looped = false then
set n to ((characters 1 through -5 of n) & " 1" & (characters -4 through -1 of n)) as text
my checknamewith_pdfsuffix(n, D, true)
else
set tmp to (last word of ((characters 1 through -5 of n) as text) as integer)
set tmpcount to (count of characters of (tmp as text)) + 5
set tmp to tmp + 1
set n to ((characters 1 through (-1 * tmpcount) of n) & tmp & (characters -4 through -1 of n)) as text
my checknamewith_pdfsuffix(n, D, true)
end if
else
return n
end if
end checknamewith_pdfsuffix
on display_message(msgTXT, msgTimeout)
tell application "System Events"
set isRunning to ¬
(count of (every process whose name is "GrowlHelperApp")) > 0
end tell
if isRunning = true then
tell application "GrowlHelperApp"
-- Make a list of all the notification types 
-- that this script will ever send:
set the allNotificationsList to ¬
{"Status"}
-- Make a list of the notifications 
-- that will be enabled by default.      
-- Those not enabled by default can be enabled later 
-- in the 'Applications' tab of the growl prefpane.
set the enabledNotificationsList to ¬
{"Status"}
-- Register our script with growl.
-- You can optionally (as here) set a default icon 
-- for this script's notifications.
register as application ¬
"Address Book" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Address Book"
--Send a Notification...
notify with name ¬
"Status" title ¬
"Status" description ¬
msgTXT application name ¬
"Address Book"
return true
end tell
else
activate
display dialog msgTXT giving up after msgTimeout
end if
end display_message

Similar Messages

  • How to Export Address Book contacts as CSV file for Gmail?

    Hi
    I wish to export some of my Address Book contacts towards Gmail (which i use through the web interface for now) but I don't want to export vcards as i don't trust big brother Google with all the other data (snail mail, phone numbers, birthdays, etc).
    I have tried to do the export through Address Book but found no CSV export option.
    I've searched the forum for an answer, but to no avail. Some other (paying) websites claim to offer solutions but the "pay before you see" deters me from using them.
    Any tip on how to do this would be greatly appreciated. Scripts and software also welcome, if shareware/freeware.
    Cheers,
    FMN

    Thanks for your answer, but gmail's help specifically states one can import contacts in CSV format and i was looking for a simple batch solution.
    Here is a link to the relevant gmail page (might be in French - am not a "Froguette" for nothing ;)) about importing:
    <http://mail.google.com/support/bin/topic.py?topic=28647>
    and the one about importing addresses as CSV files:
    <http://mail.google.com/support/bin/answer.py?hl=fr&answer=12119>)
    So my question still stands.
    FMN
    Message was edited by: FroguetteMiNote - wrong link corrected
    Message was edited by: FroguetteMiNote

  • How to export address book contacts to numbers sheet

    I am looking for a way to export my address book contacts from a specific group to a Numbers spreadsheet. Can someone help me with that? I would think it should be a simple task, but I can't seem to find any help for that kind of an operation.
    Thanks in advance for any help.

    select the contacts
    copy,
    switch to Numbers
    paste
    You should also make sure, in System Preferences, that Numbers is allowed to access Contact information in the "Security & Privacy" panel:

  • Exporting Address Book Contacts

    I would like to export my address book contacts into an excel spreadsheet in order to THEN import them into Filemaker. Can anyone give me a simple step by step?
    Thanks!

    Go to http://homepage.mac.com/kenferry/software.html --and scroll down to AddressBookToCSV. I just tested it on 10.5.2, and it will export your address book to CSV format in spreadsheet. Follow the instruction in the dialog box.

  • Deleted Notes in Exported Address Book Contact

    Help, deleted notes when I exported a couple of Vcards. Anyway to get them back? Haven't set up anykind of backup on machine.

    That's been my frustration as well. I wanted to pdf all my contact info to carry with me on a USB key in case I needed something from the Note section. The iPhone truncates the note section to about the first 2,500 characters. So if you have contacts with long Notes you've got a problem. I thought a had a work-around, but pdf'ing the Contacts didn't work because it won't print more than 1 page per contact and prints the partial Note in two narrow columns, truncating the rest of the Note. I can't believe more people haven't complained about this. Before migrating to Address Book (I used Outlook), I made extensive use of the Note section.

  • HT204053 Using I cloud my address book/ contacts are different. When I try to send mail typing in a name does not recognize it

    iCloud help, my contacts- address book are different &amp;when I try to send mail by typing in a name it is not recognized

    Make sure your default contacts account is set to iCloud. To change this go to settings > mail, calendars, contacts > default account and choose iCloud.

  • How can I export Address Book contacts to csv then to xcel to create a mailing list?

    It seems to only export to Vcard. No Vcard to csv...

    Export to vCard,
    Go to this URL
    http://labs.brotherli.ch/vcfconvert/
    And select .csv as the output

  • How do I get my talk talk address book/contacts onto I tunes?

    We have one I tuines account but two I phones and an I pad. How do we associate our own talk talk contacts with our own device - please talk in very simple terms - really not computer literate!
    Thanks

    I can only assume carbonite uses the same structure as your hard disk see http://kb.mozillazine.org/Profile_folder_-_Thunderbird#Mac_OS_X

  • Can I Export Address Book contacts from OSX 10.5.8 to OSX 10.4.11

    Is the information compatible between Systems and which Folders hold the details?
    Any help gratefully accepted.

    Found the Answer in another Posting! Export them as Vcards!! Works perfectly. Thanks you.

  • When forwarding an e-mail I have lost the address book contact list on the left side.

    I used to have the address book contact list on the left side of the page being forwarded to allow easy selection of addressees. It's gone and I need it back.
    Thank you very much

    Open a Write window and press F9 to toggle the Contact Sidebar on and off.
    You can also turn it on from the View menu in the Write window.

  • How to export Address Book content as Unicode with applescript?

    Hello,
    I have a problem for exporting address book content. I'd like to export "My Card" to a vcf file and use another application to read it. So I have the following applescript:
    *+tell application "Finder"+*
    *+set theFolder to folder "TemporaryItems" of folder "Caches" of folder "Library" of home+*
    *+end tell+*
    *+tell application "Address Book"+*
    *+set theVcard to (vcard of my card) as Unicode text+*
    *+set theFile to (theFolder as text) & "myCard.vcf"+*
    *+set theFileHandle to open for access theFile with write permission+*
    *+-- write (ASCII character of 254) to theFileHandle+*
    *+-- write (ASCII character of 255) to theFileHandle+*
    *+write theVcard to theFileHandle+*
    *+close access theFileHandle+*
    *+end tell+*
    OK, now the problem is that if "My Card" has any double-byte characters like Chinese or Arabic, the output file will just hold some question marks ("??"). The code works in Tiger but not Leopard. (Add char 254 and 255 just to make it a UTF16-BE file. So it doesn't really change the result of this script.)
    I started digging and guess here is the reason: in the following statement the "vcard of my card" will always return plain text:
    +*set theVcard to (vcard of my card) as Unicode text*+
    I got this clue from the help of ScriptEditor:
    +*vcard (text, r/o) : Person information in vCard format, this always returns a card in version 3.0 format.*+
    But in Tiger, you can see:
    +*vcard (Unicode text, r/o) : Person information in vCard format, this always returns a card in version 3.0 format.*+
    Any workaround to solve this problem?
    (And if this is not the right place to ask such question, please just let me know.)
    Thanks a lot!
    Calvin

    Hello Calvin,
    Since your code works under OSX10.4 and fails under OSX10.5, the cause would be the change made in AppleScript 2.0.
    Try -
    write theVcard as Unicode text to theFileHandle
    instead of -
    write theVcard to theFileHandle
    The read/write commands in AS 2.0 have been change so that they will always interpret the text in system's primary encoding if 'as Unicode text' is not specified in commands' parameter.
    cf.
    http://developer.apple.com/releasenotes/AppleScript/RN-AppleScript/index.html
    Unicode Support
    http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangG uide/
    pp. 151-153, 168-169 (of pdf version)
    Commands Reference > read, write
    Hope this may help,
    H

  • How was someone using my e-mail address as their Apple ID?

    First time posting, so I apologize in advance if this is the wrong forum.
    1) I have a primary e-mail adress that is not linked to my iTunes.  The only time this e-mail address was used in conjunction with anything Apple was when I purchased Quicktime Pro in 2006 and registered my iPod in 2007.  I do have an iTunes account with a seconday e-mail address, and log in occasionaly to listen to free podcasts (never purchasing anything)
    2) Today on my primary e-mail address, I recieved a warning that an app was downloaded, with this e-mail as the Apple ID.  This was the first e-mail I've recieved from Apple on this address since registering my iPod in 2007.
    I live alone, and don't share my electronics with anyone, so I know it's not that.  so I'm left wondering - how did someone use my e-mail address as an Apple ID?  Wouldn't I have recieved an e-mail during the initial registration this person tried to use? 
    Thankfully I have no credit card associated with Apple, so I wasn't overcharged, but this seems like a big security hole.

    The content was this:
    Your Apple ID, [my primary e-mail address] , was just used to download -KingdomConquest- from the App Store on a computer or device that had not previously been associated with that Apple ID.
    If you initiated this download, you can disregard this email. It was only sent to alert you in case you did not initiate the download yourself.
    If you did not initiate this download, we recommend that you go to iforgot.apple.com to change your password, then see Apple ID: Tips for protecting the security of your account for further assistance.
    Regards,
    Apple
    There is the possibility that I did change my iTunes account to my primary and just forgot, but that's doubtful considering a) today was my first e-mail from Apple since 2007 on this address and b) I'm still recieving iTunes updates on my secondary e-mail that I registered years ago. 
    Also, I don't own any devices capable of running Kingdom Conquest. Thankfully, it's a free app.  It would be interesting to see a list of other apps downloaded with this ID, if any.  Is there a way to do that?
    Maybe someone mistyped their email address when creating their account and it just happened to be yours?
    I thought that, too, but my e-mail address is [email protected]  My first name is somewhat common, my last name is definitely not.  And back to what I said earlier, I never recieved a registration e-mail from Apple, just the one above.

  • How can i use my ipod mac address to find it?

    simply put, i left my ipod at work. they missplaced it, now i need to locate it... p.s. its only a 3rd gen 32 gig so its not ios 7 campatible ,icloud only works if you have the device and download cloud on to it(and icloud is turned on).....not looking for opinions just looking for options such as: how can i use my ipod mac address to find it?       i do have the mac address

    You cannot.
    If you did not set up find my pod on the iPod itself, then it cannot be tracked.
    Sorry

  • How do I use a previous email address with my printer?

    How do I use a previous email address with my printer?  I reset my printer and want to use the same email address as before.Thanks

    At this time the printer's email address will not be available for for re-use for 6 months. This is a security measure once an email has been disabled. After that time, one can go into their account and then change the customized email address for the printer. 
    I am a former employee of HP...
    How do I give Kudos?| How do I mark a post as Solved?

  • How do I Export Address Book data?

    Is there anyway to insert Address Book data into other (non-Apple) applications, e.g. Word?
    I want to send out some letters and I would like each letter to have the recipiants address at the top of the letter. Even if I could export the data to Excel first would be OK but I only seem to be able to export vCards from Address Book and Excel (nor Word) can read them?
    Any help would be appreciated.
    Thanks,

    Yink,
    you can use some third-party applications to export Address Book data as plain text files (e.g., csv) which you should then be able to import without any problems in Excel and the like.
    Some options are:
    • Export Addresses from Mail Scripts
    http://homepage.mac.com/aamann/
    • Address Book Exporter
    http://www.gwenhiver.net/address-book-exporter.html
    • Export Address Book
    http://software.dibomedia.de/ExportAddressBook
    Andreas

Maybe you are looking for

  • MacBook to iMac server connection

    I have been connecting my Macbook to my iMac successfully via server connection for years. After installing SL on my Macbook I cannot connect to my iMac (still has 10.5.8 on it) anymore. I can see my iMac in the sidebar in Finder but, but somehow it

  • Fit Frame to Shortcut not working in Indesign CC

    Have tried deleting my preferences and making sure there were no system keyboard shortcuts that had the same keystrokes. Any other ideas?

  • Jsf page not rendering in Jdeveloper

    Hello. I am using jdeveloper 11.1.2.2. I was working on an application this evening, and added a new jsff page. When I opened the page, I got the "waiting for designer to render" then it stopped, and all I see is white. I am able to add components to

  • Playing a video outside the browser

    Say you are watching a video on flash player on a website somewhere. Is it possible to play this video outside of the browser? Meaning, can I play the same video in a different media player or just by itself on my desktop? Also, is there a way to str

  • Quantity updation of return material from Qty Contracts

    I have a contact, for which i created a release order, delivery, goods issue and invoice. Now if I do a return transaction how the quantity contract will be up dated  and my requirement is again to replace the original quantity in quantity contract.