How to list all Spotlight tags?

I’m wondering if it’s possible to generate a list of all the tags I’ve assigned and are currently being used by Spotlight. I’m hoping to see what I’ve already done and create consistency in my tagging. I’m not sure if applescript is the correct tool for this and I’m a total newbie, but I was just wondering if it’s possible.

Apparently I’ve never posted this before. I made it a while ago, but I haven’t used it recently.

set tempFile to quoted form of POSIX path of ((path to temporary items folder as Unicode text) & "Comment Stats.txt")

choose folder with prompt "Get Spotlight comment stats for all items under this folder:"
quoted form of POSIX path of result

(*	What does that shell script do?
	1.	Use the Spotlight index to find items which have a comment
	2.	Escape the spaces for the next command
	3.	Get the comment info (and extra lines) for each item that was found
	4.	Get rid of the extra lines, leaving only the comment info
	5.	Cleanup the info, leaving the actual comments; Separate words inside comments (if seperated by a comma and space)
	6.	Remove some blank lines (?)
	7.	Sort the primary comment file
	8.	Write the primary comment file
	9.	Return the total comment count for now

do shell script "/usr/bin/mdfind -onlyin " & result & " \"kMDItemFinderComment == '*'cwd\"" & ¬
	" | /usr/bin/sed 's/\\ /\\\\\\ /g'" & ¬
	" | /usr/bin/xargs /usr/bin/mdls -name kMDItemFinderComment" & ¬
	" | /usr/bin/grep '^kMDItemFinderComment'" & ¬
	" | /usr/bin/ruby -ne 'print $_[24..-1].delete(\"\\\"\").gsub(\", \", \"\\n\")'" & ¬
	" | /usr/bin/grep '^[[:alnum:]]' " & ¬
	" | /usr/bin/sort -fd" & ¬
	" | /usr/bin/tee " & tempFile & ¬
	" | /usr/bin/wc -l"
set totalComments to result as integer

do shell script " /usr/bin/sort -fdu " & tempFile
set commentList to paragraphs of result

set output to {}
repeat with thisItem in commentList
	do shell script " /usr/bin/grep '^" & thisItem & "$' " & tempFile & " | /usr/bin/wc -l"
	set end of output to result & tab & thisItem
end repeat

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 10
set output to "" & output
set AppleScript's text item delimiters to ASTID

display dialog output with title "Comment Stats  (" & totalComments & " total)" buttons {"OK"} default button 1

Thanks! It returned results on some folders, but returned nothing when I tried running it on a “top level” folder that has many subfolders within it. Is there a limit to how many folders it can account for? Also, is there any way to generate a plain text file so I can print out the results?

There shouldn’t be.

Replace the last line in the original script with this:

set outputFile to (path to desktop as Unicode text) & "Comment Stats.txt"
writeFile from output into outputFile with bom without appending given class:«class utf8»

display dialog output with title "Comment Stats (" & totalComments & " total)" buttons {"Open Output File", "OK"} default button 2

if button returned of result is "Open Output File" then
	tell application "System Events" to open outputFile
end if

on writeFile from someData into someFile given class:textClass, bom:includingBOM, appending:appending
		tell (class of someFile) to if it is not file or it is not alias then set someFile to file specification someFile
		open for access someFile with write permission
		set fileRef to result
		if not appending then set eof of fileRef to 0
		if includingBOM and ((get eof of fileRef) is 0) then
			if textClass is Unicode text then write (ASCII character 254) & (ASCII character 255) to fileRef
			if textClass is «class utf8» then write (ASCII character 239) & (ASCII character 187) & (ASCII character 191) to fileRef
		end if
		write someData to fileRef as textClass starting at eof
		close access fileRef
		return true
	on error errMsg number errNum
			close access fileRef
		end try
		error errMsg number errNum
		--return false
	end try
end writeFile

See also: writeFile

I replaced the last line of the original script and it worked beautifully! Unfortunately, it still doesn’t work on the top level folder… does it matter that the folder name has a “!” in it? as in “!Professional”…