• 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: Setting a coloured label
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting a coloured label


  • Subject: Re: Setting a coloured label
  • From: Shane Stanley <email@hidden>
  • Date: Thu, 15 Sep 2016 10:30:46 +1000


On 15 Sep 2016, at 5:37 AM, Yvan KOENIG <email@hidden> wrote:

Here is the final code :

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

on setTags:tagList forItem:fileOrPosixPath
if class of fileOrPosixPath is not text then set fileOrPosixPath to POSIX path of fileOrPosixPath
set thisURL to current application's class "NSURL"'s fileURLWithPath:fileOrPosixPath -- make URL
set {theResult, theError} to thisURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then error (theError's |localizedDescription|() as text)
end setTags:forItem:

set aFolder to (choose folder)

# Clear the existing tags
my setTags:{} forItem:(POSIX path of aFolder)
# Set the label to red
tell application "Finder"
set label index of aFolder to 2
end tell

I'm not sure that's ideal...

Let's go back to your earlier post:

My first attempt was to use set label index to 2

but, if the folder has already a label, the old one is not replaced, a new one is just added.

I tried to remove the existing label using set label index to 0 but it didn't do the trick.

In fact, if a file has a label index, and you set a new one via Finder scripting, it *does* replace the label. Try it: choose an unlabeled file, set its label index to one value via script, then set it to another via script. What you saw was the fact that setting the label index does not change a file's *tags*, and I suspect the color you were trying to replace was set manually in the Finder, and hence was set as a tag.

Tags were introduced as a replacement for label indexes, but the two co-exist. So if you set a file to have a tag of Orange, because it's one of the eight standard tags, the file's label index is also changed to that of the Orange color -- but only if it's existing label index is 0. Subsequent tags don't change a file's label index. If you remove a tag, if there are no tags left the label index is changed to 0, otherwise it is changed to the equivalent of the next tag.

Working the other way, when you set a label index, tags are unaffected. So if you change the label index via AppleScript, what you are doing is different from what you are doing in the interface.

Presumably this is all about backwards compatibility: you can add a tag to a file, and it will appear as a label under an older system. But because tags are a replacement for label indexes, it's possible that label indexes will one day disappear. So if you're planning to color things for the long haul, I think your original code is better than the code above.

The fact that the Finder doesn't support tags is yet another black mark against it. Feel free to log bug reports (System Events and the file dialogs should support them too).

If you want to see the metadata for a file, you can use this:

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

set theFile to POSIX path of (choose file)
set theFile to current application's NSString's stringWithString:theFile
set theName to theFile's lastPathComponent()
set theContainer to theFile's stringByDeletingLastPathComponent()
set thePred to current application's NSPredicate's predicateWithFormat_("kMDItemFSName == %@", theName)
set theQuery to current application's NSMetadataQuery's new()
theQuery's setPredicate:thePred
theQuery's setSearchScopes:{theContainer}
theQuery's startQuery()
repeat while theQuery's isGathering() as boolean
delay 0.1
end repeat
theQuery's stopQuery()
set metaItem to theQuery's resultAtIndex:0
set theAttrs to metaItem's attributes()
return (metaItem's valuesForAttributes:theAttrs) as record


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
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: Setting a coloured label
      • From: Shane Stanley <email@hidden>
References: 
 >Setting a coloured label (From: Yvan KOENIG <email@hidden>)
 >Re: Setting a coloured label (From: Helmut Fuchs <email@hidden>)
 >Re: Setting a coloured label (From: Yvan KOENIG <email@hidden>)

  • Prev by Date: Re: Setting a coloured label
  • Next by Date: Re: Setting a coloured label
  • Previous by thread: Re: Setting a coloured label
  • Next by thread: Re: Setting a coloured label
  • Index(es):
    • Date
    • Thread