-2

Here's what I got, but I ave a custom label with "Active Projects". How do I assign?

tell application "Finder" to set label index of theFile to 5

  • 1
    Possible duplicate of [Setting a label to a file with AppleScript](https://stackoverflow.com/questions/6250691/setting-a-label-to-a-file-with-applescript) – GalAbra Jan 31 '18 at 16:27
  • What version of OS X/macOs are you using and is a _tag_ or _label_? – user3439894 Jan 31 '18 at 16:35

1 Answers1

3

You can't do that with vanilla AppleScript. The Finder dictionary does not support adding tags.

However you can do it with AppleScriptObjC which gives access to the Foundation framework

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"

on addTagToPath(theTag, thePath)
    set theURL to current application's NSURL's fileURLWithPath:thePath
    set {success, tagArray, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theError is not missing value then error theError's localizedDescription() as text
    if tagArray is not missing value and (tagArray's containsObject:theTag) as boolean is true then return
    if tagArray is missing value then set tagArray to current application's NSMutableArray's array()
    tagArray's addObject:theTag
    set {success, theError} to theURL's setResourceValue:tagArray forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theError is not missing value then error theError's localizedDescription() as text
end addTagToPath

and use it

try
    addTagToPath("MyTag", "/Users/myUser/path/to/file.ext")
on error e
    log e
end try

The try block catches errors thrown by the NSURL methods

vadian
  • 274,689
  • 30
  • 353
  • 361