删除超过30天的下载?

the*_*sti 5 macos applescript automation 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
Run Code Online (Sandbox Code Playgroud)

use*_*894 8

我采取了不同的方法,并使用Automator中的一组操作,而不使用AppleScript.

以下工作流程将完成您要做的事情.

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

  • 获取指定的Finder项目
    • 将Downloads文件夹添加到其中.
  • 获取文件夹内容
    • []对找到的每个子文件夹重复上述步骤
  • 过滤器查找器项目
    • 查找文件:
      • 以下所有都是真的
        • 上次修改日期不是最近30天
  • 将Finder项目移至废纸篓

工作流保存为应用程序,例如:Cleanup Downloads.app

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


Apple安排这样的事情首选方法是使用launchdlaunchctl.

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

  1. 添加清理下载到:系统首选项 > 安全和隐私 > 隐私 > 辅助功能
  2. 在以下位置创建用户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>
      
      Run Code Online (Sandbox Code Playgroud)
    • 根据需要为Hoursand和Minutesunder 设置值StartCalendarInterval.示例设置为:上午10:00

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

    launchctl load ~/Library/LaunchAgents/com.me.cleanup.downloads.plist
    
    Run Code Online (Sandbox Code Playgroud)

注:请参阅手册页launchdlaunchctl在终端,如man launchctl

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

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


您对AppleScript 代码的一些评论:

一个repeat 说法是没有必要的,只要使用:

move deleteFileList to trash
Run Code Online (Sandbox Code Playgroud)

current date 命令在技术上执行current application,而不是Finder因为Finder不理解该current date 命令.因此,设置一个变量,并使用变量的命令.

set thisDate to get (current date) - 30 * days

... whose modification date is less than thisDate
Run Code Online (Sandbox Code Playgroud)

现在我并不是说你实际上在我提出的工作流程中使用AppleScript ,我只是在我讨论的代码中指出了一些问题.