How Can I get a List of All Finder Tags?

How Can I get a List of All Finder Tags?

Like is shown in the Finder window, not just for one item, but every tag I have created. I need this as a list of text items, so I can use to build a custom Spotlight search.

image

TIA.

I don’t believe there’s an official way, but it seems they’re stored in a property list file. So:

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

set libPath to POSIX path of (path to library folder from user domain)
set libPath to current application's NSString's stringWithString:libPath
set prefsPath to libPath's stringByAppendingPathComponent:"SyncedPreferences/com.apple.finder.plist"
set theData to current application's NSData's dataWithContentsOfFile:prefsPath
set {theStuff, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(reference)
if theStuff is missing value then error theError's localizedDescription() as text
set theTags to (theStuff's valueForKeyPath:"values.FinderTagDict.value.FinderTags.n") as list
2 Likes

Thanks, Shane, that is exactly what I needed! :+1:

“Official” doesn’t matter to me – it’s results that count. :wink:

One more thing, if you have time.
Using your great book, “Everyday AppleScriptObjC”, I think I figured out how to sort the tags array:

set theTags to (theStuff's valueForKeyPath:"values.FinderTagDict.value.FinderTags.n")
set theTags to theTags's sortedArrayUsingSelector:"localizedStandardCompare:"

set tagList to theTags as list

The results look good. Is there a better way?

Well localizedStandardCompare: is Finder-style sorting, but unless you’re including numbers the result will be the same as caseInsensitiveCompare:. That’s the right approach, though.

1 Like

I was just researching the subject of Finder tags yet again and this thread came up.

I’ve been also relying on the ~/Library/SyncedPreferences/com.apple.finder.plist file for years to get the list of existing Finder tags.

Well it looks like that this file doesn’t exist anymore since Monterey.

So it appears that if user creates any custom tags there’s no known way to retrieve their names/colors anymore. At least I couldn’t find any way to do it…

Hey Leo,

This was working in 2019, so it might work for you:

I didn’t find a binary and had to make the command line tool, but it worked on my macOS 10.14.6 Mojave system.

I found 822 tags on my system and got usage counts as well.

-Chris

1 Like

Thanks Chris,

Interesting tool - will look into this!

By now I realized that all I need for my current need is the NSURLLabelNumberKey resource key: it will give me the fixed color index of the first tag applied to a file regardless of the tag name. Then I can apply this color in the UI, which is good enough for this particular task at this point.

P.S. This approach isn’t useful, of course, to retrieve the colors of multiple tags applied to the file. For multiple tags, Apple only lets retrieve their names - but not colors - via NSURLTagNamesKey. Once I decide to display multiple colors I’ll explore the tool you suggested.

Edit:

OK after exploring the code on GitHub I realized that I can use kMDItemUserTags to get all tags’ info including color (via either NSMetadataQuery or xattr). So all problems are solved for the present and future goals.

1 Like

@ccstone and @leo_r I have found that using tag only returns tags that are currently present on at least on file. So tags that appear in Finder, but are not currently present on any file are not returned. Is there a different way to produce a list of all tags, even if they‘re not currently in use?

-Chris

Hey Chris,

Now you’ve done it! You’ve made me think outside the box…

By all accounts there’s no known way to get this information programatically after Mojave.

See: Post #10 in this topic.

However – I’d forgotten that the “All Tags…” panel can be opened in a Finder Window like so:

That means you can employ UI-Scripting for the task.

Note that I’m using Mojave here, so the UI code structure may require some adjustments to work on your system.

AppleScript Code
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/18 19:36
# dMod: 2023/01/18 19:36 
# Appl: System Events, Finder, BBEdit
# Task: Retrieve Finder Tags from macOS.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Finder, @System_Events, @Extract, @Finder, @Tags
# Test: macOS 10.14.6 Mojave
--------------------------------------------------------
(*

NOTES:

 - Open a Finder Window.
 - Scroll to the bottom of the tag list in the side bar.
 - Click “All Tags” and wait for the Tags Panel to open
 - Uncomment the BBEdit segment if you have a copy.
 - Run the script.

*)
--------------------------------------------------------

# UI structure for _my_ system which may be different than yours.

tell application "System Events"
   tell application "System Events"
      tell application process "Finder"
         tell (first window whose subrole is "AXStandardWindow")
            tell splitter group 1
               tell splitter group 1
                  tell scroll area 1
                     tell table 1
                        tell rows
                           set tagList to UI element 1's text field 1's value
                        end tell
                     end tell
                  end tell
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

set AppleScript's text item delimiters to linefeed
set tagList to tagList as text

# tell application "BBEdit"
# 	activate
# 	make new text document with properties {name:"Finder Tag List.txt", text:tagList}
# end tell

--------------------------------------------------------

This code produced 837 unique tags on my system, whereas the tags cli exe produced 824 tags with a whole lot of duplicates where only the case was different.

The script took about 25 seconds to run on my system.

-Chris

I asked this question on another forum and the solution was found just a few days ago.

The list of all available tags is now stored here (since Monterey):

~/Library/SyncedPreferences/com.apple.kvs/com.apple.KeyValueService-Production.sqlite

Shell script to get the required data:

sqlite3 ~/Library/SyncedPreferences/com.apple.kvs/com.apple.KeyValueService-Production.sqlite "SELECT writefile('path/to//tags.plist', ZPLISTDATAVALUE) FROM ZSYDMANAGEDKEYVALUE WHERE ZKEY = 'FinderTagDict';"

Then read the tags.plist file.

The credit goes to people on this thread:

https://apple-dev.groups.io/g/cocoa/topic/96216930?p=Created,,,20,1,0,0#

2 Likes