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:

Imprint | Privacy Policy