Renaming folder with Folder Action

Ok, like many folks on here I’m new to Applescript, so this is hopefully pretty simple:

I need a folder action script that will grab the folder name, check the folder for the existing folder contents for the next available serial and name the newly added items (e.g. foldername _SN.ext ).

here’s what I have so far. I can’t get past getting the handler to work. I have the dialog popping up to help me test it b/c I have it attached to the folder and can’t test it in script editor. Once I get the handler to work then I think I can manage setting the delimiters and getting the last item serial number. I haven’t looked at the renaming. but I figure I’m not getting anywhere if I can’t put all this into handlers.

I know it has something to do with scoping, but even when I try to declare this_folder as a global, it still doesn’t work with the handler. If i pull the handler call (GetLastItem) from the main script it runs.



on adding folder items to this_folder after receiving added_items
	
	try -- get number of items added
		tell application "Finder"
			set the folder_name to the name of this_folder
		end tell
		set the item_count to the number of items in the added_items
	end try
	
	if item_count is less than 2 then -- build alert message
		set alert_message to the folder_name & "'s last item was  " & GetLastItem() & " ."
	else
		set alert_message to the folder_name & " has gotten " & item_count & " new items. The folder's last item was  " & GetLastItem() & "."
	end if
	
	display dialog alert_message buttons {"Roger Dodger"} -- display message
	
	
end adding folder items to

on GetLastItem()
	
	set last_item to last item of this_folder
	return last_item as string
	
end GetLastItem

I’ve found that renaming a file in a folder with a FA attached keeps the FA in a perpetual loop because it treats the newly named file as a new file. I’m sure there is a more elegant way of doing this but here is a FA script that will move any newly added files/folders to a subfolder and then name them sequentially regardless of the extension (with leading zeros):

property renamed_folder : "renamed:"

on adding folder items to this_folder after receiving added_items
	try
		set this_folder to this_folder as string
		tell application "Finder"
			set folder_name to name of folder this_folder
			repeat with this_item in added_items
				set this_item to this_item as alias
				try
					set the_extension to name extension of this_item
					if the_extension is in {missing value, ""} then
						set the_extension to ""
					else
						set the_extension to "." & the_extension
					end if
				on error
					set the_extension to ""
				end try
				set the_sn to count of items of folder (this_folder & renamed_folder)
				repeat
					try
						set the_sn to the_sn + 1
						set new_name to (folder_name & "_" & (my add_leading_zero(the_sn)) & the_extension)
						get (this_folder & renamed_folder & new_name) as alias
					on error
						exit repeat
					end try
				end repeat
				set name of (move this_item to folder (this_folder & renamed_folder)) to new_name
			end repeat
		end tell
	on error the_error
		display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
	end try
end adding folder items to

on add_leading_zero(the_num)
	return text 2 thru -1 of ((10000 + the_num) as string)
end add_leading_zero

There are also other similar threads that may be of use.

Jon

Jon,

Thanks for the reply. The problem is that for it to be useful, it needs to both go directly into the folder and be aware of already serialized items in the folder. I’m working on it.
I was wondering, does the FA restart on any write to the file e.g. the comment field? I’m wondering if there is any way to tag the file as already run through the script, then just exiting the loop if it comes across one that is tagged, however that may be. I figure I can do that anyway by creating a temporary folder, moving the items in there, naming them and tagging them (i.e. “named” in comment or something) then putting them back in the original scripted folder and erasing the temp folder. At the beginning of the script would be a test for “named” in comment of any of the items. The problem is the way this script is going to be used is potentially putting in items one at a time, and I would rather not go through that for 1 item at a time.

If I run into a snag I’ll be back no doubt.
Thanks again

-Andy