I am trying to do an automator calendar event where every Friday, it deletes files whose date modified is older than 30 days; EXCEPT files within folders labeled red.

I have my Documents folder with 3 files, one folder created today and labeled red, a folder last modified a week ago, and a pdf that was modified more than 30 days ago.

My automator script goes as follows:

[Find Finder Items] Search "Documents", ALL of the following are true:

-Date last modified is not in the last 30 days
-Label is not red

[Move Finder Items To Trash]


But this moves the red labeled folder and the older pdf, when it should only move the pdf to the trash.

Can anyone help me? Thank you!

Edit: Running macOS 10.13.3

  • Running macOS 10.13.3 – dariasx1 Feb 23 '18 at 3:14
  • Can you try using a Run AppleScript action instead, with the following code: tell application "Finder" to get every file in the (path to documents folder) whose modification date > ((current date) - 30 * days) and label index is not 2. If this works as expected, you can swap get for delete. – CJK Feb 23 '18 at 3:24
  • I tried that on Script Editor and it didn't work, I tried putting "try" and "end try" at the end but it still did not work. Am I doing something wrong? – dariasx1 Feb 23 '18 at 3:50
  • When you said "EXCEPT files within folders labeled red." is only the Folder tagged red or are the files also tagged red? – user3439894 Feb 23 '18 at 3:52
  • Only the folder is tagged red. – dariasx1 Feb 23 '18 at 3:56

My initial suggestion to the problem was to replace the actions you currently have in your Automator workflow with a Run AppleScript action that uses this command:

    tell application "Finder" to get every item ¬
        in the (path to documents folder) ¬
            whose modification date < ((current date) - 30 * days) ¬
            and label index is not 2

You would then have either added an action after this to trash those items, or change get every item to delete every item in the script. However, as @user3439894 pointed out, this won't traverse folder trees, so any items inside a folder that are older than 30 days (and not tagged red) will escape detection.

The following script is an example of a method that uses recursion to descend through the directory tree deleting files (or marking them for deletion) as it goes:

    property D : {}  # The files to delete
    property R : path to documents folder  # The root of the directory tree structure
    property age : 30 * days
    property red : 2

    descend into R

    # tell application "Finder" to delete D
    return D

    to descend into here
        local here

        tell application "Finder"

            # Mark files which are older than 30 days for deletion
            # EXCEPT any that are tagged red
            set end of D to every file in here whose label index is not red ¬
                and modification date < (current date) - age

            # This checks to see if, following the purge, the 
            # current folder will become empty.  If so, it can 
            # be deleted too.  It adds to processing time, so
            # remove this code block if you don't need it.
            count the last item of D
            if the result is equal to (count the files in here as alias list) ¬
                and (count the folders in here) is 0 then
                set the end of D to here
            end if

            # This ensures folders tagged red and their contents
            # are spared from the purge
            get the folders in here whose label index is not 2

            repeat with F in the result
                set F to the contents of F  # de-referencing

                descend of me into F
            end repeat

        end tell
    end descend

I have tested it briefly on my own fairly complex tree structure, and it appeared to work successfully. However, I'm writing this and testing it whilst quite tired, but regardless, I would always strongly advise that you test this script yourself on dummy files and folders to ensure it works. Please report back on how it goes, including—if they arise—any errors, with specific details on how I can reproduce the error myself.

  • @dariasx1 How did you get on with this ? – CJK Mar 2 '18 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .