Photog, not a scriptor, could use some help trashing images

I use a great Photoshop script called Dr. Brown’s Image Processor. It lets me select a group of image files which are in the NEF format & converts them to JPGs, in the process creating a subfolder named: JPEG.

What I would like to do is trash the NEF files which I chose NOT to convert. To do that I would need a script which could compare the two folders, identify the files which are named, for example, “Picture001.nef”, “Picture009.nef”, etc, but DO NOT show up in the JPEG folder.

In other words, each file which is converted shows up as "Picture002.jpg, “Picture003.jpg”, etc., in the JPEG folder. For those files I would want to keep “Picture002.nef”, “Picture003.nef”.

I hope I’m explaining this in a way that makes sense. And I hope somebody will take pity on me & offer some advice.

This could work:

set nefDir to "julifos:Users:julifos:Desktop:carpeta sin título:NEF_FILES:"
set jpgDir to "julifos:Users:julifos:Desktop:carpeta sin título:JPG_FILES:"

set nefFiles to list folder nefDir without invisibles
set jpgFiles to list folder jpgDir without invisibles

repeat with i from 1 to count nefFiles
	set thisNefFile to nefFiles's item i
	set baseName to text 1 thru -5 of thisNefFile
	if (baseName & ".jpg") is not in jpgFiles then ¬
		tell application "Finder" to delete alias (nefDir & thisNefFile)
end repeat

Thanks so much, I assume that before I give it a spin that I should replace:

“julifos:Users:julifos:Desktop:carpeta sin título:NEF_FILES:”

with the appropiate filepath for my machine?

Always impatient, I went ahead and tried it out on a pair of test folders, changing the filepaths, and it did just what I wanted.

Mucho gracias.

Now, to be even more demanding, is there a way to have the script ask which folders I want to have it examine? Then I wouldn’t have to rewrite the filepaths each time.

Yes, there are various ways. You can use, eg, the “choose folder” command, from the Standard Additions: set nefDir to (choose folder).

OK, so now I am trying:

set nefDir to (choose folder)
set jpgDir to (choose folder)

set nefFiles to list folder nefDir without invisibles
set jpgFiles to list folder jpgDir without invisibles

repeat with i from 1 to count nefFiles
set thisNefFile to nefFiles’s item i
set baseName to text 1 thru -5 of thisNefFile
if (baseName & “.jpg”) is not in jpgFiles then ¬
tell application “Finder” to delete alias (nefDir & thisNefFile)
end repeat

Which lets me select the two folders, but then I get an error stating that "Can’t get text 1 thru -5 of “JPEG”. (JPEG is the name of the folder which the Dr. Brown script creates, containing the converted .jpg images)

And why is that -5? My guess is that the script wants to look at the filename minus the “.jpg”, but wouldn’t that be -4?

Thanks for your patience, as I said, I’m a script user, but no script writer.

Let’s follow the script, step by step:

set nefDir to (choose folder) 
set jpgDir to (choose folder)

We have now defined the two input folders as aliases. Eg: alias “path:to:NEF:” AND alias “path:to:JPEG:”

set nefFiles to list folder nefDir without invisibles 
set jpgFiles to list folder jpgDir without invisibles

We have now two lists to compare: names of files within such folders. Eg: {“file1.nef”,“file2.nef”,“file3.nef”} AND {“file1.jpg”,“file2. jpg”}
Now, see comments:

repeat with i from 1 to count nefFiles --> iterate through NEF-names-list
	set thisNefFile to nefFiles's item i --> eg, "file1.nef"
	set baseName to text 1 thru -5 of thisNefFile --> eg, "file1"
	--> now, if baseName + ".jpg" (eg, "file1.jpg") is not within jpg-list, tell the Finder to delete this NEF file
	if (baseName & ".jpg") is not in jpgFiles then ¬ 
		tell application "Finder" to delete alias ((nefDir as text) & thisNefFile) 
end repeat

Please, note that we need in the last line “(nefDir as text)”, since now “nefDir” contains an alias instead of a text-based-path, as in the first example.

About your error, seems that it exists a file/or/folder called “JPEG” under your chosen “nefDir”. So, the script errors when trying to extract “text 1 thru -5” of such file/folder name. “-5” means “fifth character from the end”. Eg:

item -5 of "abcde" --> "a"

This is thought to extract the base-name of the file, without the extension. Eg, “file.jpg” → “file”. Of course, you can specify “text 1 thru -4” and you will obtain “file.”. Then, you should specify later (baseName & “jpg”) instead of (baseName & “.jpg”).
There are various debug techniques, so you can now what’s going on in your script. For example, you can use a “display dialog” statement:

set nefDir to (choose folder)

display dialog "nefDir is now:" & return & return & (nefDir as text)

If you need (eg) now what are the contents of a list, you can “log” such var, and see it in the “Event log” tab of the Script Editor window. Eg:

set nefDir to (choose folder)

set nefFiles to list folder nefDir without invisibles

log nefFiles

And, if you use the terrific free script editor “Smile”, there are lots of advanced debugging techniques much more powerful and helpful :wink:
http://www.satimage.fr/software/en/

You are correct that the folder named “JPEG” is a sub-folder of the one containing all the NEF files. That’s the result of Dr. Brown’s script (Tips Page)

If I move that “JPEG” sub-folder, then the script does just what I want.

Is there a way to have the script ignore that folder & just deal with the files?

Yes! You can enclose the offending code in a “try” block:

repeat ...
     try
          set thisNefFile to...
          [...] more things here
     on error --> catch errors
          --> do nothing (or do something, if you wish)
     end try
end repeat

jj,
I appreciate your efforts to enable me to learn a bit more about scripting rather than just providing the code, but so far it’s not taking too well.

I’ve tried inserting the tell block (with “on error”) at various spots within the script, but without joy. Often I get:
“The variable baseName is not defined.”

Since the problem is the prescence of the folder named JPEG within the folder of NEF files, I tried adding to this line:
“set nefFiles to list folder nefDir without invisibles”
some things to try to also say without the folder “JPEG”, but nothing I tried would compile.

So at this point success seems so close, but I’m can’t quite get there.

So here I am again, asking for just a bit more guidance.

Well, I would insert the try-block as follow:

set nefDir to (choose folder)
set jpgDir to (choose folder)

set nefFiles to list folder nefDir without invisibles
set jpgFiles to list folder jpgDir without invisibles

repeat with i from 1 to count nefFiles
	set thisNefFile to nefFiles's item i
	try --> on error, just ignore this item
		set baseName to text 1 thru -5 of thisNefFile
		if (baseName & ".jpg") is not in jpgFiles then ¬
			tell application "Finder" to delete alias ((nefDir as text) & thisNefFile)
	end try
end repeat

There you go, a winner. Thank you so much. This script is going to make it quick & easy for me to save a LOT of hard disk space.

And I am going to use some of that time trying to understand why that placement of the “try” command works when my attempts failed.

If you are ever in Atlanta, let me know and I’ll do a nice portrait of you.