Finding and deleting duplicate images, especially smaller versions of larger originals, can significantly reclaim disk space on your Mac. This AppleScript provides a powerful solution, allowing you to identify and remove duplicate images based on size and similarity. It's crucial to back up your data before running any script that deletes files. This script is designed to be robust, but unexpected errors are always possible.
How the Script Works
This AppleScript tackles the problem in several stages:
-
Image Gathering: It first gathers all image files (JPEG, PNG, TIFF, etc.) within a specified folder. You'll need to adjust the
targetFolder
variable below to point to the directory you wish to clean. -
Duplicate Identification: It compares images based on their pixel dimensions and file size. Images with identical dimensions and similar file sizes are flagged as potential duplicates. The similarity check helps account for slight compression variations.
-
Size Comparison: For each potential duplicate pair, it determines which image is smaller and flags that one for deletion.
-
Deletion Confirmation: Before deleting any files, the script presents a list of images slated for removal, allowing you to review and cancel the operation if necessary.
-
File Deletion: If you confirm, the script proceeds to delete the smaller duplicate images.
The AppleScript Code
-- Set the target folder here. Make absolutely sure this is correct!
set targetFolder to POSIX path of (path to desktop as text) & "/Images"
-- Tolerance for file size difference (in bytes). Adjust as needed.
set fileSizeTolerance to 1024 -- Allows for a 1KB difference
tell application "Finder"
set imageFiles to every file of folder targetFolder whose name extension is in {"jpg", "jpeg", "png", "tiff", "gif"}
set imageInfo to {}
repeat with aFile in imageFiles
set {name:name, size:size, posixPath:posixPath} to {name:name of aFile, size:size of aFile, posixPath:POSIX path of aFile}
set theImage to aFile
tell application "System Events" to tell process "Preview"
launch
open theImage
delay 1
tell (window 1)
set dimensions to {width:width, height:height}
end tell
quit
end tell
set end of imageInfo to {name:name, size:size, posixPath:posixPath, width:dimensions's width, height:dimensions's height}
end repeat
set duplicatesToDelete to {}
repeat with i from 1 to count imageInfo
set thisImage to item i of imageInfo
repeat with j from i + 1 to count imageInfo
set thatImage to item j of imageInfo
if thisImage's width is thatImage's width and thisImage's height is thatImage's height then
if abs(thisImage's size - thatImage's size) ≤ fileSizeTolerance then
if thisImage's size < thatImage's size then
set end of duplicatesToDelete to {path:thisImage's posixPath, size:thisImage's size}
else
set end of duplicatesToDelete to {path:thatImage's posixPath, size:thatImage's size}
end if
end if
end if
end repeat
end repeat
if count duplicatesToDelete > 0 then
set deleteList to {}
repeat with aDuplicate in duplicatesToDelete
set end of deleteList to aDuplicate's path & " (" & aDuplicate's size & " bytes)"
end repeat
display dialog "The following images will be deleted:" buttons {"Cancel", "Delete"} default button 2 with title "Confirm Deletion" giving up after 60 with list deleteList
if button returned of result is "Delete" then
repeat with aDuplicate in duplicatesToDelete
try
do shell script "rm -rf " & quoted form of aDuplicate's path
end try
end repeat
display dialog "Deletion complete."
end if
else
display dialog "No duplicate images found."
end if
end tell
Important Notes:
- Error Handling: The
try...end try
block attempts to handle potential errors during file deletion. However, it's strongly recommended to test this script on a small sample of images before running it on a large collection. - Preview App: This script uses the Preview application to get image dimensions. Make sure Preview is installed on your system.
- Permissions: The script requires appropriate file system permissions to delete files.
- Backup: Always back up your data before running this or any similar script.
This enhanced script provides a more robust and user-friendly experience for managing duplicate images on your Mac. Remember to adjust the targetFolder
and fileSizeTolerance
variables according to your needs. Always proceed with caution and review the deletion list carefully before confirming the removal of any files.