Script please to de-duplicate imported jpeg files iPhone photos (keep only E enhanced versions)

Good day to all. New member here with no scripting experience, kindly seeking an automation script please.

I have just imported thousands of photos from my 12Pro to my iMac via ImageCapture. All photos have been downloaded as jpegs to a single folder.

In some cases I have 2 files of essentially the same photo: The original plus an auto-Enhanced version. As examples:

IMG_1111.JPG
IMG_E1111.JPG

IMG_2222 2.JPG
IMG_E2222 2.JPG

In short, might someone kindly be able to provide a script that keeps only the E versions of all my photos & deletes the originals, in only the cases where there is in-fact 2 versions of essentially the ‘same’ photo?

Thank you.

Hi again all,

I’ve managed to write this code. But I get this error:
error “Can’t get name of "IMG_3297.JPG".” number -1728 from name of “IMG_3297.JPG”

Any ideas please? Thank you.

My code is as follows:

-- Specify the folder path where your photos are located
set photoFolderPath to "/Users/MyName/Desktop/Test/"

-- Get a list of all files in the folder
set allFiles to list folder photoFolderPath

-- Iterate through the files
repeat with aFile in allFiles
	set fileName to name of aFile
	
	-- Check if the file name has a corresponding enhanced version
	if fileName starts with "IMG_" then
		
		set enhancedFileName to "IMG_E" & text 5 thru -1 of fileName
		
		-- Check if the enhanced version exists
		if enhancedFileName is in (get names of files of (photoFolderPath as alias)) then
			--Delete the original file
			set originalFilePath to photoFolderPath & fileName
			do shell script "rm " & quoted form of POSIX path of originalFilePath
		end if
	end if
end repeat

(Backticks added by NG.)

My [Test] folder contains these JPG files:
IMG_3297.JPG
IMG_E3297.JPG
IMG_3917.JPG
IMG_E3917.JPG
IMG_4073 1.JPG
IMG_E4073 1.JPG

This script begins by getting a list of all files whose name begins with IMG_E. It then tries to move a file with the same name minus the ‘E’ to the trash. If such a file exists it adds that trashed item’s filename to a list; if not, it ignores it and continues through the e-list.

tell application "System Events"
	set phPath to (path to desktop as text) & "Test:" as text
	set eFileList to files of folder phPath whose name begins with "IMG_E"
	set trashList to {} -- list of files moved to trash
	set neverList to {} -- optional, list of E-files without matching no-E
	repeat with eachFile in eFileList
		set AppleScript's text item delimiters to "E"
		set imgName to name of eachFile
		set noe to text items of imgName
		set AppleScript's text item delimiters to ""
		set noe to noe as text -- name without the 'E'
		try -- if the same name without the 'e' exists…
			move (file (phPath & noe as text)) to trash
			set end of trashList to noe
		on error
			set end of neverList to name of eachFile
		end try
	end repeat
	trashList -- optional, to return list of trashed files
	-- neverList -- optional, to return list of E-file without matching no-E
end tell

It is not case-sensitive.

Update: I added a couple of lines that will collect a list of filenames for which only the enhanced variant exists.

ps Thank you, masked man

1 Like

Thank you for the solution (and the edits needed for code format)
Much appreciated on both counts!

1 Like

My pleasure but 'twas another that fixed the code formatting.