How can I automate the deletion of files from my downloads folder but keep files I've tagged with "Keep"? I've been able to use Automator to delete files daily, but I can't work with tags in Automater. Most likely this requires AppleScript (I'm fairly new to AppleScript). I've created an AppleScript to delete files from Downloads folder, but it's not working (I can't tell if it's a permissions issue), and I'm lost on how file tags/labels work in AppleScript.

    tell application "Finder"
        delete (every item of folder "/Users/wheel58m/Library/Mobile Documents/com~apple~CloudDocs/Downloads")
    end tell
on error
    display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try

Any documentation or resources is appreciated.


Automatically Delete Files From Downloads Folder Daily Unless Tagged With “Keep”?

The following example AppleScript code can be used in a shell script with an /usr/bin/osascript shebang, or an AppleScript script/application, or a Run AppleScript action in an Automator workflow to delete any file in the targetFolder that does not have a custom "Keep" Tag set in Finder:

-- # targetFolder can be either an HFS path or a POSIX path,
-- # "path:to:target:folder:" or "/path/to/target/folder"
-- # 
-- # Change the value of targetFolder from "/path/to/target/folder"
-- # to the appropriate fully qualified pathname in either form.

set targetFolder to "/path/to/target/folder"

-- # The remaining code should not need to be modified, 
-- # unless the mdfind query needs to be changed.

set targetFolderPOSIXpath to ¬
    the quoted form of ¬
        the POSIX path of targetFolder

set shellCMD to {¬
    "mdfind -onlyin", space, targetFolderPOSIXpath, space, ¬
    "'! kMDItemKind == Folder && ! kMDItemUserTags == Keep'"} ¬
    as string

set posixFilesToDelete to ¬
    paragraphs of ¬
    (do shell script shellCMD)

set filesToDelete to {}
repeat with thisFile in posixFilesToDelete
    set thisFile to POSIX file thisFile as alias
    set the end of filesToDelete to thisFile
end repeat

if filesToDelete is not {} then ¬
    tell application "Finder" to ¬
        delete filesToDelete 

Note: Scroll as necessary to see remaining code.


  • As I do not have the target folder mentioned in the OP, I could not test for any permissions issues that may need addressing in: System Preferences > Security & Privacy > Privacy
  • The example AppleScript code was tested and work for me without issue in macOS Catalina using a temporary folder created within my Home folder while adding files and folders to it and tagging only some files with a custom "Keep" Tag in Finder.
  • As to "Automatically Delete Files" there are a number of different ways to accomplish this depending on how the example AppleScript code is used. Used as an AppleScript shell script or application it can be scheduled to run at whatever time you'd like, using any of several different methods.
    • As an AppleScript application using: Calendar
    • As an AppleScript shell script using: launchd or cron

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

  • Wow! Thanks for the detail. I was able to get this working perfectly! I'll create an AppleScript application to run with Calendar and add error handling with the documentation you mentioned. This is exactly what I needed. Thank you. – wheel58m Apr 12 at 20:36
  • @wheel58m, Really shouldn't need any additional error handling and the note at the bottom is boiler plate I usually always add just to cover the bases, so to speak. The code as is was actually tested as an AppleScript application scheduled in Calendar and worked without issues. That's not to say you can't customize the code to fit your needs. Glad it works for you! – user3439894 Apr 12 at 20:47

From what I can tell, tags are still called "labels" in Applescript. Finder will return a different "label index" depending on a file's tag/label.

tell application "Finder" to get label index of file "path:to:file"

When a file has no tags, this returns "0". When a file has a "red" tag, it returns 2, and when it has a "yellow" tag, it returns 3. When a file has a "red" and a "yellow" tag, it... still returns 3. 😬

This all feels kind of weird/fragile, but if you can't find a better way to do this (!), I'd do some experimenting to find the "label index" of your keep tag, and then check for that in your code.

  • The problem is any Finder item with a custom Tag its label index property will return 0 just like any Finder item without a Tag. So you cannot differentiate Finder items with a "Keep" Tag or Finder items without a Tag by using its label index property ! – user3439894 Apr 12 at 17:20
  • 2
    I've no clue how this all works, but I found many references to tags as distinct from labels.. stackoverflow.com/q/24599917 stackoverflow.com/q/19784075 discussions.apple.com/thread/7741109 & apple.stackexchange.com/q/131258/85275 – Tetsujin Apr 12 at 17:40
  • 2
    Just to add to my first comment, sorry, I accidentally left out "with no color assigned" as in... The problem is any Finder item with a custom Tag with no color assigned, its label index property will return 0 just like any Finder item without a Tag. Sorry for any confusion. --- IMO The safest way to achieve the goal of the OP using native macOS utilities is to focus on the kMDItemUserTags metadata attribute using an mdfind query, which is not AppleScript but can be used in conjunction with it to achieve the goal. – user3439894 Apr 12 at 21:46
  • RE: "From what I can tell, tags are still called "labels" in Applescript." -- From the AppleScript dictionary for Finder... "labeln : (NOT AVAILABLE YET) A Finder label (name and color)", so while label is mentioned it is not yet, as of macOS Big Sur, usable by a query on a Finder item, like label index would be, and would return an error, e.g. "Can’t get label ..." – user3439894 Apr 12 at 23:37

The following AppleScript is a working version of the code you posted, if you replace <SystemDrive> and <username> with the names of your system drive and username, respectively.

    tell application "Finder"
        delete (every item of folder "<SystemDrive>:Users:<username>:Library:Mobile Documents:com~apple~CloudDocs:Downloads")
    end tell
on error
    display dialog ("Error. Couldn't Move the File") buttons {"OK"}
end try

See Apple's documentation on working with files and folders in AppleScript.

I couldn't find any mention of tags in AppleScript's Finder library but Howard Oakley states that they can be added with AppleScript, hence they should be accessible, perhaps as labels as @Wowfunhappy suggests. You might need to install and learn how to use tag, as mentioned in the same article.

  • How does the AppleScript code in your answer keep from deleting the files which have a "Keep" Tag? It doesn't! The title of the question asks: "Automatically Delete Files From Downloads Folder Daily Unless Tagged With “Keep”?" – user3439894 Apr 12 at 17:19
  • 2
    I meant that it will do what the code posted in the question is meant to do. Then I explained how he might go about finding out how to access tags. – HeckFinlay Apr 12 at 17:21
  • 1
    Thanks, I'm able to get the script to delete the files now. I just needed to add the drive name and replace "/" with the colon ":". Just trying to figure out the tags, haha. – wheel58m Apr 12 at 17:44

You must log in to answer this question.

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