I tried to change the Finder label of some files in a folder from green to yellow, but it seems I can only add yellow (not delete the green label first and then apply yellow instead).
This is what I was trying with macOS 10.14.6:
tell application "Finder"
activate
set theFolder to "Macintosh HD:Users:nev:Desktop:source"
set theFiles to every file of (folder theFolder)
repeat with theFile in theFiles as alias list
-- red is 2
-- orange is 1
-- yellow is 3
-- green is 6
-- purple is 5
-- grey is 7
-- none is 0
if label index of theFile is 6 then
set label index of theFile to 3
end if
end repeat
open theFolder
end tell
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
property |NSURL| : a reference to current application's NSURL
-- Fetch tags; pass a POSIX path
on returnTagsFor:POSIXPathInput
set thisURL to |NSURL|'s fileURLWithPath:POSIXPathInput
set {theResult, theTags, theError} to thisURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theResult as boolean is false then error (theError's |localizedDescription|() as text)
if theTags = missing value then return {} -- because when there are none, it returns missing value
return theTags as list
end returnTagsFor:
-- Replace tags; pass a list of the new tags plus a POSIX path
on setTags:tagList forItem:POSIXPathInput
set thisURL to |NSURL|'s fileURLWithPath:POSIXPathInput
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 p2d to path to desktop as text
tell application "Finder"
activate
set theFolder to p2d & "source"
set theFiles to (every file of (folder theFolder)) as alias list
repeat with theFile in theFiles
-- red is 2 - rouge
-- orange is 1 - orange
-- yellow is 3 - jaune
-- green is 6 - vert
-- purple is 5 - violet
-- grey is 7 - gris
-- none is 0 - aucun
--if label index of theFile is 6 then
set POSIXPath to POSIX path of theFile
if (my returnTagsFor:POSIXPath) contains "vert" then # replace "vert" by "green"
(my setTags:{"jaune"} forItem:POSIXPath) # replace "jaune" by "yellow"
end if
end repeat
open theFolder
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 6 septembre 2019 16:42:11
Some OS versions ago Apple introduced the ability to add multiple labels (called tags) to the Finder items but hasn’t implemented the functionality in the dictionary of the Finder.
This is a method to replace tags using the API of Foundation’s NSURL where the tags are represented by a string array.
You might use the localized tag strings
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
property NSNotFound : a reference to 9.22337203685477E+18 + 5807 -- weird but working AppleScript representation of the NSNotFound constant
tell application "Finder"
activate
set theFiles to every file of folder "source" as alias list -- the desktop folder is the "root" folder of the Finder
repeat with theFile in theFiles
try
(my replaceTag:"Green" withTag:"Yellow" atPath:(POSIX path of theFile))
on error e
display dialog e & " - " & POSIX path of theFile
end try
end repeat
open theFolder
end tell
on replaceTag:sourceTag withTag:destinationTag atPath: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 then
set tagIndex to tagArray's indexOfObject:sourceTag
if tagIndex is NSNotFound then return
set mutableTagArray to current application's NSMutableArray's arrayWithArray:tagArray
mutableTagArray's replaceObjectAtIndex:tagIndex withObject:destinationTag
set {success, theError} to theURL's setResourceValue:mutableTagArray forKey:(current application's NSURLTagNamesKey) |error|:(reference)
if theError is not missing value then error theError's localizedDescription() as text
end if
end replaceTag:withTag:atPath:
Your mistake is making theFiles as alias list. The alias has not access to property label index. So, try this:
tell application "Finder"
activate
set theFolder to choose folder
set theFiles to every file of theFolder
repeat with theFile in theFiles
if label index of theFile is 6 then set label index of theFile to 3
end repeat
open theFolder
end tell
No, these are multiple tags attached to your files from your previous attempts. Right-click on the file, click on “Tags …” and clean the multiple tags from previous attempts. For example, select the tag “Green” and if it has the name “Green” “Yellow”, remove “Yellow” so that the tag becomes only “Green”. After that, you will see that the script is working correctly.