How to Retrieve All Available Finder Tags

How do I retrieve all available Finder tags?

NSWorkspace.shared().fileLabels only returns the system tags that were available when the user account was created (the default system tags).

There's unfortunately no API in macOS to retrieve the tags that you have created yourself in the Finder.

The solution is to parse the ~/Library/SyncedPreferences/com.apple.finder.plist:

func allTagLabels() -> [String] {
// this doesn't work if the app is Sandboxed:
// the users would have to point to the file themselves with NSOpenPanel
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { $0["n"] as? String }
}
}
return []
}

let all = allTagLabels()
print(all)

This gets all Finder tags labels.

You can also select only the custom tags (ignore the system ones):

func customTagLabels() -> [String] {
let url = URL(fileURLWithPath: "\(NSHomeDirectory())/Library/SyncedPreferences/com.apple.finder.plist")
let keyPath = "values.FinderTagDict.value.FinderTags"
if let d = try? Data(contentsOf: url) {
if let plist = try? PropertyListSerialization.propertyList(from: d, options: [], format: nil),
let pdict = plist as? NSDictionary,
let ftags = pdict.value(forKeyPath: keyPath) as? [[AnyHashable: Any]]
{
return ftags.flatMap { tag in
if let n = tag["n"] as? String,
tag.values.count != 2
{
return n
}
return nil
}
}
}
return []
}

How to get list of files on Mac that have been tagged by Finder with a color?

It's possible.

But you'll have to rely on xattr, xattr-lib should do the trick.

Some examples have already been made on how to use the xattr command line tool, those can be found here:

  • https://apple.stackexchange.com/questions/110662/possible-to-tag-a-folder-via-terminal
  • Get file's "get info" attributes in mac using python
  • How can I add OS X "tags" to files programmatically?

I hope these help you out.

In worse case you could use a tool called tag:

tag -l file # list
tag -a tag1 file # add
tag -s red,blue file # set
tag -r \* file # remove all tags
tag -f green # find all files with the green tag
tag -f \* # find all files with tags
tag -m red * # match (print files in * that have the red tag)

This in combination with say subprocess.Popen could solve your needs.

set finder tags from swift?

The only way I know of to do it is to write the com.apple.metadata:_kMDItemUserTags extended attribute, which you can do using the setxattr API.

Applescript, show all files with tag

You can use the GUI Scripting to click on left-pane in the finder window.

Because this script use GUI Scripting to control the user-interface, you must add the applet to an approval list, displayed in the Security & Privacy system preference pane.

set tagName to "Chemistry" -- the full tag's name
my showFinderWindow(tagName)

on showFinderWindow(thisTag)
tell application "Finder" to if not (exists Finder window 1) then make new Finder window
tell application "System Events"
tell process "Finder"
repeat with i in windows
tell outline 1 of scroll area 1 of splitter group 1 of i to if exists then
tell (first row whose value of its static text 1 is thisTag) to if exists then perform action "AXOpen" of static text 1
exit repeat
end if
end repeat
end tell
end tell
end showFinderWindow

Applescript Finder Tags

The new tags aren’t directly supported in AppleScript yet. You have to read them and set them via shell scripting, which you can access from AppleScript with the “do shell script” command. The shell command that you want is “xattr” which enables you to manipulate file metadata on an OS X system. You can type “man xattr” into the Terminal application to get the xattr manual.

There is also a 3rd party tool called “tag” which may be helpful, because xattr is fairly complicated:

Tag — A command line tool to manipulate tags on Mavericks files, and to query for files with those tags.

http://brewformulas.org/Tag



Related Topics



Leave a reply



Submit