Anyway to change folder color without osascript?

I have a script that creates an output folder from shell script with the day’s date as part of its name and while I can redirect easily to it with shell-scripting, I’d like to have it set to be a unique color, but the only directions online seem to be using an osascript, but passing the date-string that is part of the folder’s name is proving really difficult. Can I use the kMDItemFSLabel property to directly change the color in shell script itself?

This is what I have so far that isn’t working - keep getting “41:42: syntax error: Expected expression but found unknown token. (-2741)”:


set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
set theFolder to "~/Desktop/TheFolder\\ " & dateCurrent & "/"
do shell script "osascript -e 'tell app \"Finder\" to set label index of  '" & theFolder & "' to 3'"

This is the string I am outputing to from within the shell-script that works great:


~/Desktop/TheFolder\\ " & dateCurrent & "/

OK - Figured it out without Shell Script:


set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
set outputFolder to (((path to desktop from user domain) & "TheFolder " & dateCurrent) as string) as alias
tell application "Finder" to set label index of outputFolder to 3

OMG I HATE all the Coaxing nonsense Finder demands. WHENEVER possible - shell script!!!

In this case you did use the shell wrong and made a newbie mistake ;).

To answer the question to other readers artphotodude didn’t quote the path for bash which bash requires to do and the fault has nothing to do with AS and certainly nothing to do with the Finder. In addition the path needed an additional AS escaping because the value from bash will be unescaped before it is send as an argument to the osascript shell command.

AppleScript is quirky, flawed and inconsistent compared to Bash but in this case Bash demands the escaping and the long code.

Here is how it should work if someone is determined to make it run in shell:

set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
set theFolder to (path to desktop from user domain as string) & " TheFolder " & dateCurrent

set theScript to "tell app \"Finder\" to set label index of alias \"" & escapeASString(theFolder) & "\" to 3"
do shell script "osascript -e " & quoted form of theScript

on escapeASString(str)
	set str to stringReplace(str, "\\", "\\\\")
	set str to stringReplace(str, "\"", "\\\"")
	return str
end escapeASString

on stringReplace(haystack, needle, replace)
	tell AppleScript
		set oldTIDs to text item delimiters
		set text item delimiters to needle
		set components to text items of haystack
		set text item delimiters to replace
		set newHaystack to components as string
		set text item delimiters to oldTIDs
	end tell
	return newHaystack
end stringReplace

or you can use the command line arguments and use them inside the run handler which avoids required AS escaping of the folder name:


set dateCurrent to ((do shell script "date '+%m-%d-%Y'") as text)
set theFolder to (path to desktop from user domain as string) & " TheFolder " & dateCurrent

set theScript to "on run argv
tell app \"Finder\" to set label index of alias (item 1 of argv) to 3
end run"
do shell script "osascript -e " & quoted form of theScript & space & quoted form of theFolder

I see what you mean.

But as you can see osascript is more trouble than it is worth much of the time (twice as many lines as not using it). Also, many scripts can not be put into single-line format and then the complexity gets exponential.

In 99% of situations where Finder/System Events are totally stupid, there is a shell-script way of doing something easily, and there really should just be a way to directly set the kMDItemFSLabel from shell - sadly in the case of things Finder directly controls this is usually not possible. This is likely one of the last holdovers from the old MacOS alchemy. :rolleyes:

In this case it shouldn’t. The metadata database is maintained using importers which are fired when new events come by. Therefore all it’s data in this database is read-only and changing its value wouldn’t affect the file because there is no relation/binding to the actual value. It’s also better for security so that broken or malicious importers cannot mess up your files, only the database.

This is even better “ and much faster - than shell scripting


use framework "Foundation"
set dateFormatter to current application's NSDateFormatter's alloc()'s init()
set dateFormatter's dateFormat to "MM-dd-yyyy"
set dateCurrent to dateFormatter's stringFromDate:(current application's NSDate's |date|())
set theFolder to POSIX path of (path to desktop) & "TheFolder" & dateCurrent as text
set fileURL to current application's NSURL's fileURLWithPath:theFolder
fileURL's setResourceValue:3 forKey:(current application's NSURLLabelNumberKey) |error|:(missing value)