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.



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/"
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
1 Like

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