AppleScript: match and delete files in folder

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!):

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
share improve this question
 

1 Answer

up vote 1 down vote accepted

try this

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值