File label

Hi all,

I know that setting and reading the file labels doesn’t work very good.
Yet every file (theoretically) can have a label index which should be equivalent to a label text (as set in the Finder Preference).

Is there anyway to extract the label text (for example “Urgent”) if I have the label index (for example: 1) and viceversa?

Something like:

tell application "Finder"
  set myText to "AnyText"
  set myText to the text of label 1
end tell

The reason why I am asking this is because I am using the FileTagsLib v1.0.0 (thanks Shane!!!) and it seems that I can only use the “label text” to set a tag.

Thanks.

Hi.

Is this the kind of thing you mean?

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

tell application "Finder" to set itemAlias to item 1 of (selection as alias list)

set itemURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of itemAlias)
set tagNames to item 2 of (itemURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value))
if (tagNames is not missing value) then set tagNames to tagNames as list

return tagNames

Edit: Script altered to use NSURLTagNamesKey instead and to return any tag names found as a list!

Thanks for your hep.
But I what I with this script is the color as text (such as “Red”, “Orange” etc…) and not the name I gave it to that tag.
It is strange the something so simple is not doable with Applescript and maybe even with ObjC…

Labels and tags are not the same thing. Finder labels have been replaced by tags, but the old labels are also treated as tags for backwards compatibility, and to an extent vice-versa. But there is no such thing as a tag index.

Put another way, the old labels had an index because they were actually stored as an index. Tags don’t have an index because they are stored as a list of strings, which can be in any order.

Thanks for clarifying this.
L.

OK. It does return the colour names for me, but I’ve never looked into changing them!

The resource key I used only returns a single string, even when an item has more than one tag. I’ve now modified the script to use NSURLTagNamesKey instead, which returns an array of strings (or ‘missing value’). This may not help you, though.

This actually works much better for what I need.
Thanks !