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:
- Add Cleanup Downloads to: System Preferences > Security & Privacy > Privacy > Accessibility
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
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.