Impossible to replace Finder file label in Mojave?

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

Is it indeed not possible on Mojave 10.14.6 ?

You may achieve your goal with :

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:

Stefan that is perfect - thank you so much for this :slight_smile:

Except … when the folder is somewhere else, e.g.

set theFolder to "Macintosh HD:Users:nev:Documents:applescripts:source"

I run into trouble… :frowning:

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

@KniazidisR - that adds the new label but doesn’t delete the old, like my code

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.

I like relative paths

set theFolder to alias ((path to documents folder as text) & "applescripts:source:")

I did that, with the same “additive” result.

That’s wrong. You have access to all Finder properties with an alias

And this is bad syntax. choose folder returns an alias. To avoid a double reference remove the keyword folder in the second line.

Stefan - changing the code to a regular applescript path and using the rest of your code worked, e.g.:

set theFolder to "Macintosh HD:Users:nev:Pictures:photos-in:here"

:slight_smile:

Hi:

I went thru this earlier this year. Here is a link to that discussion. My resulting script there, based on Shane’s code, is working fine.

 https://www.macscripter.net/viewtopic.php?id=46876