"with replacing" question

I found a cool script online to export Address Book entries as a batch of individual vcards. (The link to it is here: http://scriptbuilders.net/files/exportaddressbookasvcards1.0.html)

I want to automate this, so I'd like to have it replace outdated vcards in the destination directory with their changed versions. My understanding is that the "with replacing" command will do this. But I added it, and when I changed someone's phone number in a vcard, the updated vcard didn't have the new phone number.

Below are the original script and my tweaked version with "with replacing" added in what I thought was the right location. Any suggestions on how to make this work would be very appreciated. Thanks.

-------original script below-------- ("xxxxx" hides actual username)

tell application "Address Book"
set PathName to "Users:xxxxx:Current:ABBackup:" -- insert your folder path here
--
set npersons to count of people
repeat with i from 1 to npersons
set vcardinfo to (vcard of person i) as text
set personName to (name of person i) as text
set target_file to PathName & personName & ".vcf" as text
try
set the open targetfile to ¬
open for access file target_file with write permission
set eof of the open targetfile to 0
write vcardinfo to the open targetfile starting at eof
close access the open targetfile
on error theErrMsg number theErrNumber
return theErrNumber
end try
end repeat
end tell

------my adjusted script with "with replacing" added to the line with "write vcard info"----------

tell application "Address Book"
set PathName to "Users:xxxxxx:Current:ABBackup:" -- insert your folder path here
--
set npersons to count of people
repeat with i from 1 to npersons
set vcardinfo to (vcard of person i) as text
set personName to (name of person i) as text
set target_file to PathName & personName & ".vcf" as text
try
set the open targetfile to ¬
open for access file target_file with write permission
set eof of the open targetfile to 0
write vcardinfo to the open targetfile starting at eof with replacing
close access the open targetfile
on error theErrMsg number theErrNumber
return theErrNumber
end try
end repeat
end tell



MacBook Pro 15"/2.16Ghz/2GB RAM Mac OS X (10.4.9)

Posted on Mar 29, 2007 9:49 AM

Reply
1 reply

Mar 29, 2007 1:12 PM in response to RKNYC

The "with replacing" is from the Finder's dictionary - you don't need to use it with the Standard Addition's read/write. Opening a file for access creates a new file if one doesn't exist, and setting the eof to 0 effectively "empties" the file.

The original script works for me (edits to the Address Book get written), although there isn't an error dialog if somethng fails in the try block (you might add a dialog in there to see if something is happening), There is also a tutorial about the file read/write at MacScripter: http://macscripter.net/articles/4370_10_29C/

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

"with replacing" question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.