Problem with folder content reanming

I’m trying to build a folder action that watches for folders of images to be added to it. When one arrives, the script should take the name of that folder and use it to modify the names of the image files within that folder

for example the path:
FAFolder/AddedFolderName/DSCN1001
would become
FAFolder/AddedFolderName/AddedFolderName1001

I had the script working with bare images, but I can’t get it to work when I modified it to work with folders of images.

Does anyone see what is wrong with my script below?
(the set_item_name procedure was borrowed from elsewhere)

Thank you.
azide

property text_to_trim : “DSCF” – the text to be removed from the begining of file names.
property itin_count : 0 --records how many times the script has run

on adding folder items to source_folder after receiving added_items

repeat with add_count from 1 to number of items in the added_items
	set this_folder to item add_count of the added_items
	set this_folderinfo to info for this_folder
	if folder of this_folderinfo is true then
		
		tell application "Finder"
			set the folder_name to the name of this_folder -- record the name of this folder
		set the this_folder to this_folder as alias
			set folder_items to every item in this_folder
		end tell
		
		set the character_count to the number of characters of the text_to_trim
		set this_folder to this_folder as string
		
		repeat with i from 1 to number of items in the folder_items -- loop to process individual items
			set this_item to item i of the folder_items
			set this_info to info for this_item  -- ***** the script seems to stop here *****
			if folder of this_info is false then -- ignore folders for renaming
				set the current_name to the name of this_info -- record it's name
				if the current_name begins with the text_to_trim then -- ignore items with names that we are not interested in
					-- crop out the text_to_remove
					set the new_name to (characters (the character_count + 1) thru -1 of the current_name) as string
					set the new_name to the folder_name & new_name -- create the new name
					set_item_name(this_item, new_name) -- send the name to the renaming procedure
				end if
			end if
		end repeat
		
	end if
end repeat

end adding folder items to

on set_item_name(this_item, new_item_name)
– This procedure safley renames a Finder file/folder.
tell application “Finder”
–activate
set the parent_container_path to (the container of this_item) as text
if not (exists item (the parent_container_path & new_item_name)) then
try
set the name of this_item to new_item_name
on error the error_message number the error_number
if the error_number is -59 then
set the error_message to “This name contains improper characters, such as a colon (:).”
else --the suggested name is too long
set the error_message to error_message – “The name is more than 31 characters long.”
end if
–beep
tell me to display dialog the error_message default answer new_item_name buttons {“Skip”, “OK”} default button 2
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is “Skip” then return 0
my set_item_name(this_item, new_item_name)
end try
else --the name already exists
–beep
tell me to display dialog “This name is already taken, please rename.” default answer new_item_name buttons {“Skip”, “OK”} default button 2
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is “Skip” then return 0
my set_item_name(this_item, new_item_name)
end if
end tell
end set_item_name


I haven’t analyzed the script closely but, according to the comments, the problem starts when the script gets down to the business of working with the contents of the folders. Is it possible that the files are still in the process of arriving when the folder action kicks in? This is sometimes the case when files are copied over a network.

– Rob

The files are coming in locally.
I had simply dropped in a single folder containing a few pictures to test the folder action.

I had tried to debug the script by adding numbered “display dialog” lines in between lines of code. The commented section is as far as the script would get before stopping. I suspect the problem may actually lie six lines up from that point. There the script gets the contents of the dropped-in-folder for processing. I’m guessing that there is some difference between the way added_items behaves (the group of things for a folder action to process) and the way the finder processes the line: set folder_items to every item in this_folder" I would think these two should work in the same manner, but possibly not?

I do know a previous version of the script worked on individual pictures being added, but I began to run into problems when I changed that script to work on dropped-in folders full of pictures instead of groups of individual dropped-in pictures.

Thanks again.
Azide

Does it help to change this line: set folder_items to every item in this_folder

to this: set folder_items to every item in this_folder as alias list

?

– Rob

Cool!

Thank you very much.
That was the bit that I was missing.

:smiley: Azide :smiley: