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.
The solution is to use the OS X built-in Automator and the great tag command-line utility.
- Create a new Service that accepts selected files/folders from Finder (follow the steps here)
In Run Shell Script, select "Pass input:" as
argumentsand 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
TAGwith the name of the tag you want to use and
tagwith the location where tag is installed (the one above should be default for brew).
- 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.