删除超过30天的下载?

时间:2017-09-26 08:21:11

标签: macos automation applescript automator

我正在尝试在automator中创建一个任务,如果超过30天,将〜/ Downloads中的文件移动到垃圾箱。

我希望每天都这样。

它没有工作,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

2 个答案:

答案 0 :(得分:6)

我采用不同的方法,并使用 Automator 中提供的一组操作,而不使用 AppleScript

以下工作流程将完成您正在寻找的目标。

Automator 中,创建新的工作流程,添加以下操作:

  • 获取指定的Finder项目
    • 将“下载”文件夹添加到其中。
  • 获取文件夹内容
    • []对找到的每个子文件夹重复
  • 过滤器查找器项目
    • 查找文件:
      • 以下所有情况均属实
        • 上次修改日期不是过去30天
  • 将Finder项目移至废纸篓

工作流保存为应用,例如:清理Downloads.app

这应该比 AppleScript 版本快得多,它在我的测试中完成。

Apple 安排此类内容的首选方法是使用launchdlaunchctl

每天运行清理下载,我会执行以下操作:

  1. 清理下载添加到:系统偏好设置> 安全&隐私> 隐私>的辅助功能
  2. ~/Library/LaunchAgents/

    中创建用户LaunchAgent
    • 示例: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下设置MinutesStartCalendarInterval的值。示例设置为:上午10:00

  3. 终端 运行以下命令 加载 LaunchAgent:

    launchctl load ~/Library/LaunchAgents/com.me.cleanup.downloads.plist
    
  4. 注意:请参阅终端中launchdlaunchctl的手册页,例如: man launchctl

    或者使用具有GUI的第三方实用程序,例如:Lingon X

    注意:我与Lingon X的开发者无关,但我是一个满意的客户。

    AppleScript 代码的一些评论:

    repeat 声明是不必要的,只需使用:

    move deleteFileList to trash
    

    current date 命令技术上在current application下执行,而不是Finder,因为 Finder 无法理解current date 命令。因此,设置一个变量并在命令中使用变量

    set thisDate to get (current date) - 30 * days
    
    ... whose modification date is less than thisDate
    

    现在我并未建议您实际使用AppleScript而不是我提议的工作流程,我只是在代码中指出一些内容我问题。

答案 1 :(得分:0)

如果Finder挂起,这可能是因为&#34;整个内容&#34;中存在太多文件。可能是某些下载的项目是由带有子文件夹/子文件夹的文件夹组成的......然后是太多的文件。

相反,我建议您只查看下载文件夹的第一级:项目顶级:

Set DeleteFileList to every items of folder "Macintosh HD:Users:George:Downloads" whose modification date is less than ((get current date)) - 30 * days)

DeleteFileList的内容将由文件和文件夹组成,但我想你要删除完整的文件夹,而不仅仅是文件中的文件。