ok, Finder tags are a cool thing. Nice replacement for comments, really.
The problem is, i like to use folder actions and want to automate this tagging process on files
Currently i know how to get tags:
#get tags from a file, folder
tell application "Finder" to set trg to item 1 of (get the selection)
set the_tags to paragraphs of (do shell script "mdls -raw -name kMDItemUserTags " & quoted form of POSIX path of (trg as text))
adding tags shouldnt be the big problem i think. Just some damn workaround with xattr. Couldnt find anything relevant.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
on returnTagsFor:posixPath -- get the tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
on setTags:tagList forPath:posixPath -- set the tags, replacing any existing
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags:forPath:
on addTags:tagList forPath:posixPath -- add to existing tags
set aURL to current application's |NSURL|'s fileURLWithPath:posixPath -- make URL
-- get existing tags
set {theResult, theTags} to aURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
if theTags is not missing value then -- add new tags
set tagList to (theTags as list) & tagList
set tagList to (current application's NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() -- delete any duplicates
end if
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end addTags:forPath:
Iām surprised to hear that. I canāt check under 10.11 here, however those handlers were all written on a system running 10.11. Are you passing a list of tags in both cases?
They all work on my own 10.11 system. But this siteās software has been updated since Shane posted them, so of course the 'Ć¢ā° ā in the last handler has to be changed back to āā ā.
The second and third handlers both take two parameters: a list of strings denoting the tag colours and a POSIX path to the target item. On my system, the strings are in English. I donāt know what the situation is on systems localised for different languages. With setTags:forPath:, passing an empty tag list will remove all tags from the item.
Since all the handlers use āinterleaved parameterā syntax, calls to them have be preceded by a reference to the script containing them (such as āmyā or āitsā).
My 10.11 system is comparatively slow, so it sometimes takes a few seconds for the tags to appear on screen. Adding a Finder āupdateā command seems to speed things up.
set theItem to (choose file)
my setTags:{"blue", "red"} forPath:(POSIX path of theItem)
tell application "Finder" to update theItem
One thing I have found is that tags set with these handlers can only be removed with them. The āTagsā¦ā item at the bottom of the Finderās āFileā menu doesnāt work to remove them.
This turns out to be because I was entering the tag names as lower-case strings: {āblueā, āredā}. These work in the scripts to set or delete tags with the required colours. But the Finderās āFileā menu sets tags with capitalised initial letters ā {āBlueā, āRedā} ā and itās case sensitive when matching tags to remove. Shaneās handlers appear to be case-insensitive, but itās largely an effect of the way they work and the fact that the tag resource system produces appropriate tags for both, say, āBlueā and āblueā. If both are set, you only see one blue tag on the screen, but there are two in the resource and Shaneās returnTagsFor: handler will return both.
A standard format could be imposed by inserting this line at the tops of the setTags:forPath: and addTags:forPath handlers:
set tagList to (current application's NSArray's arrayWithArray:tagList)'s valueForKey:"capitalizedString"
Thanks ā I didnāt notice that. Iāve edited it now.
The tags donāt have to be colours ā they can be any strings you like. The default ones are defined to be sort of backwards compatible with the old Finder labels. You can get those names like this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
current application's NSWorkspace's sharedWorkspace()'s fileLabels() as list
Just be aware that it also strips custom icons in 10.13.