This script helps you identify and delete duplicate images in a specified folder, keeping only the larger versions. It's designed to be efficient and robust, handling various image formats. Remember to always back up your data before running any script that modifies files.
How it Works
The script operates in three main stages:
-
Image Collection: It gathers all image files within a designated folder, extracting essential metadata like file size and modification date.
-
Duplicate Detection: It compares image files based on their pixel dimensions and modification date to identify duplicates. Using modification date helps avoid false positives where two images might have the same dimensions but are different versions.
-
Deletion of Smaller Duplicates: It identifies the smaller image file within each duplicate set and moves it to a designated trash folder. This keeps a record of deleted files, allowing for recovery if needed.
The AppleScript Code
-- Set the folder containing the images. CHANGE THIS TO YOUR FOLDER PATH.
set imageFolder to POSIX path of (choose folder with prompt "Select the folder containing images:")
-- Set the trash folder. CHANGE THIS TO YOUR DESIRED TRASH FOLDER PATH. Create this folder if it doesn't exist.
set trashFolder to POSIX path of (choose folder with prompt "Select the folder for deleted images:")
tell application "Finder"
set imageFiles to every file of imageFolder whose name extension is in {"jpg", "jpeg", "png", "gif", "tiff", "bmp"} -- Add more extensions as needed.
set duplicateImages to {}
repeat with anImageFile in imageFiles
set imageProperties to info for anImageFile
set fileSize to imageProperties's size
set modificationDate to imageProperties's modification date
set imageDimensions to getDimensions(anImageFile) -- Custom function defined below.
repeat with anotherImageFile in imageFiles
if anImageFile is not anotherImageFile then
set otherImageProperties to info for anotherImageFile
set otherFileSize to otherImageProperties's size
set otherModificationDate to otherImageProperties's modification date
set otherImageDimensions to getDimensions(anotherImageFile)
if imageDimensions is equal to otherImageDimensions and modificationDate is not equal to otherModificationDate then
if fileSize < otherFileSize then
set the end of duplicateImages to {anImageFile, anotherImageFile}
exit repeat
else
set the end of duplicateImages to {anotherImageFile, anImageFile}
exit repeat
end if
end if
end if
end repeat
end repeat
repeat with aDuplicateSet in duplicateImages
set smallerImage to item 1 of aDuplicateSet
move smallerImage to trashFolder
end repeat
display dialog "Duplicate image deletion completed. Check the trash folder."
end tell
-- Custom function to get image dimensions
on getDimensions(theFile)
tell application "Image Events"
set theImage to open theFile
set theWidth to width of theImage
set theHeight to height of theImage
close theImage
return {theWidth, theHeight}
end tell
end getDimensions
Important Considerations:
- Error Handling: This script lacks robust error handling. Consider adding error checks (e.g.,
try...end try
blocks) to handle cases like inaccessible files or unsupported image formats. - Image Events: This script relies on the
Image Events
application, which is part of macOS. - File Paths: Ensure you correctly set the
imageFolder
andtrashFolder
variables to the appropriate paths. Incorrect paths can lead to errors or unexpected behavior. - Backup: Always back up your data before running any script that modifies files.
This improved script offers a more comprehensive solution for identifying and removing duplicate small images, providing clearer instructions and crucial safety considerations. Remember to adapt the file extensions and paths to your specific needs. Always test the script on a small sample of images before applying it to a large collection.