Its goal is to backup individual vCards from contacts to Dropbox.
#!/bin/zsh -f
DIR="$HOME/Dropbox/Backups/vCards"
[[ ! -d "$DIR" ]] && mkdir -p "$DIR"
# Don't Indent - BEGIN
/usr/bin/osascript <<EOT
set vPath to (path to the home folder as string) & "Dropbox:Backups:vCards"
alias vPath
tell application "Contacts"
repeat with cardPerson in people
set nameOfvCard to name of cardPerson & ".vcf"
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
EOT
# Don't Indent - END
First, why use a Bash script that simply contains an AppleScript?
I would just use an AppleScript in an Execute an AppleScript action.
IAC, we can't help unless you describe the problem in detail.
Exactly what is not working? Where is it failing?
It is best to upload your macro with the script in it.
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.
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
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
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)
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
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!
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.