Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple script in Automator - to remove same name files with different extensions

Hi there,


I do have a folder with more than 6000 files which are somehow listed like this:


file1.jpg

file1.pdf

file2.jpg

file3.jpg

file4.jpg

file4.pdf

...


I would like to run a script to identify and delete JPG files that DO HAVE an associated PDF file. In the exemple above, it would only delete file1.jpg and file4.jpg because file1.pdf and file4.pdf exist.


I have tried to accommodate scripts I found out there to make this work as I am no expert. At this point, I am trying to run a Apple script in Automator that looks like that:


set ff to choose folder

tell application "System Events"

set everyJpg to files of ff whose name extension = "jpg"

repeat with thisJpg in my everyJpg

set thisPdf to my getBaseName(thisJpg) & ".pdf"

if (exists file thisPdf of ff) then

delete thisJpg

end if

end repeat

end tell


However, I get an error message like :


"The action "Run AppleScript" encountered an error: Can't make file "Mackintosh HD: Users: Zalmati:MyFolder:file2.jpg" of application "System Events" into the expected type"


From what I can tell, the script looks plain and simple but I don't know how to troubleshoot this.


Is there something easy I am missing?


Thanks,

Posted on Apr 17, 2021 3:11 AM

Reply
Question marked as Best reply

Posted on Apr 19, 2021 4:13 AM

The following AppleScript will remove only those JPG with a name matching PDF counterpart. I have tested this with your simple filenames, and examples with your complicated naming examples, and it works identically in Script Editor and as an Automator application using a Run AppleScript action.


Ideally, I would have liked to write this entirely in AppleScript/Objective-C, as the Finder gets lethargic in large file repository processing. The ASOC code would have done the job without Finder to slow it down.


Tested: macOS 11.2.3 on M1 mini.


Code:


use framework "Foundation"
use AppleScript version "2.4" # Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property PDF : ".pdf"
# allow for extension case, spelling, and any JPEG2000 files
property JPG_EXT : {"jpg", "JPG", "jpeg", "JPEG", "jp2", "JP2"}


set ff to (choose folder default location (path to desktop)) as text

tell application "Finder"
	set everyJpg to (every item in folder ff whose name extension is in JPG_EXT) as alias list
	
	if everyJpg = {} then return
	
	repeat with thisJpg in everyJpg
		set x to POSIX path of thisJpg
		set thisPdf to (my getBasename(x) & PDF) as POSIX file
		if exists thisPdf then delete thisJpg
	end repeat
end tell
return

on getBasename(afile)
	# return entire POSIX path except extension
	return ((NSString's stringWithString:(POSIX path of afile))'s stringByDeletingPathExtension()) as text
end getBasename


Similar questions

8 replies
Question marked as Best reply

Apr 19, 2021 4:13 AM in response to lanceloz

The following AppleScript will remove only those JPG with a name matching PDF counterpart. I have tested this with your simple filenames, and examples with your complicated naming examples, and it works identically in Script Editor and as an Automator application using a Run AppleScript action.


Ideally, I would have liked to write this entirely in AppleScript/Objective-C, as the Finder gets lethargic in large file repository processing. The ASOC code would have done the job without Finder to slow it down.


Tested: macOS 11.2.3 on M1 mini.


Code:


use framework "Foundation"
use AppleScript version "2.4" # Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property PDF : ".pdf"
# allow for extension case, spelling, and any JPEG2000 files
property JPG_EXT : {"jpg", "JPG", "jpeg", "JPEG", "jp2", "JP2"}


set ff to (choose folder default location (path to desktop)) as text

tell application "Finder"
	set everyJpg to (every item in folder ff whose name extension is in JPG_EXT) as alias list
	
	if everyJpg = {} then return
	
	repeat with thisJpg in everyJpg
		set x to POSIX path of thisJpg
		set thisPdf to (my getBasename(x) & PDF) as POSIX file
		if exists thisPdf then delete thisJpg
	end repeat
end tell
return

on getBasename(afile)
	# return entire POSIX path except extension
	return ((NSString's stringWithString:(POSIX path of afile))'s stringByDeletingPathExtension()) as text
end getBasename


Apr 18, 2021 1:31 PM in response to lanceloz

There are two issues:

  1. Coding mistake that I have now corrected that stops removing unpaired JPG
  2. You are using multiple periods in your filenames that break the code that was meant to only encounter a single (normal) period before the name extension. I will need some time to see if I can work around that issue, as I will have to toss the current handler that gets the basename.
    1. One possibility is to compare JPG and PDF file paths without the extension and if the path strings match and the JPG has a red tag, then delete it.

Apr 18, 2021 12:13 PM in response to VikingOSX

Thank you VikingOSX.


I tried your script from within Automator on a test folder (test4) with the following dummy files:

So, to make it easier visually, only the jpg files with the red tag are supposed to be deleted because their pdf equivalent exist.


When I run your script I get the following result:

It has deleted all jpg files in the test folder, that is the three red tag ones that were expected to be deleted but also another green tag file file3.jpg which did not have a pdf equivalent.


Here is the script I run from within Automator:


by the way, not sure if it does matter, but real files are named more like "2019.05.22 home spirit 1105,22 19004574.jpg" and "2019.05.22 home spirit 1105,22 19004574.pdf" for instance.


Apr 17, 2021 7:16 AM in response to lanceloz

Try this AppleScript on a test folder containing matching .jpg and .pdf files, before unleashing it on those 6000 files. Note that AppleScript is not the fastest solution for folders with large file quantities to process. The following was tested on macOS 11.2.3.


use scripting additions

property DELIM : {":", "/", "."}

set ff to (choose folder default location (path to desktop)) as text

tell application "Finder"
	set everyJpg to sort ((every item in folder ff whose name extension is "jpg") as alias list) by name
end tell

if everyJpg = {} then return

repeat with thisJpg in everyJpg
	set thisPdf to my getBaseName(thisJpg) & ".pdf"
	if exists (ff & thisPdf) then delete thisJpg
end repeat
return

on getBaseName(afile)
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
	set basename to text item -2 of (afile as text)
	set AppleScript's text item delimiters to TID
	return basename as text
end getBaseName


Apr 18, 2021 2:55 PM in response to VikingOSX

Couple of things:

The red tag vs Green tag have been added by me for the screen capture of the dummy files just to make it more visual. But on the 6000+ files, there are no tags.


Regarding the numerous dots in the filename, I could do a global replacement of the dots with something temporary like+ signs or equivalent, prior to run your script. I could then restore the dots by reversing the remanning process after the script is run.

Apr 18, 2021 3:35 PM in response to lanceloz

I have something working right now that only removes the extension from the entire jpg file path, which eliminates your having to change filenames. It's working with file1 ... filenn name schemes in Script Editor. Need to create a mixture of PDF and JPG using your longer file naming convention to see how it behaves. Will post when tested in both Script Editor and Automator.

Apple script in Automator - to remove same name files with different extensions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.