• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: tags manipulation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tags manipulation


  • Subject: Re: tags manipulation
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 28 Sep 2015 13:58:13 +1000

On 28 Sep 2015, at 1:41 PM, 2551 <email@hidden> wrote:

As an addendum, there’s a WWDC 2013 (sorry don’t have the reference to hand) that demos code for finding out the tag(s) associated with a particular file (and for programmatically setting tags), though I’m afraid Shane or someone else will have to do the translation to ASObjC. 

I posted ASObjC versions a while back, but at the risk of repetition:

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 tags
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 ≠ 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 don’t know but I’m assuming NSURLTagNamesKey will not return labels.

It actually does take labels into account -- so a Finder label of  6 returns a tag of "Red". It's only Spotlight-based searches that skip them.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: tags manipulation
      • From: 2551 <email@hidden>
References: 
 >tags manipulation (From: Jean-Christophe Helary <email@hidden>)
 >Re: tags manipulation (From: Shane Stanley <email@hidden>)
 >Re: tags manipulation (From: Christopher Stone <email@hidden>)
 >Re: tags manipulation (From: Shane Stanley <email@hidden>)
 >Re: tags manipulation (From: Christopher Stone <email@hidden>)
 >Re: tags manipulation (From: Shane Stanley <email@hidden>)
 >Re: tags manipulation (From: Shane Stanley <email@hidden>)
 >Re: tags manipulation (From: Shane Stanley <email@hidden>)
 >Re: tags manipulation (From: 2551 <email@hidden>)

  • Prev by Date: Re: tags manipulation
  • Next by Date: Re: tags manipulation
  • Previous by thread: Re: tags manipulation
  • Next by thread: Re: tags manipulation
  • Index(es):
    • Date
    • Thread