theonlygusti theonlygusti - 23 days ago 9
AppleScript Question

Delete downloads older than 30 days?

I am trying to create a task in automator that moves files in ~/Downloads to the trash if they are older than 30 days.

I want this to run every day.

It's not working though, Finder just hangs and stops responding and I have to Force Quit it from activity monitor.

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)
repeat with deleteFile in deleteFileList
delete deleteFile
end repeat
end try
end tell

return input
end run

Answer Source

I'd take a different approach and use a set of actions available in Automator without the use of AppleScript.

The following workflow will accomplish that you're looking to do.

In Automator, create a new Workflow adding the following actions:

  • Get Specified Finder Items
    • Add the Downloads folder to it.
  • Get Folder Contents
    • [] Repeat for each subfolder found
  • Filter Finder Items
    • Find files where:
      • All of the following are true
        • Date last modified is not in the last 30 days
  • Move Finder Items to Trash

Save the Workflow as an Application, e.g.: Cleanup

This should run much faster then AppleScript version, it did in my testing.

Apple's preferred method to schedule something such as this is to use launchd and launchctl.

To run Cleanup Downloads, daily, I'd do the following:

  1. Add Cleanup Downloads to: System Preferences > Security & Privacy > Privacy > Accessibility
  2. Create a User LaunchAgent in: ~/Library/LaunchAgents/

    • Example: as an XML file containing:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
      <plist version="1.0">
              <string>/Applications/Cleanup Stub</string>
    • Set the value for Hours and Minutes, under StartCalendarInterval, as appropriate for your need. The example is set for: 10:00AM

  3. In Terminal run the following command to load the LaunchAgent:

    launchctl load ~/Library/LaunchAgents/

Note: See the manual pages for launchd and launchctl in Terminal, e.g. man launchctl

Or use a third party utility that has a GUI, e.g.: Lingon X

Note: I'm not associated with the developer of Lingon X, however I am a satisfied customer.

Some comments on your AppleScript code:

A repeat statement isn't necessary, just use:

move deleteFileList to trash

The current date command technically executes under current application, not Finder as Finder does not understand the current date command . Therefore, set a variable and use the variable in the command.

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

... whose modification date is less than thisDate

Now I'm not proposing you actually use the AppleScript over the workflow I proposed, I'm just pointing out some things in the code I take issue with.