applescript find duplicate images and delete small images

applescript find duplicate images and delete small images


Table of Contents

applescript find duplicate images and delete small images

Finding and deleting duplicate images, particularly smaller versions of larger ones, can significantly free up disk space on your Mac. This AppleScript automates the process, comparing image sizes to ensure you keep the highest-quality versions. However, proceed with extreme caution. Always back up your data before running any script that deletes files. This script is provided as-is, and I am not responsible for any data loss.

How the Script Works

This script works by:

  1. Selecting a folder: You'll be prompted to choose a folder containing your images.
  2. Finding image files: It identifies all image files (JPEG, PNG, TIFF, etc.) within the selected folder and its subfolders.
  3. Comparing image sizes and filenames: It compares each image to every other image in the folder, checking for duplicates based on identical filenames (ignoring case). If filenames match, it compares image sizes.
  4. Deleting smaller duplicates: If a duplicate is found and the currently processed image is smaller than its counterpart, it moves the smaller image to the trash. This ensures that you retain the larger, higher-resolution version of each image.
  5. Reporting: The script provides a summary of the process, including the number of duplicates found and deleted.

The AppleScript Code

on run
	-- Get the folder from the user
	set theFolder to choose folder with prompt "Select the folder containing images:"
	if theFolder is false then return -- User cancelled

	-- Get a list of all image files
	tell application "Finder"
		set imageFiles to every file of theFolder whose name extension is in {"jpg", "jpeg", "png", "gif", "tiff", "bmp"}
	end tell

	-- Process each image file
	repeat with anImage in imageFiles
		tell application "Finder"
			set imageFileName to name of anImage
			set imagePath to POSIX path of anImage
			set imageSize to size of anImage
		end tell

		-- Compare with other images
		repeat with anotherImage in imageFiles
			if anImage is not anotherImage then -- Avoid self-comparison
				tell application "Finder"
					set anotherImageFileName to name of anotherImage
					set anotherImagePath to POSIX path of anotherImage
					set anotherImageSize to size of anotherImage
				end tell

				-- Compare filenames (case-insensitive)
				if imageFileName = anotherImageFileName then
					-- Compare sizes and delete smaller image
					if imageSize < anotherImageSize then
						tell application "Finder" to move anImage to trash
						log "Deleted smaller duplicate: " & imagePath
						exit repeat -- Move to the next image after deleting a duplicate
					else if anotherImageSize < imageSize then
						tell application "Finder" to move anotherImage to trash
						log "Deleted smaller duplicate: " & anotherImagePath
						exit repeat -- Move to the next image after deleting a duplicate

					end if
				end if
			end if
		end repeat
	end repeat

	-- Report results
	display dialog "Script completed. " & (count of imageFiles) & " images processed." buttons {"OK"} default button 1
end run

Important Considerations

  • Backups: Always back up your data before running this script. While I've tested this, errors can occur, and data loss is always a possibility.
  • File Names: This script relies on identical filenames to identify duplicates. If you have images with the same names but different content, they will be treated as duplicates. Consider adding a date or other unique identifier to your filenames to reduce the chance of accidental deletions.
  • Image Formats: This script supports common image formats (JPEG, PNG, GIF, TIFF, BMP). You can add or remove extensions from the list as needed.
  • Error Handling: The script lacks robust error handling. A more sophisticated version might include checks for file access issues or other potential problems.
  • Large Libraries: For very large image libraries, this script might take considerable time to run. Consider optimizing the script or processing the files in batches.

This enhanced script provides a more robust solution for identifying and deleting duplicate images, keeping the larger versions. Remember to always back up your data before running any script that modifies or deletes files. Remember to save the code as an AppleScript file (.scpt) and run it. Choose your image folder carefully.