1

My photo camera allows to save pictures in RAW and JPG in parallel. I find this convenient because on my Mac I can quickly browse the JPGs and delete the "bad" ones. Besides, I keep the RAW files of the "good" JPGs in case I need to do some deep editing.

I would like to write an AppleScript which deletes all the "bad" RAWs (RAW files which don't have a corresponding JPG anymore). All files are in the same directory.

This is my outline (far away from correct syntax!):

I have copied a Testscript that should autodelete pictures in RAW as AppleScript. This script must match, if a corresponding jpeg exist, and should delete all RAW Files, where this is not the case.

set source_folder to choose folder with prompt "Please select directory."
tell application "Finder"
    set rawFiles to every file of source_folder whose name extension is "raw"
    repeat with aFile in rawFiles
        set baseName to text 1 thru -5 of (get name of aFile)
        set jpgFile to baseName & ".jpg"
        if not (exists file jpgFile of source_folder) then delete aFile
    end repeat
end tell

Nevertheless, it is not working; all files are always deleted.

2
  • Have you checked .jpg and .jpeg? It does matter in your case... Commented Mar 21, 2022 at 14:40
  • any idea how i can get this to work for both? Commented Mar 21, 2022 at 14:42

1 Answer 1

1

If, as comments suggest, you have two extensions for JPG files [jpg & jpeg], then the hacky solution is to rename one to the other. [Your script as it stands works when there are 'correct' pairs or not pairs of "jpg" & "raw"]

set source_folder to choose folder with prompt "Please select directory."
tell application "Finder"
    set fixJpg to every file of source_folder whose name extension is "jpeg"
    repeat with myFile in fixJpg
        set name extension of myFile to "jpg"
    end repeat
    set rawFiles to every file of source_folder whose name extension is "raw"
    repeat with aFile in rawFiles
        set baseName to text 1 thru -5 of (get name of aFile)
        set jpgFile to baseName & ".jpg"
        if not (exists file jpgFile of source_folder) then delete aFile
    end repeat
end tell

This makes all "jpeg" files into "jpg" then handles them in your original manner, deleting RAW files with no corresponding JPG. There's probably a better way to do this, but this one does work.

1
  • much better with 2 repeats and the jpg without e was also a problem Commented Mar 22, 2022 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .