You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

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?

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

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

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.