folder action to maintain a constant count of files?

hiya,

…I’m trying to get a folder action working, but I think my scripting skills have been damaged from prolonged C++ usage!

…I’m basically trying to have a folder that is fed images over a network, but I only want to keep the latest 200 or so images…so, I tried to make a folder action that, on “adding folder items”, sorts the items by date and moves the oldest item to the trash: so far I just get “Error: -1728. Can’t get every item of alias “internal:Users:tigital:Desktop:test:”.”…


property max_files : 10 -- set the number of files for screensaver

on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			--get the name of the folder
			set the folder_name to the name of this_folder
		end tell
		
		-- find out how many new items have been placed in the folder
		set the item_count to the number of items in this_folder
		if the item_count is greater than max_files then
			tell application "Finder"
				--move oldest file to the trash
				--sort this_folder by date and (move oldest to trash)
				--On my machine, the sort puts the newest file first. If yours puts it last, change item 1 to item -1
				set the oldest_file to the name of (info for (item 1 of (sort (get files of folder this_folder) by modification date)))
				move oldest_file to trash
			end tell
		end if
	on error the error_message number the error_number
		set the error_text to "Error: " & the error_number & ". " & the error_message
		-- the following line evokes the sub-routine to write the error into an error log created on the desktop
		-- if the file "Script Error Log.txt" already exists, it will add one line to the log
		my write_error_log(the error_text)
	end try
end adding folder items to

on write_error_log(this_error)
	set the error_log to ((path to desktop) as text) & "Script Error Log.txt"
	try
		open for access file the error_log with write permission
		write (this_error & return) to file the error_log starting at eof
		close access file the error_log
	on error
		try
			close access file the error_log
		end try
	end try
end write_error_log

…any ideas?

Hi.

This works alone, but when I ran the entire script, I got “Error: -1728. Can’t get item 1 of {}.” I didn’t look into that.


property max_files : 10

on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			
			set the folder_name to the name of this_folder
			
			set the item_count to count of (files of entire contents of this_folder)
			display dialog item_count as string --to make sure it worked after I took out the error log.
		end tell
	end try
end adding folder items to

EDIT: I worked on this some more, but Jacques has posted a better solution than I would have.

hey all,

…thanks so much for the ideas! In the end, Jacques’ was closest: it turns out that we need “item -1” for the oldest item, so I modified it as such:


property max_files : 10 -- set the number of files for screensaver

on adding folder items to this_folder after receiving added_items
	tell application "Finder" to tell folder this_folder
		set nbr to (number of files) - max_files
		if nbr > 0 then delete item -1 of (sort (get files) by modification date) --move oldest files to the trash
	end tell
end adding folder items to