thomasbest

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

Close

Q: Setting Finder Tags in Mavericks with AppleScript?

  • All replies
  • Helpful answers

  • by red_menace,

    red_menace red_menace Nov 19, 2013 2:02 PM in response to thomasbest
    Level 6 (15,519 points)
    Desktops
    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.

  • by thomasbest,

    thomasbest thomasbest Nov 19, 2013 11:25 PM in response to red_menace
    Level 1 (0 points)
    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?

  • by red_menace,

    red_menace red_menace Nov 19, 2013 11:50 PM in response to thomasbest
    Level 6 (15,519 points)
    Desktops
    Nov 19, 2013 11:50 PM in response to thomasbest

    It works for me in Mavericks, although it only sets the label (such as they are) - to set a tag programmatically you will need to use xattr.

  • by julesforum,

    julesforum julesforum Jun 16, 2014 7:48 AM in response to thomasbest
    Level 1 (0 points)
    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

  • by L. Mariachi,

    L. Mariachi L. Mariachi Mar 17, 2015 1:23 PM in response to julesforum
    Level 1 (0 points)
    Mar 17, 2015 1:23 PM in response to julesforum

    Thank you! I had no idea that utility existed.

  • by VikingOSX,

    VikingOSX VikingOSX Mar 17, 2015 2:58 PM in response to thomasbest
    Level 7 (20,544 points)
    Mac OS X
    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

  • by T2YUKI,

    T2YUKI T2YUKI Apr 12, 2016 3:34 AM in response to thomasbest
    Level 1 (92 points)
    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's class "NSURL"'s fileURLWithPath:POSIXPath

      set {theResult, theTags, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)

      if theResult as boolean is false then

      error (theError's localizedDescription() 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:aList toTheFileAtPath:POSIXPath

      set newTagList to (its tagsOfFileAtPath:POSIXPath) & aList

      set newTag to current application's NSArray's arrayWithArray:newTagList

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

      set {theResult, theError} to theURL's setResourceValue:newTag forKey:(current application's NSURLTagNamesKey) |error|:(reference)

      if theResult as boolean is false then

      error (theError's localizedDescription() 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