I wrote small apple script to set the images for the entries in my address book depending on firstname, lastname and middle name.
So, for that I am iterating through all the entries of address book and setting the image.
Now, while iterating I am facing issue if the entry is a vcard. So, can some body tell me how to skip if the entry is a vcard. If I can find whether entry in addressbook is vcard, I want to just skip it.
Here is my script:
tell application "Address Book"
set the_file to (((path to desktop) as string) & "addbook.txt") as file specification
try --TRY1
open for access the_file with write permission --Debug information to write
set eof of the_file to 0 --Clear old data
set the_people to every person -- Get the list of people from address book
set imagePath to POSIX path of (choose folder with prompt "Select an image folder:") --Get the image folder path
repeat with this_person in the_people --Repeat the same operation with every person in addressbook
--Check here for vcard or not
if (not (exists image of this_person)) then --check if image exists or not. if not exist proceed else skip
set fileName to missing value
(*compute the file name from firstname, middlename and lastname + jpg *)
if (first name of this_person exists) and (middle name of this_person exists) and (last name of this_person exists) then
set fileName to imagePath & first name of this_person & " " & middle name of this_person & " " & last name of this_person & ".jpg"
set msg to "no"
tell application "Finder" to if exists fileName as POSIX file then set msg to "yes" --check if image file exists or not
if msg = "yes" then
set idata to missing value
try -- TRY2
(* set the image by reading the file *)
set fileOpen to open for access (fileName as POSIX file)
set idata to read fileOpen as TIFF picture
close access fileOpen
set image of this_person to idata
write (fileName & " Added") to the_file starting at eof
write return to the_file starting at eof
save addressbook
on error errs number errn
set errorTo to fileName & " Failed to set the image"
write (errorTo) to the_file starting at eof
write return to the_file starting at eof
try
close access fileOpen
end try
end try
else
set fileNotFound to fileName & " file not found."
write (fileNotFound) to the_file starting at eof
write return to the_file starting at eof
end if
end if
end if
end repeat
close access the_file
on error
write ("Couldn't put image") to the_file starting at eof
write return to the_file starting at eof
try
close access the_file
end try
end try
display dialog "Please check the log file addbook.txt on your desktop for more details"
end tell