set the label of a hierarchy of files and folders

hello,

just messing around here and thought this might be useful. choose the folder, choose the color, and the script will label everything with the color label you’d like. use ‘blank’ to get rid of labels. NOTE: i’ve noticed that you need to get into the folder with the choose in order for the script to pick the right folder.

here is the code:

--setLabel will recursively run through a hierarchy of files and folders and set a label on each.  
--this code could be easily adapted to label only files of a certain type or to set some other file attribute.
--this could also be repurposed as a folder action if you wanted to label all files as you dropped them on a folder.

global theIndex
--theColors are in the order that they appear when you make a label.  changing the order will produce unknown results.
set theColors to {"blank", "orange", "red", "yellow", "blue", "purple", "green", "gray"}

set mainFolder to (choose folder)
set myColor to (choose from list theColors) as string
--this part finds out what theIndex (number) the label is.
set howMany to number of items in theColors
set a to 1
repeat while a ≤ howMany
	if (item a of theColors) is myColor then
		set theIndex to (a - 1)
		exit repeat
	end if
	set a to (a + 1)
end repeat

ProcessFolder(mainFolder)
--here's where all the real work gets done.
on ProcessFolder(theFolder)
	set fileList to {}
	set folderList to {}
	tell application "Finder"
		set fileList to (every file of theFolder)
		set folderList to (every folder of theFolder)
	end tell
	repeat with thisFile in fileList
		tell application "Finder"
			set label index of thisFile to theIndex
		end tell
	end repeat
	repeat with thisFolder in folderList
		tell application "Finder"
			set label index of thisFolder to theIndex
		end tell
		ProcessFolder(thisFolder)
	end repeat
end ProcessFolder

display dialog "Done!"

cheers.

Hi, waltr.

That’s a good exercise in recursive technique. :slight_smile: But in the handler, there’s no need to intialise fileList and folderList to {}. They’re set to that or to something else in the following lines anyway.

For the end purpose of giving the same label to every item in a hierarchy, using the Finder’s entire contents property would give a shorter script and a faster result:

--setLabel will bulk-set a label on each file and folder in a hierarchy (except for the root).
--this code could be easily adapted to label only files of a certain type or to set some other file attribute.
--this could also be repurposed as a folder action if you wanted to label all files as you dropped them on a folder.

--theColors are in the order that they appear when you make a label.  changing the order will produce unknown results.
set theColors to {"blank", "orange", "red", "yellow", "blue", "purple", "green", "gray"}

set mainFolder to (choose folder)

set myColor to (choose from list theColors)
if (myColor is false) then error number -128
set myColor to item 1 of myColor

--this part finds out what theIndex (number) the label is.
set a to 1
repeat until ((item a of theColors) is myColor)
	set a to (a + 1)
end repeat
set theIndex to a - 1

tell application "Finder" to set label index of every item of entire contents of mainFolder to theIndex
-- Or, in this case, simply:
-- tell application "Finder" to set label index of entire contents of mainFolder to theIndex


display dialog "Done!"

thanks NG,

that is Fast!. i realize my initialization is just a weird habit–not sure where i picked that one up. thanks for the pointers.

cheers.

thanks for the script. exactly what I needed.

fyi - the first script is much more useful when troubleshooting.

issue:
had many locked files

the first script was able to isolate the specific file while the second one which is considerably faster was not able to show which file had the issue.

Solution is to

script 1
add

set locked of thisFile to false

above

set label index of thisFile to theIndex

script 2
add

tell application "Finder" to set locked of every item of entire contents of mainFolder to false 

above

tell application "Finder" to set label index of every item of entire contents of mainFolder to theIndex

Script 2 Error

There is no locked files. Used sudo find . -flags uchg to remove all locked files but the following error occurs:

“error “Finder got an error: Network file permission error.” number -5000”

Using script 1 but it’s taking a lot of time. Conclusion is that there is something wrong with script 2.

Any help would be appreciated.