folder action for added files and folder size

I’ve modified the built-in “add - new item alert.scpt” so that when it detects new files within a folder, it will move them to an external volume. If denied enough times that the total folder size reaches >20GB, it will point that out in a dialog preceding the move. However, this is my first folder action, and it doesn’t seem to have any effect when i add files to the folder that i’ve attached this action to.

Help?



property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

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 the added_items
		
		--find out if the folder has reached its size limit
		set folder_size to (info for this_folder)
		if the folder_size is greater than 2.0E+7 then
			
			--create the alert string for folder limit
			set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
			if the item_count is greater than 1 then
				set alert_message to alert_message & "The new items added to " & «data utxt201C» & the folder_name & «data utxt201D» & " have increased the size of the folder to more than 20GB."
			else
				set alert_message to alert_message & "The new item added to " & «data utxt201C» & the folder_name & «data utxt201D» & " has increased the size of the folder to more than 20GB."
			end if
			set the alert_message to (the alert_message & return & return & "All items in this folder will be moved to your external drive.")
(*Here i intend to define the property added_items as all of the items in the folder, so that at the end of the script when Finder moves added_items, it would move everything in the folder.*)

			
			--create the alert string for any new file(s)
		else
			set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
			if the item_count is greater than 1 then
				set alert_message to alert_message & (the item_count as text) & " new items have been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "." & return & return & "These items will be moved to your external drive."
			else
				set alert_message to alert_message & "One new item has been placed in folder " & «data utxt201C» & the folder_name & «data utxt201D» & "." & return & return & "This item will be moved to your external drive."
			end if
		end if
		
		display dialog the alert_message buttons {"Cancel", "Ok"} default button 2 with icon 2 giving up after dialog_timeout
		set the user_choice to the button returned of the result
		
		if user_choice is "Ok" then
			tell application "Finder"
				move added_items to folder "/Volumes/Movies/iMovie Events"
			end tell
		end if
	end try
end adding folder items to

Operating System: Mac OS X (10.5)

Hi,

a try block in a folder action is useless unless you catch the error and log the message text.
Folder action scripts abort silently in case of an error anyway.

But you can debug folder action scripts by commenting out the first and last line, remove the try block
and add these lines at the beginning of the script

set this_folder to (choose folder)
set added_items to (choose file with multiple selections allowed)

The problem of the script is, info for returns a record of properties.
You have to specify the size property to get the size.

set folder_size to size of (info for this_folder)

Actually you can do this in the Finder tell block


.
tell application "Finder"
	--get the name of the folder
	set folder_name to name of this_folder
	set folder_size to size of this_folder
end tell
.

and delete the info for line

A second error is at the end. AppleScript works with HFS paths (colon separated)


.
move added_items to folder "Movies:iMovie Events:"
.

Thanks Stefan, that was a big help. I’ve made a number of changes but i’m still kind of stuck.

I’ve added some property settings at the top of the script so that I can debug better.

tell application "Finder"
	set home_path to home as text
end tell
set this_folder to (home_path & "Movies:iMovie Events")
set added_items to (home_path & "Desktop:test.mov")

I’ve also changed folder paths to HFS and added that move command that I hadn’t made yet.

	set the alert_message to (the alert_message & return & return & "All items in this folder will be moved to your external drive.")
		
		move every item of this_folder to "Volumes:External:iMovie Events"

if user_choice is "Ok" then
		tell application "Finder"
			move added_items to "Volumes:External:iMovie Events"
		end tell
	end if

Ultimately, the problem i’m having now is that when i run this, no items are moved and i’m left in the editor with a result of “Mac OS X:Users:user:Desktop:test.mov” and nothing at all in the event log. This at least confirms that the “home_path” thing i tried works for the path that i want, but that’s it.

this_folder must contain an alias


set this_folder to alias ((path to movies folder as text) & "iMovie Events:")

added_items must contain a list of aliases

set added_items to {alias ((path to desktop folder as text) & "test.mov")}

The Finder move command expects an alias or a Finder file specifier,
HFS paths start always with the disk name, not with the folder volumes like a POSIX path

 move added_items to folder "External:iMovie Events:"