MacOS – Automator: Filter Finder Items selecting wrong files

applescriptautomatormacos

I wanted an Automator folder action to automatically remove old downloads from my downloads folder whenever I download a new file (I wanted to avoid making this a scheduled job).

The workflow looks like this:
enter image description here

When I test it, it works fine – selecting only files older than 60 days. However when I actually download a new file to the downloads folder it sends the new file to the trash.

What gives? Is the timestamp on the new file not correct until after the download finishes or something?

EDIT: I discovered that it deletes the file being added to the folder no matter what. Meaning – if I drag and drop a new file to the folder, the workflow deletes it even though the time stamp is from today.

Best Answer

Here is a solution to bypass Automator completely. Just save this script in the Script Editor.App to your /Users/your_shortname/Library/Workflows/Applications/Folder Actions folder. Once you do that, that folder action script will be available to choose from in your Services/Folder Acions Setup menu in Finder by control + click the downloads folder

This is the version if you are going to use Script Editor.app

on adding folder items to this_folder after receiving these_items
    tell application "Finder"
        set nameExtensionz to {"download"}
        set deleteFilez to items of (path to downloads folder) whose creation date < ((current date) - (days * 60)) and (name extension) is not in nameExtensionz
        delete deleteFilez
    end tell
end adding folder items to

If you do not feel comfortable with Script Editor app, in Automator app you can add a run AppleScript action and add my code. Just view the screenshot below. You will not need to worry about the new downloaded file getting deleted

This is the version to use if you are going to add an AppleScript action to an Automator Workflow folder action

tell application "Finder"
        set nameExtensionz to {"download"}
        set deleteFilez to items of (path to downloads folder) whose creation date < ((current date) - (days * 60)) and (name extension) is not in nameExtensionz
        delete deleteFilez
end tell

enter image description here