Change file label color based on date stamp

Hello,

I’m trying to write a script that will check the contents of a folder and all of its subfolders. On dropping a new file into the folder, I want it to check the date and if it’s older than a certain time then change the file label to a certain color. Here’s the script I have so far but I haven’t been able to get it to work. Any help or suggestions would be appreciated. Thanks!

on adding folder items to this_folder after receiving these_items
	set date_stamp to (current date) - (1 * minutes)
	tell application "Finder"
		set newFiles to every file of entire contents of thisFolder whose modification date is less than date_stamp
		-- now you have a list of all new files, so check their types:
		repeat with eachFile in newFiles
			if file type of newFiles is in {"pdf"} then
				set label name of newFiles to 6
			else
				set label name of newFiles to 0
			end if
		end repeat
	end tell
end adding folder items to

Try this (note the changes):
I didn’t test it.

on adding folder items to this_folder after receiving these_items
	set date_stamp to (current date) - (1 * minutes)
	tell application "Finder"
		try
			set newFiles to every file of entire contents of thisFolder whose modification date is less than date_stamp as alias list
		on error
			set newFiles to every file of entire contents of thisFolder whose modification date is less than date_stamp as alias as list
		end try
		-- now you have a list of all new files, so check their types:
		repeat with eachFile in newFiles
			if file type of eachFile is in {"pdf"} then
				set label index of eachFile to 6
			else
				set label index of eachFile to 0
			end if
		end repeat
	end tell
end adding folder items to

Hi Durango

Its looking like Adam beat me to it!!
But heres my attempt anyway!!

set date_stamp to (current date) - (1 * minutes)
tell application "Finder"
	set newFiles to every file of entire contents of (choose folder) whose file type is "PDF "
	repeat with eachFile in newFiles
		set t to modification date of eachFile
		tell eachFile
			if t is less than date_stamp then
				set label index to 6
			else
				set label index to 0
			end if
		end tell
	end repeat
end tell

I’d go with Adams any day of the week though!!

Apologies for it not being in a folder action form!

Oops, more knowledgeable people with speedier typing fingers already answered, but…

I noticed you have “this_folder” then “thisFolder”, and “set label name of newFiles to” instead of “set label index of eachFile to”. I also have an easier time with “name extension” than with “file type”, so try this (worked when I tested it):

on adding folder items to this_folder after receiving these_items
	set date_stamp to (current date) - (1 * minutes)
	tell application "Finder"
		--set this_folder to (choose folder)
		set newFiles to every file of entire contents of this_folder whose modification date is less than date_stamp
		-- now you have a list of all new files, so check their types:
		repeat with eachFile in newFiles
			if name extension of eachFile contains "pdf" then
				set label index of eachFile to 6
			else
				set label index of eachFile to 0
			end if
		end repeat
	end tell
end adding folder items to

Thank you all soooooo much! I modified the script a little but see now what I was doing wrong. Here’s what I ended up with. Basically I am using this script to mark files that are past due for our editors. Anything in green will be a caution and anything in red will be over due. Editors will be able to open the folder and view immediately what is past due.

Thanks again to everyone who made a suggestion!

on adding folder items to this_folder after receiving these_items
	set date_stamp to (current date)
	tell application "Finder"
		--set this_folder to (choose folder)
		set newFiles to every file of entire contents of this_folder whose modification date is less than date_stamp
		-- now you have a list of all new files, so check their types:
		repeat with eachFile in newFiles
			set t to modification date of eachFile
			tell eachFile
				if t is less than date_stamp - (36 * hours) then
					set label index of eachFile to 1
				else if t is less than date_stamp - (24 * hours) then
					set label index of eachFile to 6
				else
					set label index of eachFile to 0
				end if
			end tell
		end repeat
	end tell
end adding folder items to

So maybe I got ahead of myself.

This script runs flawlessly as a folder action on my desktop. However, when I try to assign the folder action over our network the script will not run? Anyone have any thoughts as to why this is?

Thanks!

Tell us more than that. Where is the FA? Where is the Folder?

Sorry.

The folder action script resides on my machine in my “Folder Action Scripts” folder. The folder action needs to be applied to a folder on a mounted volume on my desktop.

If I apply the folder action to a folder on my desktop and run the script…I have no problems. If I apply the same folder action to a folder on the mounted volume…it doesn’t work. I’ve tried applying other folder actions (generic ones) to the same folder on the volume and they run without an issue.

Any thoughts are appreciated. Thanks!

As odd as it may sound, check the permissions of the FA. I have had some oddness which if I remember right, was related to the group that the script belonged, was different to the bundled scripts.

That was it! Checking the permission…I noticed that it was wrong. Changed it and the script runs fine. Thanks everyone and Mark! I would have never thought to check that.

Is there any way to modify the first line so that the folder action applies to the addition of contents to the ENTIRE CONTENTS of the folder?

Thanks!