Wednesday, January 17, 2018

#1 2016-09-23 04:03:37 pm

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

Adding Finder tags

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:

Applescript:

#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.

Last edited by Joy (2016-09-23 04:12:51 pm)

Offline

 

#2 2016-09-23 06:15:38 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5230

Re: Adding Finder tags

Here are handlers to read, add and replace tags:

Applescript:

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:

Last edited by Shane Stanley (2018-01-07 05:36:30 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2018-01-06 09:08:34 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

Re: Adding Finder tags

Till yesterday I used just returnTagsfor
but today I needed to add tags. Both handlers adding or replacing tags don't work. At least on 10.11

Offline

 

#4 2018-01-06 09:08:59 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5230

Re: Adding Finder tags

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?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2018-01-07 04:01:36 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4488

Re: Adding Finder tags

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.

Applescript:

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.

Last edited by Nigel Garvey (2018-01-07 04:08:24 am)


NG

Offline

 

#6 2018-01-07 04:04:38 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 547
Website

Re: Adding Finder tags

One of the items in my list was a number variable for handler setTags:tagList. It's working now

Still, handler addTags:tagList refuses to work.

Edit.
addTags:tagList works now. And no,  I needn't to wait for the tags to appear:it's instantaneous.
Thanks also Nigel!

Last edited by Joy (2018-01-07 04:09:55 am)

Offline

 

#7 2018-01-07 05:12:03 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4488

Re: Adding Finder tags

I wrote:

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:

Applescript:

set tagList to (current application's NSArray's arrayWithArray:tagList)'s valueForKey:"capitalizedString"

Last edited by Nigel Garvey (2018-01-07 05:46:24 am)


NG

Offline

 

#8 2018-01-07 05:51:12 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5230

Re: Adding Finder tags

Nigel Garvey wrote:

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 '≠'.



Thanks -- I didn't notice that. I've edited it now.

a list of strings denoting the tag colours



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:

Applescript:

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

Adding a Finder 'update' command seems to speed things up.



Just be aware that it also strips custom icons in 10.13.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)