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
http://www.satimage.fr/software/en/