Tag selected files in Finder with single keyboard shortcut

finder-tagshortcut

It's been well covered how to create a keyboard shortcut for the Tags… menu option in Finder. What I want is a way to tag selected files or folders with a single keyboard shortcut. The same shortcut should untag them.

Best Answer

The solution is to use the OS X built-in Automator and the great tag command-line utility.

  1. Create a new Service that accepts selected files/folders from Finder (follow the steps here)
  2. In Run Shell Script, select "Pass input:" as arguments and use the following code:

    TAG="Red"
    tag="/usr/local/bin/tag"
    
    for filename in "$@"
    do
      if $tag --list --no-name "$filename" | grep "$TAG\b"; then
        $tag --remove "$TAG" "$filename"
      else
        $tag --add "$TAG" "$filename"
      fi
    done
    

    replacing the value of TAG with the name of the tag you want to use and tag with the location where tag is installed (the one above should be default for brew).

  3. Assign a shortcut to this service in the System Settings.

The script simply checks for each file whether it is already tagged with the given tag, and removes or adds the tag to switch it. You can go fancier from here.