Finder labels (tags?)

I’m reminded of why I tried a number of times over the years to get a handle on AppleScript and why I quit each time. Discoverability seems almost impossible and the errors returned don’t help much. Each thing I try to do just gets me a number of errors.

I’m trying to build a script to help me manage Finder labels (which the Finder itself shows as ‘tags’, but searching for ‘tags’ or ‘tag’ in Finder’s dictionary returns nothing, while searching for ‘label’ does), and I can’t even get the current tag on a file. What’s wrong with this?

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

tell application "Finder"
	set currentLabel to the name of the label of item "test.md" of home
end tell

Which returns this error:

Finder got an error: Can’t get label of document file "test.md" of folder "dr" of folder "Users" of startup disk.

You may try:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
	set currentLabel to label index of item "test.md" of home
end tell

Oops, it appears that I forgot the correct tags required to format embedded code.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 13 mai 2021 18:27:24

Thanks for the reply. This returns 0, not the label itself. I put a label sd8on that test file.

The character to format code is the back tick (accent grave si tu préfères): ```

Finder’s scripting dictionary states that the label class object isn’t yet available, although the term does successfully resolve during compilation.

Therefore, with respect to vanilla Applescript, you’re limited to the [most recently applied] coloured label referenced by label index (1=orange, 2=red, 3=yellow, 4=blue, 5=purple, 6=green, 7=grey, 0=none).

To manipulate named tags, you’ll have to use ASObjC’s NSURL class, with which you can access the NSURLTagNamesKey resource key.

Thanks for the reply, I appreciate. It seems I’m not very good at picking “easy” projects to start learning :wink: Or perhaps there is nothing easy with AppleScript :thinking:

It’s easy and pretty powerful. The language itself is fairly simple and surprisingly powerful. The biggest limitations are in how AppleScript is implemented within each app, including Apple’s own apps, like Finder.

Once you get past the initial learning curve (much easier using Script Debugger, and asking questions here) you can get into a scripting grove.

In this case when I saw your post I opened the Finder dictionary and did a search for Label. As CJK pointed out, that’s not available yet, but then I found the label index of the Item class, and from there it was pretty easy to see what can and can’t be done from Applescript.

1 Like

It’s simply a case of neglect: the Finder’s dictionary hasn’t been updated since before tags existed.

If you do want to set tags rather than labels, you can use my FileManagerLib from here:

The three relevant commands are tags of, set tags of, and add tags.

2 Likes

Shane’s library is (of course) outstanding and simplifies all the tasks related to tags. I use it every day in my pro workflows.

1 Like

Thank you all for the encouragements and pointing me to relevant libraries, that helps a lot! I’ll dig deeper into this and see what I can do :slight_smile:

Where is a good place to learn about using libraries like FileManagerLib?

Tu peux essayer:

set vFile to (get choose file)'s POSIX path
set tCMD to “xattr -p com.apple.metadata:_kMDItemUserTags " & vFile & " | xxd -r -p | plutil -convert json - -o -”
set vTAGS_list to do shell script tCMD

– [“Red\n6”,“Orange\n7”,“Yellow\n5”] – la liste des «labels» brute

set vTAGS_list to vTAGS_list’s characters 3 thru -3 as string – enlever les «[]»
set vTAGS_list to my fSnR(vTAGS_list, “”,"", “,”) – enlever les «"»
set vTAGS_list to my fSnR(vTAGS_list, “\n”, “=”) – remplacer les «\n» par «=»

return my fTID(vTAGS_list, “,”) – retourne la liste des «labels»; couleur et No

on fTID(LeItem, LeDel)
tell (a reference to AppleScript’s text item delimiters)
set {oldTIDs, contents} to {contents, LeDel}
set {x, contents} to {text items of (LeItem as string), oldTIDs}
return x
end tell
end fTID

on fSnR(LeData, avant, apres)
tell (a reference to my text item delimiters)
set {oTID, contents} to {contents, avant}
set {LeData, contents} to {LeData’s text items, apres}
set {LeData, contents} to {LeData as Unicode text, oTID}
end tell
return LeData
end fSnR

Nothing specific really. Just put the library in ~/Library/Script Libraries/ with any name you want and use that name to call the library in the script:

for ex, I have a:

use Shane : script "Shane_tags"

at the beginning of my script and then I use it like this:

## mise en place du tag "courant"	
Shane's setTags:{"courant"} forPath:(POSIX path of (project_folders as alias))
Shane's setTags:{"courant"} forPath:(POSIX path of (project_location))

Et voilà !

Start with the Script Libraries section here:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_script_objects.html#//apple_ref/doc/uid/TP40000983-CH207-SW6

Thanks everyone, very useful. Lots of learning to do! :slight_smile:

Just for posterity, in case someone else like me comes along at a later date, this is the code that gets the tag I wanted.

Get this library: FileManagerLib

Put it in ~/Library/Script Libraries (and create the folder if it doesn’t exist).

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use Shane : script "FileManagerLib" version "2.3.5"

tell application "Finder"
	set currentLabel to tags of "~/test.md"
end tell

This will return a list of items in currentLabel, each item being one tag.

1 Like

Two niggles:

  • If the library defines terminology and you’re addressing it that way, there’s no need to define a property for it — you can just say use script…. (Not that it hurts to include a property.)

  • It’s nothing to do with the Finder, so you shouldn’t put it in a Finder tell block. Sometimes doing this with apps doesn’t matter, but it’s good to get into the habit of only including in tell blocks code that relates to the app in question. It can cause problems otherwise.

Two niggles

Thank you for the precisions. So apparently this is the code.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "FileManagerLib"

set currentLabel to tags of "~/test.md"

It amazes me how simple it is once you see it, and how hard it is to find how to do it like that. I’ve tried use "FileManagerLib" and that didn’t work. Then seeing Suzume’s example, I tried variations of Shane's tags of to no avail. Then after almost giving up I tried the code I posted above which did work. The link you gave me has no example of actually using a script library in this way. None.

From memory, it was written before dictionaries for script libraries were a thing.

But there’s always the FileManagerLib Read Me file…

That’s true! I guess I should have read that better.

I guess the right answer would have been “the FileManagerLib Read me file” instead of a link to an old document that was written before script libraires were a thing :wink:

Another right answer would be ScriptDebugger Help menu. Open SD help and search for “Library Script.”

2 Likes