Import vCard into Address Book + Groups

I am looking for a way to import a .vcf file (with one contact) into Address Book and assign it to two groups simultaneously. I have searched and don’t seem to be able to find any examples. Any help is greatly appreciated.

One idea might be to import vcard and then use the “last import” group to assign the new groups to the contact, but I am not sure what syntax to use.

Hi,
Hopefully the script below will do what you want. The only way I can see to import a vcard is to use the open command but this presents a dialog so I’m having to use System events to OK the dialog and also the script assumes that the vcard is the same name as the entry name in address book. Somebody else on this site maybe able to offer better code but as I said this should do what you want!!
Thanks,
Nik

set thefile to choose file
tell application "Finder" to set file_Name to name of thefile
set Entry_Name to text 1 thru -5 of file_Name --> this line strips off the ".vcf" extension

tell application "Address Book"
	activate
	open thefile
end tell

delay 1

tell application "System Events" to keystroke return

delay 1

tell application "Address Book"
	set theid to id of person Entry_Name
	add person id theid to group "nameofyourgroup"
end tell

Thanks Nik.

With some modifications your script worked. I am saving a vCard from FileMaker to my desktop and I modified my FileMaker script to add the name of the contact to the file name so it would work with your script. I am wondering if there is a way to dispose of the “choose” dialog so this would happen automatically and I could run it from within FileMaker without intervention. One possibility might be to use a static file name for the .vcf file, but this could cause complications for assigning the groups. Maybe there is a way to send the file name to applescript from filemaker?

Here is my modified script:


set thefile to choose file
tell application "Finder" to set file_Name to name of thefile
set Entry_Name to text 1 thru -5 of file_Name --> this line strips off the ".vcf" extension

tell application "Address Book"
	activate
	open thefile
end tell

delay 1

tell application "System Events" to keystroke return

delay 1

tell application "Address Book"
	set theid to id of person Entry_Name
	add person id theid to group "Client"
	add person id theid to group "Filemaker"
	save addressbook
end tell

delay 1

tell application "Finder"
	delete thefile
end tell

Chris

Okay - the above script only works if the contact in my filemaker database does not have a middle name. If I change the output of the file from filemaker to include the middle name in the file name, then those without a middle name end up with an extra space, and the script does not work.

If I output the vCard from filemaker to a file with a static name like “vcfoutput.vcf” is there a way to use the “last import” group to get the contact’s id after it is imported, and then use the id to add the contact to the new groups? Does anyone know of a way to access the contact listed in the “last import” group and get its id? I have tried but have not had success.

I am able to get the id of the 1st person in a group list using this script:

tell application "Address Book"
	set theid to id of item 2 of person of group "Personal"
	return theid
end tell

but if I try substituting “Last Import” for the name of the group, I get this error:

Address Book got an error: Can’t get group “Last Import”

so the Last Import group must not be a real group, but there has got to be a way to access the contacts in the Last Import list!?!?

Hi,

group “Last Import” isn’t accessible at all with AppleScript.
but you can select the contacts manually and use selection

Thanks Stefan:

I did go about looking into using the select command to choose the file and it works, although I had to do it using repeat (thanks to reference in Matt Neuberg’s AppleScript book, 2nd Ed. pg 354).

Here is my script:

tell application "Address Book"
	activate
	open "Disk:Users:Username:Desktop:vcfoutput.vcf"
end tell

delay 1

tell application "System Events" to keystroke return

delay 1

tell application "Address Book"
	repeat with aThing in (get selection)
	end repeat
	set Entry_Name to name of aThing
	set theid to id of person Entry_Name
	add person id theid to group "Client"
	add person id theid to group "Filemaker"
	save addressbook
end tell

delay 1

tell application "Finder"
	delete "Disk:Users:Username:Desktop:vcfoutput.vcf"
end tell

Maybe there is a more efficient way to do it?

Chris

are you sure, that the end repeat line is at the right place? :wink:

Not sure at all. Maybe repeat is not necessary? I tried removing it, but then the script doesn’t work.

it should be


.
tell application "Address Book"
   repeat with aThing in (get selection)
       set Entry_Name to name of aThing
       set theid to id of person Entry_Name
       add person id theid to group "Client"
       add person id theid to group "Filemaker"
     end repeat
   save addressbook
end tell
.

Got it.

Thanks.

How should this script look like if the groups are on a Address Book Server?