Setting Finder Tags in Mavericks with AppleScript?

I used to set Finder labels (before OS X 10.9) via AppleScript with the command "set label index of x to 4" (x being the file name, 4 the color of the label).

Now finder labels are gone in Mavericks and replaced by tags. How can I replicate the same behavior in OS X 10.9?


Thanks for your help!

MacBook Air, OS X Mavericks (10.9)

Posted on Nov 19, 2013 10:45 AM

Reply
7 replies
Sort By: 

Nov 19, 2013 2:02 PM in response to thomasbest

The first label works the same (to keep backwards compatibility), but tags are implemented as an extended file attribute. Ars Technica has a decent writeup on the implementation in their Mavericks Review article, but basically you are looking at using the xattr and plutil shell utilities to add or modify a property list attribute.

Reply

Nov 19, 2013 11:25 PM in response to red_menace

Thanks for your answer. Unfortunately, it doesn't work for me. The exact same code worked before mavericks, but doesn't under 10.9.


tell application "Finder"

set name of myfile to (myname & mytext & \".\" & suffix) as string

set label index of myfile to 2 --red label

end tell


LIne 2 works (The file "myfile" is renamed)

Line 3 doesn't work under 10.9 (no tag is set to the renamed file)


Any ideas?

Reply

Jun 16, 2014 7:48 AM in response to thomasbest

There's a great litte 3rd party command line tool called 'tag' which makes querying and manipulating tags very easy. I've used it to create an Automator Folder Action that tags files automatically when dropped into the folder.


https://trac.macports.org/browser/trunk/dports/sysutils/tag/Portfile


An easy way to download and install it is through Macports. If you have Macports already installed then run the following command in terminal:


sudo port install tag


To add the tag 'footag' to a file or directory:

tag -a "footag" /path/to/filename


List the tags for a file,etc

tag -l /path/to/filename


Further help:

tag -h

Reply

Mar 17, 2015 2:58 PM in response to thomasbest

-- tag colors in Mavericks or later


property labelColor : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}


set myPF to POSIX path of ((path to desktop) as text) & "filename.ext"


tell application "Finder"

set label index of (POSIX file myPF as alias) to blue of labelColor

end tell

Reply

Apr 12, 2016 3:34 AM in response to thomasbest

Hi,


I'm reading Everyday AppleScriptObjC 3rd Edition by Shane Stanley and I've just found how to get tags of a file (in Chapter 14, 'One For the Files'). We can use the -getResourceValue:forKey:error: method of the NSURL class, like the following:


on tagsOfFileAtPath:POSIXPath

set theURL to current application'sclass "NSURL"'s fileURLWithPath:POSIXPath

set {theResult, theTags, theError} to theURL'sgetResourceValue:(reference) forKey:(current application'sNSURLTagNamesKey) |error|:(reference)

if theResult as boolean is false then

error (theError'slocalizedDescription() as text)

else

set theTags to theTags as list

if theTags = {missing value} then set theTags to {}

return theTags

end if

end tagsOfFileAtPath:

The NSURL class has also the -setResourceValue:forKey:error: method and we can use this method to add tags to a file, like the following:

on addTag:aListtoTheFileAtPath:POSIXPath

set newTagList to (its tagsOfFileAtPath:POSIXPath) & aList

set newTag to current application'sNSArray'sarrayWithArray:newTagList

set theURL to current application'sclass "NSURL"'s fileURLWithPath:POSIXPath

set {theResult, theError} to theURL'ssetResourceValue:newTagforKey:(current application'sNSURLTagNamesKey) |error|:(reference)

if theResult as boolean is false then

error (theError'slocalizedDescription() as text)

else

return (its tagsOfFileAtPath:POSIXPath)

end if

end addTag:toTheFileAtPath:

In case of Preview.app, we can use those scripts by the following statements:


use AppleScript version "2.4"

use framework "Foundation"


set thePOSIXPath to path of front document of application "Preview"


try

its addTag:{"Tag1", "Tag2"} toTheFileAtPath:thePOSIXPath

on error errMess


-- handle the error

end try

Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Setting Finder Tags in Mavericks with AppleScript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.