Finding and deleting duplicate images, especially smaller versions of larger images, can significantly free up disk space on your Mac. This AppleScript automates the process, comparing images based on their pixel dimensions and content, allowing you to easily manage your photo library.
This script focuses on identifying duplicates and deleting smaller versions while preserving the original, higher-resolution image. It's crucial to back up your images before running any script that modifies files. Use this script at your own risk. I am not responsible for any data loss.
How the Script Works
The script utilizes the hash
function to compare images. This method compares the underlying data of the images, rather than just the filenames, providing a more accurate duplicate detection. It then checks the dimensions to determine which image to keep and which to delete.
The script operates on a specified folder. You'll need to adjust the targetFolder
variable to point to the correct directory.
The AppleScript Code
-- Set the target folder here. CHANGE THIS TO YOUR DESIRED FOLDER.
set targetFolder to POSIX path of (choose folder)
-- Get a list of image files in the target folder.
tell application "Finder"
set imageFiles to every file of folder targetFolder whose name extension is in {"jpg", "jpeg", "png", "gif", "tiff"}
end tell
-- Create a dictionary to store image hashes and their paths.
set imageHashDict to {}
-- Iterate through the image files.
repeat with aFile in imageFiles
-- Get the POSIX path of the file.
set filePath to POSIX path of aFile
-- Get the image's hash.
set theHash to hashOfFile(filePath)
-- Check if the hash already exists in the dictionary.
if theHash is not in imageHashDict then
-- Add the hash and path to the dictionary.
set imageHashDict to imageHashDict & {theHash:filePath}
else
-- Hash exists - we have a potential duplicate.
set existingPath to value of imageHashDict at theHash
-- Get image dimensions.
tell application "Image Events"
set theImage to open filePath
set fileWidth to width of theImage
set fileHeight to height of theImage
close theImage
end tell
tell application "Image Events"
set theExistingImage to open existingPath
set existingWidth to width of theExistingImage
set existingHeight to height of theExistingImage
close theExistingImage
end tell
-- Compare dimensions and delete the smaller image.
if fileWidth < existingWidth or fileHeight < existingHeight then
try
-- Delete the smaller image. Use caution!
do shell script "rm -rf " & quoted form of filePath
log "Deleted smaller image: " & filePath
on error errorMessage number errorNumber
log "Error deleting " & filePath & ": " & errorMessage
end try
else
try
-- Delete the duplicate image. Use caution!
do shell script "rm -rf " & quoted form of filePath
log "Deleted duplicate image: " & filePath
on error errorMessage number errorNumber
log "Error deleting " & filePath & ": " & errorMessage
end try
end if
end if
end repeat
-- Function to calculate the hash of a file.
on hashOfFile(filePath)
try
do shell script "shasum -a 256 " & quoted form of filePath
set theHash to text 1 thru -2 of result -- Remove trailing newline
return theHash
on error errorMessage number errorNumber
log "Error hashing " & filePath & ": " & errorMessage
return false
end try
end hashOfFile
Before Running:
- Back up your data: This is critical! Test this script on a small sample of images first.
- Understand
rm -rf
: This command permanently deletes files. There is no undo. - Adjust
targetFolder
: Change this line to point to the folder containing your images. Use thechoose folder
command to select a folder graphically. - Image Events: Ensure that you have the
Image Events
application available. It's typically included with macOS.
This enhanced script provides more robust error handling and detailed logging, making it a safer and more reliable solution for managing your image duplicates. Remember to always exercise caution when deleting files.