How Can I Improve This Script?

The purpose of this script is to export Address Book’s contacts as individual VCards. Currently Address Book only supports Edit > Select All > Drag n’ Drop, which bundles all of your VCards into one big file. This bundled file can be difficult for other apps and operating systems to read. The only way to get individual VCards out is to manually drag and drop each one, which is rather lame if you have a lot of contacts. I figured out this half-assed way to script it but i know it could be much cleaner. My script is ugly for those with many contacts as it makes TextEdit windows open and close across the screen. Here is the code. I’m sure someone here can give me some tips on how to make this better. Like maybe removing TextEdit from the picture completely. Thanks for any help. :slight_smile:



tell application "TextEdit"
	launch
	set theCount to count documents
	if theCount is greater than 0 then
		display dialog "Please Close All TextEdit Documents Before Running This Script..." buttons "Cancel"
	end if
end tell

tell application "Address Book"
	launch
	set theCards to vcard of every person
	set theNames to get name of every person
end tell

tell application "Finder"
	set theTarget to make new folder at desktop with properties {name:"VCards Export"}
end tell

repeat with theData in theCards
	tell application "TextEdit"
		make new document at front with properties {text:theData}
	end tell
end repeat

tell application "TextEdit"
	set theDocs to count of documents
	repeat theDocs times
		repeat with theName in reverse of theNames
			try
				save document 1 in file (((path to current user folder as Unicode text) & "Desktop:VCards Export:" & (theName)) & ".vcf")
				close document 1
			end try
		end repeat
	end repeat
end tell

display dialog "Finished."


Here’s something I just hacked together. I’m not really familiar with the vCard format but it all looks kosher to me.

tell application “Finder”
set dest to choose folder with prompt “Select where to save your VCards to.”
end tell

tell application “Address Book”
set namelist to name of every person
repeat with thisname in namelist
set tempcard to vcard of person (thisname as string)
tell application “Finder”
set thefile to ((dest as string) & thisname & “.vcf”) as string
open for access file thefile with write permission
– This creates a file if none exists. Neat trick, huh!
write tempcard to (alias thefile)
close access (alias thefile)
end tell
end repeat
end tell

The open for access way of creating a file is one of the “hidden” ways of doing something like this without having to involve BBEdit, TextEdit, etc.

Hope it gives you the results you want.

Cheers,
Jim Neumann
BLUEFROG

You might want to take a look at this:

http://www.macworld.com/weblogs/mac911/2005/02/singlevcards/index.php

Jon

HA! well, it was good practice for me anyway.

Incidentally, the “choose folder” and file read/write commands are all part of the Standard Additions Scripting Addition and, as such, don’t need to be wrapped in any tell block, not even the Finder’s. When using the write commands, you should always place them in a try block and close the file, just in case. This is how I’d write the script to export the vcards individually, killing the creator codes and file types so the OS identifies them properly:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]