I am doing some spring cleaning with my Address book. I exported all the addresses to Excel, cleaned, reformatted the phone numbers etc… then reimported everything as a new address book. All that with Applescript.
Now I want to export the photo of each person in a tiff file in a specific folder with the name and surname as filename and after that reimport this photos into the new address book.
I know how to handle the filename with name and surname of each person but I don’t know how to save the actual image (TIFF picture) and retrieve it.
try this, the script asks for the image folder and the action (Save or Restore)
set imageFolder to (choose folder) as text
set {button returned:buttonReturned} to display dialog "Restore or Save AddressBook Images" buttons {"Cancel", "Restore", "Save"}
if buttonReturned is "Save" then
tell application "Address Book"
repeat with aPerson in (get people)
if image of aPerson is not missing value then
set {name:personName, image:personImage} to aPerson
if (my writeToDisk(personImage, (imageFolder & personName & ".tif"))) is false then
display dialog ("Could not save image for name \"" & personName & quote) buttons {"Cancel", "Continue"} default button 2
end if
end if
end repeat
end tell
else
tell application "Finder" to set fileList to files of folder imageFolder
repeat with aFile in fileList
set fullName to text 1 thru -5 of (get name of aFile)
tell application "Address Book"
try
set thePerson to (first person whose name is fullName)
set imageData to read (aFile as alias) as TIFF picture
set image of thePerson to imageData
on error
display dialog ("No matching person found for name \"" & fullName & quote) buttons {"Cancel", "Continue"} default button 2
end try
end tell
end repeat
end if
on writeToDisk(rawData, filePath)
try
set fRef to open for access file filePath with write permission
write rawData to fRef as TIFF picture
close access fRef
return true
on error
try
close access file filePath
end try
return false
end try
end writeToDisk
Thank you very much Stefan. It works perfectly, too perfectly as I have no more programming to do, you did it all!
I am sure a lot of people who have been with Mac and MacOS X for a long long time have a very messy address book. With a bit of Applescript and a bit of VBA in Excel 2011 plus your script, you can now have a very tidy address book.
Do you think people would be interested? :rolleyes:
I am willing to give back to the community and write all what I did to spring clean my address book. I don’t know where though!
Model: iMac 21.5-inch, Mid 2011
AppleScript: 2.2.1
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)