mac用户可通过automator构建五种自动清理下载文件夹方案:一、快捷键触发的快速操作;二、launchd定时后台执行;三、带预览确认的可视化工作流;四、folder action响应式即时清理;五、applescript图形化交互选择策略。

如果您希望在Mac上高效删除下载文件夹中长期积压的冗余文件(如重复安装包、过期压缩包、临时媒体等),避免手动筛选耗时且易遗漏,可通过Automator结合系统原生能力构建可复用、可调度的自动清理脚本。以下是多种互不依赖、可独立部署的进阶方案:
一、用Automator创建“快速操作”并绑定快捷键
该方法将Shell清理逻辑封装为访达上下文菜单项,支持一键触发、无需打开终端,且可自由分配全局热键,适合高频清理场景。
1、启动“Automator”应用,新建文档,选择“快速操作”类型。
2、在右侧操作库中搜索“运行 Shell 脚本”,将其拖入工作流区域。
3、将Shell设为/bin/zsh,在脚本框内粘贴以下命令:
find ~/Downloads -maxdepth 1 -type f \( -name "*.dmg" -o -name "*.pkg" -o -name "*.zip" -o -name "*.tgz" -o -name "*.rar" \) -mtime +30 -delete
4、点击右上角“设定”图标,勾选“工作流接收‘无输入’”,并启用“在‘访达’中可用”。
5、保存为“清理30天前下载文件”;随后进入“系统设置 → 键盘 → 键盘快捷键 → 快速操作”,找到该项并分配快捷键(如Command+Option+Shift+D)。
二、用Automator制作定时应用程序并配合launchd用户级调度
此方案脱离图形界面依赖,以守护进程方式后台运行,确保即使Mac未登录图形会话也能准时执行清理,适用于服务器类或长期开机环境。
1、打开Automator,新建文档,选择“应用程序”类型。
2、添加“运行 Shell 脚本”操作,输入以下命令:
#!/bin/zsh
cd ~/Downloads
find . -maxdepth 1 -type f -name "*.tmp" -delete
find . -maxdepth 1 -type f -name ".*.part" -delete
3、保存为“AutoCleanDownloads.app”,建议置于~/Applications目录(若不存在请先创建)。
4、在终端中执行:
mkdir -p ~/Library/LaunchAgents
touch ~/Library/LaunchAgents/com.user.downloadclean.plist
5、用文本编辑器打开该plist文件,粘贴以下内容(示例:每日凌晨2点执行):
6、加载任务:
launchctl load ~/Library/LaunchAgents/com.user.downloadclean.plist
三、用Automator构建带预览确认的工作流
该方案在执行删除前主动列出待清理文件,提供可视化确认环节,防止误删关键临时文件(如正在下载的.part文件或未完成解压的临时目录)。
1、打开Automator,新建“快速操作”,选择“文件夹”作为输入类型。
2、添加“获取指定的 Finder 项目”操作,点击“添加”按钮,输入路径:~/Downloads。
3、添加“运行 Shell 脚本”操作,Shell设为/bin/zsh,脚本内容为:
for f in "$1"/*; do
if [ -f "$f" ] && [[ "$f" == *.log || "$f" == *.cache || "$f" == *".download" ]]; then
echo "$f"
fi
done
4、添加“显示提醒”操作,在消息中插入“运行 Shell 脚本”的输出,并设置按钮为“继续”与“取消”。
5、添加第二个“运行 Shell 脚本”,仅当上一步点击“继续”时执行:
find "$1" -maxdepth 1 \( -name "*.log" -o -name "*.cache" -o -name "*.download" \) -type f -delete
6、保存为“预览式清理下载日志缓存”,启用后可在访达中右键下载文件夹→“快速操作”调用。
四、用Automator+Folder Action实现响应式即时清理
此方案监听下载文件夹内容变更事件,一旦检测到特定扩展名文件写入即刻触发过滤逻辑,适用于需严格控制下载区洁净度的开发或安全审计环境。
1、在访达中进入~/Downloads,右键空白处选择“服务 → 设置文件夹操作…”(若未显示,请先在“系统设置 → 键盘 → 键盘快捷键 → 服务”中启用“设置文件夹操作”)。
2、点击“新建文件夹操作”,选择“运行 Shell 脚本”操作。
3、Shell设为/bin/zsh,脚本内容为:
#!/bin/zsh
if [[ "$1" == *.tmp ]] || [[ "$1" == *.crdownload ]]; then
rm -f "$1"
osascript -e 'display notification "已自动清除临时下载文件" with title "Downloads 监控"'
fi
4、勾选“对已存在的项目运行此操作”,并保存为“即时拦截临时下载”。
5、此后任何以.tmp或.crdownload结尾的文件被写入下载文件夹,将在毫秒级内被静默删除并弹出通知。
五、用Automator封装AppleScript实现图形化交互清理
该方法融合AppleScript的GUI能力与Automator的流程控制,提供下拉菜单供用户选择清理策略(按时间/按类型/按大小),全程无需终端输入,适合非技术用户日常维护。
1、打开Automator,新建“快速操作”,接收“无输入”。
2、添加“运行 AppleScript”操作,粘贴以下代码:
tell application "Finder"
set downloadsFolder to folder "Downloads" of home
set allItems to every item of downloadsFolder
set choices to {"按修改日期(7天前)", "按文件类型(.dmg/.pkg)", "按大小(大于100MB)"}
set choice to choose from list choices with prompt "请选择清理策略:" default items "按修改日期(7天前)"
if choice is false then return
if item 1 of choice is "按修改日期(7天前)" then
repeat with anItem in allItems
if (modification date of anItem end repeat
else if item 1 of choice is "按文件类型(.dmg/.pkg)" then
repeat with anItem in allItems
if (name extension of anItem is "dmg" or name extension of anItem is "pkg") and (class of anItem is file) then delete anItem
end repeat
else if item 1 of choice is "按大小(大于100MB)" then
repeat with anItem in allItems
if (size of anItem > 100000000) and (class of anItem is file) then delete anItem
end repeat
end if
end tell
3、保存为“交互式下载清理”,启用后可在聚焦搜索中输入名称直接运行,或通过访达右键菜单调用。











