Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    return input
end run
like image 807
theonlygusti Avatar asked Sep 26 '17 08:09

theonlygusti


People also ask

How do you delete files from older than 30 days?

To delete files older than 30 days on Windows 10, you can use the ForFiles tool. Use this command: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”. Change “30” for the number of days you want and the folder path.


1 Answers

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 Downloads.app

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: com.me.cleanup.downloads.plist as an XML file containing:

      <?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>
      
    • 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/com.me.cleanup.downloads.plist
    

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.

like image 59
user3439894 Avatar answered Oct 05 '22 03:10

user3439894