You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Mac的Automator中删除文件夹及子文件夹内的JPG文件?

我来帮你搞定这个问题!你的初始工作流思路没问题,但遇到网络文件夹失效的情况,多半是Automator原生的Get Folder Contents动作对网络卷的支持不够友好。下面给你两个靠谱的解决方案,亲测有效:

方案一:优化Automator工作流(适配网络文件夹)

这个方案用AppleScript替代原生的文件夹遍历动作,能更稳定地处理本地和网络共享文件夹:

  • 打开Automator,新建一个工作流(如果需要右键快速触发,也可以选「快速动作」)
  • 第一步保留Get selected Finder Items,这个动作负责获取你选中的目标文件夹(不管是本地还是网络挂载的)
  • 删掉原来的Get Folder Contents,添加Run AppleScript动作,把下面的代码粘贴进去:
    on run {input, parameters}
        set allItems to {}
        repeat with aFolder in input
            set folderContents to list folder aFolder with invisibles without expanding aliases
            repeat with anItem in folderContents
                set itemPath to (aFolder as text) & anItem
                set theItem to POSIX file itemPath as alias
                copy theItem to end of allItems
                -- 递归处理子文件夹
                if folder of info for theItem is true then
                    copy (run script "on run {a}
                        return list folder a with invisibles without expanding aliases
                    end run" with parameters {theItem}) to end of folderContents
                end if
            end repeat
        end repeat
        return allItems
    end run
    
  • 接下来继续用Filter Finder Items动作,设置条件为文件扩展名 is JPG(如果需要兼容小写.jpg,可以把条件改成「文件扩展名 matches JPG」并勾选「忽略大小写」)
  • 最后用Move Finder Items to Trash动作,完成删除操作

为什么这个方案有效?因为AppleScript是通过Finder的原生API来遍历文件,对SMB/AFP这类网络共享文件夹的兼容性比Automator原生动作更好,不会出现遍历中断的问题。

方案二:Shell脚本版工作流(更稳定的批量操作)

如果还是遇到问题,可以直接用Shell脚本结合Finder的删除命令,彻底规避Automator原生动作的局限:

  • 新建Automator工作流,第一步依然是Get selected Finder Items
  • 添加Run Shell Script动作,Shell选择/bin/zsh(或/bin/bash),把下面的代码粘贴进去:
    find "$@" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | while read -r file; do
        osascript -e "tell application \"Finder\" to delete POSIX file \"$file\""
    done
    
  • 保存工作流即可

这个脚本的好处是:

  • find命令能稳定遍历所有子文件夹,不管是本地还是网络卷
  • 用osascript调用Finder的delete命令,会把文件移到废纸篓(和手动删除行为一致),还能自动处理权限问题

重要注意事项

  • 确保你对目标网络文件夹有删除权限,这是所有操作生效的前提
  • 如果是网络共享文件夹,挂载时尽量勾选「连接时登录」,避免权限会话过期导致操作失败
  • 测试时先选少量文件或文件夹,确认没问题再批量操作,避免误删重要文件

内容的提问来源于stack exchange,提问作者Ovi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:29:30