• 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: Labels and tags ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Labels and tags ?


  • Subject: Re: Labels and tags ?
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 23 Jan 2015 22:45:47 +1100

On 23 Jan 2015, at 10:33 pm, Jean-Christophe Helary <email@hidden> wrote:

I'm looking for a way to do something similar to the above, but with tags, which does not seem to be trivial.

You can do it easily enough with AppleScriptObjC. Here's are handlers to retrieve tags, set tags, and add tags:

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 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:

If you save it them in a script library, you can also use them from Mavericks.


-- 
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: Labels and tags ?
      • From: Jean-Christophe Helary <email@hidden>
    • Re: Labels and tags ?
      • From: Jean-Christophe Helary <email@hidden>
References: 
 >Labels and tags ? (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Labels and tags ?
  • Next by Date: Re: Labels and tags ?
  • Previous by thread: Labels and tags ?
  • Next by thread: Re: Labels and tags ?
  • Index(es):
    • Date
    • Thread