A really simple questionss (and thus, embarrassing) re: label items

I am using Applescript to create a library of .txt documents from email received in Mail. The documents are placed into one of two folders based on subject matter (for our purposes, DOGS and CATS). A smart folder then displays only the .txt files (in both folders) that have been added today. It’s working well, but I have one tweak I’d like to add – the ability to label the .txt with a specific color (i.e. orange for DOGS and yellow for CATS). I am contemplating two approaches – the first being a folder action that applies the label when the .txt file is added; the second would set the label from the Applescripts I’m using (and which are invoked by Mail Act-On).

My problem is that I can’t figure out how to set the label index property of the .txt documents.

Here’s my folder action approach:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set label index of these_items to 3
	end tell
end adding folder items to

Here’s my applescript approach, commenting out the failed attempt (regulars to this forum will see their handiwork reflected – thanks):

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				set theText to content of thisMessage
				tell thisMessage
					set fileName to subject & "_" & (do shell script "date +%m/%d/%y/%T") & ".txt"
				end tell
				--this part removes colons from the file name so the path is correct
				set TID to AppleScript's text item delimiters
				set AppleScript's text item delimiters to ":"
				set newName to (text items of fileName)
				set AppleScript's text item delimiters to "_"
				set fileName to newName as string
				set AppleScript's text item delimiters to TID
				set theFile to "maccan2:PRLibrary:HFN:Hospitals:" & fileName
				set theFileID to open for access file theFile with write permission
				write theText to theFileID
				close access theFileID
				-- tell application "Finder" to set label index of theFile to 3
			end repeat
		end tell
	end perform mail action with messages
end using terms from

(I’m not a scripter, obviously, but I play one on tv. And I stayed at a Holiday Inn Express…)

At any rate, any tips or suggestions would be vastly appreciated.


Hi Jack,

the folder action script works only with a repeat block

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with this_item in these_items
			set label index of this_item to 3
		end repeat
	end tell
end adding folder items to

and the mail script works with this:

tell application "Finder" to set label index of (theFile as alias) to 3

Did the trick! Thanks so much!


That script works perfect!
How does one make it so that files greater than one size (>1000 kb) are one color and smaller (< 1000 kb) are another color?


making it so only certain file types are changed like .avi are not but .doc are.

Thanks in advance for any help from anyone!

Model: new MacBook
AppleScript: 2.1.1 (81)
Browser: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv: Gecko/20060911 Camino/1.0.3
Operating System: Mac OS X (10.4)

Which one? - you’ve asked this in another thread as well - see that one.

Hey Adam,

I had posted the first one and then found this one that works for changing the color as a folder action.
I have a system that files are sent to in our office that contain basically two types of files.
They are either:
a. contain the name “rest” and are less than 500 kb
b. contain the name “stress” and are greater than 501 kb

Master folder called “people”–> subfolders with peoples names that get sent over automatically → then the subfiles listed above get dropped into the persons folder

People folder:name folder: files a and b

I basically am trying to create a folder action so that when new files land in this folder then the subfolders are searched and the files are changed according to their size and/or name to rest being yellow and stress being green.

Sorry for the double post.
I did figure out how to create an automator string that does it I would like to have it as a single folder action instead.

thanks for your help!!!

Are the files in question “landing” in the folder by your own action, or is this folder shared with others? Folder Actions can be a little “iffy” when files are loaded in via a shared connection, particularly if dropped on in quantity.

As I see what you want to do:

  1. An outer folder (with the action) will receive files called “John Doe Rest.doc” or “John Doe Stress.doc” from others on your LAN.

  2. You want to sort these files into a folder somewhere called “John Doe” enclosing two more called “Rest” and “Stress” with Stresses marked with one color and Rests with another.

  3. Given that all stresses are in one folder and rests in another and that their names contain those words, It’s not clear why you want to color them too, but that seems to be what you want. Right? (Unless you want to remove the Rest and Stress bits from the name and rely on color only).

  4. How does size come into it? If all Stresses exceed 500, and all Rests are less, isn’t the name enough, or are you hoping that when folks forget to include the keyword, you’ll be able to tell by size?

Hey Adam,

Yes the files are landing in the folder by another action.
We have a folder called “people”.
When a study is completed the files comes across the network and dumps into this main folder.
We have software that takes the file and and creates a folder with the patient name inside the “people” folder automatically.
After the rest or stress scan is completed the image file comes over and because of the DICOM header info it knows where to place the rest or stress file into the correct persons folder.

So at the end of the day we have a main folder “people” with subfolders that have peoples names on them.
Inside each persons folder are a rest and stress file. When we go to process these files sometimes it gets confusing looking at the rest and stress files b/c they have the persons name imbedded before the term rest or stress.

Example: Bell_Adam,11/27/06,rest

I wanted to create a script that would change the subfiles as they hit the folder to change their color to make it easy for the person processing these files. I created the automator that does the correct action but it changes the processed images colors also which are usually over 2.5 megs each. This is why I wanted to change them by name and size requirements.

Here is my script that works using the automator application also:

on adding folder items to this_folder after receiving added_items
tell application “Macintosh HD:Users:DonnieOzenne:Library:Workflows:Applications:Folder Actions:ColorChange_Auto.app”
open added_items
end tell
end adding folder items to


I don’t have that workflow, but in AppleScript generally, you would do something like this

set f to alias ((path to desktop as text) & "Bell_Adam,,11/27/06,,stress.rtfd:")
--- I just used rtfd because it's easy to make a big file.
tell application "Finder"
	set p to properties of f
	set n to name of p
	set s to ((size of p) / 1024) -- KB
	set e to name extension of p
	if s > 1024 and e = "rtfd" and n contains "stress" then
		set label index of f to 6
		set label index of f to 3
	end if
end tell

Easy enough to set up an if selector with or’s and and’s in the conditions to filter them out.
do something
else if…then
do another thing
do a third thing
end if