Finder Tag – Auto-tagging Folders/Files upon Drop-in

finder-tag

I want to be able to automatically assign a red tag to any folder/file that is dropped into a folder sitting on my desktop. I've done some research about this and it seems like this can't really be done even with Automator or Smart Folders, even though it seems pretty simple.

Best Answer

Option 1: Using Hazel

You can auto-tag folders/files dropped into a folder using the commercial software Hazel, which according to its developer Noodlesoft, "watches whatever folders you tell it to, automatically organizing your files according to the rules you create."

Option 2: Using Folder Actions

You can auto-tag folders/files dropped into a folder using the Folder Actions feature of macOS. I wrote the following AppleScript based partly on one of Apple's default Folder Action Scripts and partly on a script provided in a discussion in the Apple Support Communities. I tested this script in Mac OS X 10.6.8, 10.9.5, and 10.11.6 and it worked in all three versions of macOS (but see the Important Caveat section below).

property label_color : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}
on adding folder items to this_folder after receiving these_items
    tell application "Finder"
        try
            repeat with i from 1 to number of items in these_items
                set this_item to item i of these_items
                set label index of this_item to red of label_color
            end repeat
        end try
    end tell
end adding folder items to

The first line of the script, property label_color : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}, is not really necessary; I just included it to show which label index number corresponds to each color. You could remove the first line and change red of label_color to 2 and the script would have the same effect.

There are a few ways to attach this AppleScript to a folder as a Folder Action Script. One way is to open Automator, create a new Folder Action, add a Run AppleScript action, and replace the content of the Run AppleScript action with this script—but this didn't work in my test. Another way, which worked in my test, is to save the script in either /Library/Scripts/Folder Action Scripts/ (for use by any user) or in ~/Library/Scripts/Folder Action Scripts/ (for use by the current user only), and then do the following steps:

  1. In the Finder, Control+click (or right click) on any folder to show the contextual menu.
  2. From the contextual menu, choose Folder Actions Setup (or Services and then Folder Actions Setup).
  3. From the prompt window, choose the name of the script that you just saved.
  4. Check the Enable Folder Actions option in the upper-left side of the Folder Actions Setup window, if it is not already checked.

Important Caveat

Technically, the previous script only sets the file label, which is not exactly the same as a tag, as John Siracusa explained in his Ars Technica review of tags implementation in OS X 10.9; however, in Finder in OS X 10.9 and later, the result of the previous script will be indistinguishable from a tag. The previous script works in versions of Mac OS X 10.6 or later.

A more robust Folder Action Script that sets a red tag and label instead of only a red label (although, as I said, the results are indistinguishable in the Finder) can be found in the AppleScript given below.

The script given below requires that you first install James Berry's tag tool (which requires OS X 10.9 or later). It can be installed easily with the package manager Homebrew (brew install tag) or MacPorts (sudo port install tag). If you install it with Homebrew, replace /opt/local/bin/tag (which is the MacPorts path) with /usr/local/bin/tag (which is the Homebrew path) in the script below.

on adding folder items to this_folder after receiving these_items
    tell application "Finder"
        try
            repeat with i from 1 to number of items in these_items
                set this_item to item i of these_items
                do shell script "/opt/local/bin/tag --add Red " & quoted form of POSIX path of this_item
            end repeat
        end try
    end tell
end adding folder items to

A third way to write the AppleScript (in OS X 10.9 and later) would be to install an AppleScript/Objective-C Library written by Shane Stanley called FileTagsLib and then replace the do shell script line in the script above with the appropriate command as described in the FileTagsLib documentation, but I have not tested this third option.