Add color Tag by keyboard shortcut

Hey there – I'd like to set color tags in finder using a keyboard shortcut. Is there any way to realize this in BTT? I'm looking for something like this: https://www.macrumors.com/how-to/tag-finder-items-macos-keyboard-shortcuts/

Thanks!

I think the easiest solution is to use an automator workflow like in your link and trigger it using the BTT predefined action "Start Automator Workflow".

The workflow could look like this:

There are probably many other ways to achieve this, but that sounds like an easy one.

Thanks for the quick reply! It does work, but it takes some 4-5 seconds until the tag appears - would you know any quicker way to achieve this?

I think the main problem is that Finder takes a bit to "refresh".

You can try whether doing it with Apple Script & the xattr command is faster:

tell application "Finder" to repeat with filename in (get selection)
	set filePath to POSIX path of (filename as alias)
end repeat

do shell script "xattr -w com.apple.metadata:_kMDItemUserTags '<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><array><string>Red</string></array></plist>' " & "\"" & filePath & "\""
return selection

1 Like

you're a genius. this helps so much. thank you. bettertouchtool is the cornerstone of my workflows!

You can also use this (pure) AppleScript:

tell application "Finder" to repeat with filename in (get selection)
	set filePath to POSIX path of (filename as alias)
	-- set Tag(s)
	my (Tags's setTagsFor:filePath newTags:{"Red"}) -- or {"Read", "Blue", "Green"} or {} to clear
	-- read Tags
	set theTags to my (Tags's getTagsFor:filePath)
end repeat

-- Helper Script
script Tags
	use scripting additions
	use framework "Foundation"
	
	on getTagsFor:posixPath -- get the tags
		set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
		set {theResult, thetags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
		if thetags = missing value then return {"NO Tags"}
		return thetags as list
	end getTagsFor:
	
	on setTagsFor:posixPath newTags:thetags -- set the tags
		set aURL to current application's |NSURL|'s fileURLWithPath:posixPath
		set {theResult, theError} to aURL's setResourceValue:(thetags) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
		if theResult is false then error (theError's |localizedDescription|() as text) number (theError's code() as integer)
	end setTagsFor:newTags:
end script
1 Like

Awesome. Thank you :pray:

I just discovered this. It works great for me, but there is one change I want to make and I cannot figure it out from either version.

I want the button to toggle the respective colour tag on the selected file(s). I'm also not interested in returning any results, as I only want an action, not to surface any information into BTT.

I already have a script which will do this toggle by using the open source tag tool, which I have hooked up to Alfred. But I cannot figure out how to integrate that script call into the AppleScript loop.

My script is run by ~/toggleTag.sh Purple /path/to/some/file /path/to/another/one

It works like this:

#!/bin/bash
COLOUR=$1
shift
for FILE in "$@"
do
  CURRENT=$(tag --no-name --list "$FILE")
  MODE="ADD"
  test "${CURRENT#*$COLOUR}" != "$CURRENT" && MODE="REMOVE"
  if [ "$MODE" == "ADD" ]
  then
    /opt/homebrew/bin/tag --no-name --no-tags --add "$COLOUR" "$FILE"
  else
    /opt/homebrew/bin/tag --no-name --no-tags --remove "$COLOUR" "$FILE"
  fi
done

I eventually got some help in another forum and ended up with this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property rootCmd : "/Users/allisterjenks/toggleTag.sh Yellow "

on run
	
	tell application "Finder"
		set theFiles to the selection
		if theFiles is {} then return
	end tell
	
	repeat with theFile in theFiles
		tell application "Finder" to set theFileAlias to theFile as alias
		set theFilePath to the POSIX path of theFileAlias
		set theSource to the quoted form of theFilePath
		set theCmd to rootCmd & theSource
		do shell script theCmd
	end repeat
	
end run

It runs really well and the great thing is it works even when QuickLooking a file.