How Do I Export vCards from Apple Contacts using AppleScript?

Will upload it once I try one more thing.

VCF to Dropbox Macro

VCF to Dropbox.kmmacros (2.0 KB)

But it only created 55 vCard files, not the 682 files that I was expecting. I think something is still not right. I don't know AppleScript enough to know what is going on or where the problem in the script is.

Sounds like a timing issue.

With this large number of transactions, the only way to debug is to run the AppleScript in a script editor, preferably Script Debugger 7.

I would add a log statement just before each card to identify which card it is failing on.

tell application "Contacts"
  repeat with cardPerson in people
    set nameOfvCard to name of cardPerson & ".vcf"

    log "Card: " & nameOfvCard   ### ADD THIS LINE

    set outFile to (open for access file (vPath & ":" & nameOfvCard) with write permission)
    write (vcard of cardPerson as text) to outFile
    close access outFile
  end repeat
  quit
end tell

set vPath to (path to home folder as text) & "Dropbox:Backups:vCards:"
do shell script "mkdir -p " & quoted form of POSIX path of vPath

tell application "Contacts"
	set {contactNames, vCards} to {name, vcard} of people
	quit
end tell
set treatedNames to {}
repeat with i from 1 to (count contactNames)
	set bareName to (item i of contactNames)
	if bareName is in treatedNames then
		set nameOfvCard to bareName & "_" & i & ".vcf"
	else
		set end of treatedNames to bareName
		set nameOfvCard to bareName & ".vcf"
	end if
	set outFile to (open for access file (vPath & nameOfvCard) with write permission)
	try
		set eof outFile to 0
		write (item i of vCards) to outFile as «class utf8»
	end try
	close access outFile
end repeat
1 Like

Also, I removed 2 contacts that the script didn't like I think it was because the names contained an ampersand.

I share the macro in the Macro Library section. Individual VCF Cards to Dropbox Macro (v9.0.3)

How can I export a backup of my contacts with AppleScript.

I have the following but it errors out.

tell application "Contacts"
	set the clipboard to (vcard of people) as text
	do shell script "pbpaste > /Users/troyxx/Dropbox/xx/Contactsxx/TroyContactsxx.vcf"
end tell

Are you sure you want pbpaste in there ?

You are pasting the clipboard contents to overwrite/create a .vcf file ?

Hey @ComplexPoint thanx.... not sure what I want in there.... above my pay grade...
The goal to accomplish would be a complete backup of my contacts. whether a found set is displayed or not... would want a complete backup.
It used to work, just as is... but I haven't visited it in a year or so.... something has changed... which is ok... I just need to update the script in some fashion... (My guess)

Is

Contacts > File > Export > Contacts Archive...

any good to you ?

Yes, but I was looking to automate it 'under the hood' with AppleScript. I could do it with KM of course with select menu etc etc, just feel AppleScript would be a bit more reliable/stable... NOT that KM is not..... just my thought. -
Again, thank you

Hi @ComplexPoint do you know of an AppleScript that can back a 'contacts archive' of the full list of contacts?
I appreciate your help.

Do you mean create an archive ?
Or restore from one ?

(do you have a preference for the archive format ?)

thanx man, yeah, only the archive, as a backup if anything goes wrong with the upcoming import! No pref for format as long as it backs up all card in the app even if there is a found set displayed, and the said export can be imported in case of a problem with my next import....
Really appreciate your time and expertise!

There doesn't seem to be anything the API, so I think the most solid approach may be to use:

1 Like

Hey @troy,

That was actually a very good idea. You just didn't have quite enough expertise to put all the pieces to together.

As far as I can see the resultant .vcf file will import into Contacts.

I've looked at it pretty carefully, but I'm not going the whole 9 yards to backup my contacts, import the new file, and then make sure it's all copacetic. You can do that.

The great thing about this method is that it can run in the background in about 2 seconds (or perhaps less on a newer, faster machine).

It took me a while to overcome a couple of sticky wickets, but I'm quite pleased with the result.

-Chris


Backup Apple Contacts to a Date-Stamped VCF File v1.00.kmmacros (8.2 KB)

Macro-Image

Keyboard Maestro Export

1 Like

Morning,

I ran the script and it took a little while to run (which is fine), but so you know it was like a minute plus.

It made a .vcf file but when I opened that file to import, the following dialog came up. Also, the file was 1 byte in size.

Screen Shot 2021-11-11 at 8.56.41 AM

Hey Troy,

Something's wrong.

How many contacts do you have?

It shouldn't have taken a whole minute to run, unless you have 5000 or more contacts.

And it certainly shouldn't have created a 1 byte file.

My first guess is that you haven't given Keyboard Maestro full disk access, but based on what you do I'd think it's not a good guess.

Run this from the Script Editor or Script Debugger, and let me know what you get.

tell application "Contacts"
   if not running then run
   return vcard of person 1
end tell

You can definitely write to a file with Keyboard Maestro?

-Chris

Hi Chris,
KM did not have full disk access, but it does now, and I restarted.
8,500 cards
Ran the script you provided, still 1 byte,
Text file attached.
thank you!!
Contact test.txt.zip (119.6 KB)

Hmm... I have no clue of why.

Try this more Keyboard Maestro native macro.

Backup Apple Contacts to a Date-Stamped VCF File v2.00.kmmacros (11 KB)

Macro-Image

Keyboard Maestro Export

That's what I would expect.

-Chris