AppleScript:匹配和删除文件夹中的文件

AppleScript: match and delete files in folder

我的相机允许同时保存 RAW 和 JPG 格式的照片。我发现这很方便,因为在我的 Mac 上我可以快速浏览 JPG 并删除 "bad" 的。此外,我保留了 "good" JPG 的 RAW 文件,以备我需要进行一些深度编辑时使用。

我想编写一个删除所有 "bad" RAW(不再具有相应 JPG 的 RAW 文件)的 AppleScript。所有文件都在同一目录中。

这是我的大纲(与正确的语法相去甚远!):

tell application "Finder"
  set source_folder to choose folder with prompt "Please select directory."
  my clearFiles(source_folder)
end tell

on clearFiles(source_folder)
  set theItems to ""
  tell application "System Events"
    set theItems to get the name of every disk item of source_folder
  end tell
  repeat with theFile in theItems
    if the extension of theFile is "raw" and exists name of theFile & ".jpg" then
      tell finder 
        delete (name of theFile & ".raw") in source_folder
      and tell
    end if
  end tell
end clearFiles

试试这个

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