Need a do shell script command to set the label index.

Hi,
This is just additional information on the topic. I was doing some more reading and searching on osascript, and I came across this script by James Nierodzik in #3 of http://macscripter.net/viewtopic.php?id=25499
After a bit of tinkering, I came up with this osascript command:

set osaCode to "'tell application \"Finder\" to set label index of item \"" & aNameTarget & "\" to \"" & LabelIndexSource & "\"'"

and this is the one script solution to my initial question:

set sourceFolder to (path to documents folder as text) & "Job Templates:Label Templates:"
set targetDefLoc to (path to documents folder as text) & "Jobs:"
set targetFolder to choose folder default location alias targetDefLoc with prompt "Choose the TARGET Job folder to set the labels."
tell application "Finder"
	set sourceFileNames to name of every item of folder sourceFolder
	set targetFileNames to name of every item of folder targetFolder
	repeat with aName in sourceFileNames
		set aNameSource to alias (sourceFolder & aName)
		set LabelIndexSource to label index of aNameSource
		if aName is in targetFileNames then
			set aNameTarget to alias ((targetFolder as string) & aName)
			set osaCode to "'tell application \"Finder\" to set label index of item \"" & aNameTarget & "\" to \"" & LabelIndexSource & "\"'"
			tell current application
				do shell script "osascript -e " & osaCode with administrator privileges
			end tell
		end if
	end repeat
end tell

This, I think, is a neat and easy solution for using osascript without the need for a shell script.
Obviously, an osascript with a shell script is still the solution for more complicated scripts. McUsrII, thanks for pushing me to learn about it :slight_smile:
Cheers, Chris

You could also set the label of a file using xattr shell command in case. If you’re curious here’s an example to set the label to red with administrator privileges:

set theFile to quoted form of POSIX path of ((path to desktop folder as text) & "test")

set theData to every word of (do shell script "xattr -p com.apple.FinderInfo " & theFile & " || echo ")
if theData = {} then --we have no xattr
	repeat 32 times
		set end of theData to "00"
	end repeat
end if
set item 10 of theData to "0C" --0C is red
set AppleScript's text item delimiters to space
set theData to theData as string
set AppleScript's text item delimiters to ""
set theData to quoted form of theData
do shell script "xattr -xw com.apple.FinderInfo " & theData & space & theFile with administrator privileges

That is just brilliant. :slight_smile:

Seems like I should try the apropos command more often, or man -k, to see if there is an existing tool that can do it for me.

Now I only have to convert the colors to the value for xattr.