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

Setting a coloured label


  • Subject: Setting a coloured label
  • From: Yvan KOENIG <email@hidden>
  • Date: Wed, 14 Sep 2016 16:09:56 +0200

Hello
I wish to set the label of a folder to red.

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.

set wantedLabel to 2

set aFolder to choose folder

tell application "Finder"
if label index of aFolder is not wantedLabel then
log "point 1"
if label index of aFolder is not 0 then
log "point 2"
set label index of aFolder to 0
--open aFolder
repeat 20 times
tell me to delay 0.1
update aFolder
log "point 3"
if label index of aFolder is 0 then exit repeat
log "point 4"
end repeat
--close window 1
end if
log "point 5"
set label index of aFolder to wantedLabel
end if
end tell

As the folder was originally labelled in blue, the result was two labels : blue and red.

So, i decided to use ASObjC using a code written by Shane Stanley.

I just added a complementary handler returning the localized names of the colors because it's what me must pass to the code.

The draft code became :

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:

on getLabelsNames()
return (current application's NSWorkspace's sharedWorkspace()'s fileLabels()) as list
--> {"Aucun", "Gris", "Vert", "Violet", "Bleu", "Jaune", "Rouge", "Orange"}
end getLabelsNames

set target to POSIX path of (choose folder)

my setTags:{item -2 of my getLabelsNames()} forItem:target


Is there a way to set the colored label without having to extract the list of localized names ?


Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) mercredi 14 septembre 2016 16:09:37



 _______________________________________________
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: Helmut Fuchs <email@hidden>
  • Prev by Date: Re: How to activate script embedded in Application Folder in Applications?
  • Next by Date: Re: Setting a coloured label
  • Previous by thread: Re: How to activate script embedded in Application Folder in Applications?
  • Next by thread: Re: Setting a coloured label
  • Index(es):
    • Date
    • Thread