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
|