How to create a list of all file and folder tags used on macOS within a specific folder or drive?

Hi all,
Please could anyone tell me how to create a list of all the tags used within a given folder? The folder could be chosen as the root, or the current folder, or any custom file path. It is for macOS ver. 10.15.7 (Catalina).

Methods tried so far:

(1) The following code was run in Terminal but it returned (null) when there are actually tags in use on the drive.

mdls -name kMDItemUserTags /
// returns kMDItemUserTags = (null)

(2) I then tried to run it at a deeper level on the hard drive (in case the error was arising due to file permission restrictions) but this also returned the same ‘null’ error message above.

mdls -name kMDItemUserTags /filepath/folder
// same error message

Next I used Apple Script with the following code but this generated an error:

tell application "Finder"
    set allTags to (get tags of every item of entire contents)
end tell

return allTags

// returns `error "Finder got an error: Can’t get entire contents." number -1728 from entire contents`

Could anyone tell me how to generate a list of the tags please? Any help would be much appreciated.

TIA!

The workaround using AsObjC code:
 

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property |⌘| : a reference to current application

set theFolder to choose folder
set theFolder to |⌘|'s |NSURL|'s fileURLWithPath:(POSIX path of theFolder)

set theEnumerator to |⌘|'s NSFileManager's |defaultManager|()'s enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:0 errorHandler:(missing value)
set entireContents to theEnumerator's allObjects()

set theTags to {}
repeat with theURL in entireContents
	set {success, tagArray, theError} to (theURL's getResourceValue:(reference) forKey:(|⌘|'s NSURLTagNamesKey) |error|:(reference))
	set tagArray to tagArray as list
	if (tagArray is {missing value}) or (theTags contains {tagArray}) then
		-- do nothing
	else
		set end of theTags to tagArray
	end if
end repeat
return theTags

 

Wow!! Thank you very much KniazidisR!! That’s very helpful. So impressive!

This code did resolve the issue that I was seeking help with.

In case it’s of interest, there was a slightly unexpected result in the output which I thought it might be helpful to identify. I ran the code and selected a folder containing multiple files, but only one of which had any tag associations. That file has two tags, let’s say, “To print” and “To backup”. The above script returned this output in the console:

{{"To Print"}, {"To Print", "To Backup"}}

In Finder, the folder view shows the file with only one coloured tag identifier circle next to it (for the To Backup tag incidentally), but when the file’s properties are inspected in Finder, it shows both tags for the file correctly. Sometimes, removing the tags & reapplying them fixes this, but the visibility of tags seems to be unstable on the system I’m using (hence the original post :slight_smile: ).

I don’t know if the system’s tag indexing system has been corrupted somehow - thus the (null) error when querying kMDItemUserTags originally) and the unstable Finder tag identifiers.

Thank you very much again for your help to solve the underlying issue. These additional comments are simply for information and in case it helps identify any other potential issue with the OS tag management.

Hope you have a great day!