删除超过30天的下载?
我想在automator中创建一个任务,将〜/下载的文件移动到垃圾桶中,如果它们超过30天。删除超过30天的下载?
我希望这个每天运行。
虽然它不工作,Finder只是挂起并停止响应,我必须强制退出活动监视器。
on run {input, parameters}
tell application "Finder"
set deleteFileList to (files of entire contents of folder alias "Macintosh HD:Users:George:Downloads" whose modification date is less than ((get current date)) - 30 * days)
try
repeat with deleteFile in deleteFileList
delete deleteFile
end repeat
end try
end tell
return input
end run
我会采取不同的方式,并使用一组行动提供的Automator无需使用AppleScript的。
以下工作流程将完成您正在寻找的工作。
在的Automator,创建一个新的工作流加入以下操作:
- 获得指定的Finder项
- 添加下载文件夹吧。
- 获取文件夹内容
- []重复每个子文件夹中
- 过滤器Finder项目
- 查找文件,其中:
- 以下的所有都为真
- 最后修改日期是不是在过去30天
- 以下的所有都为真
- 查找文件,其中:
- 将Finder项废纸篓
保存工作流程作为应用,如:清理下载。应用程序
这应该运行得更快然后AppleScript版本,它在我的测试。
苹果首选方法来安排这样的东西,因为这是使用launchd
和launchctl
。
运行清理下载,每天,我会做到以下几点:
- 添加清理下载到:系统预置>安全&隐私>隐私>无障碍
-
创建一个用户LaunchAgent在:
~/Library/LaunchAgents/
-
举例:
com.me.cleanup.downloads.plist
为包含XML文件:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.me.cleanup.downloads</string> <key>ProgramArguments</key> <array> <string>/Applications/Cleanup Downloads.app/Contents/MacOS/Application Stub</string> </array> <key>RunAtLoad</key> <false/> <key>StartCalendarInterval</key> <array> <dict> <key>Hour</key> <integer>10</integer> <key>Minute</key> <integer>00</integer> </dict> </array> </dict> </plist>
设置
Hours
和Minutes
值,StartCalendarInterval
下,以适合您的需要。该示例设置为:10:00 AM
-
-
在终端运行以下命令到负载的LaunchAgent:
launchctl load ~/Library/LaunchAgents/com.me.cleanup.downloads.plist
注:见终端中的launchd
和launchctl
的手册页,例如man launchctl
或者使用第三方工具,它有一个GUI,例如:Lingon X
注:我不跟Lingon X的开发者相关的,但是我是一个满意的客户。
您的AppleScript代码一些评论:
一个repeat
声明是没有必要的,只要使用:
move deleteFileList to trash
的current date
命令技术上执行下current application
,而不是Finder
作为查找器不理解current date
命令。因此,设置为 a 变量并在命令中使用变量。
set thisDate to get (current date) - 30 * days
... whose modification date is less than thisDate
现在,我不建议你居然在工作流程我建议使用AppleScript的,我只是指出了一些东西,在代码我再跟。
如果Finder挂起,这可能是因为“整个内容”中有太多文件。这可能是因为一些下载的项目是由具有子文件夹/子文件夹的文件夹组成的......然后是太多的文件。
相反,我建议你只看看下载文件夹的第一级:项目顶层:
Set DeleteFileList to every items of folder "Macintosh HD:Users:George:Downloads" whose modification date is less than ((get current date)) - 30 * days)
DeleteFileList的内容将制成的文件和文件夹,但我猜你想删除完整的文件夹,而不仅仅是其中的文件。
@theonlygusti,我已更新我的答案,以包含信息来安排使用工作流应用程序。 – user3439894
我假设“[]对每个重复...”意思是为每个找到的子文件夹重复一遍?所以,[这是它应该是什么样子?](http://i.cubeupload.com/hrJBcx.png) – theonlygusti
@theonlygusti,在** Automator **中,创建一个新的**应用程序** _workflow_,添加以下_actions_:... :)是的,将其另存为应用程序。 – user3439894