Can anyone tell me why this error occurs???

Hi, i’ve been posting here for guidance on a script i’'m working on.
Its function is to grab a file in high ress from a folder, create a preview and a thumbnail in another folder, and move the high ress file to that folder in the end. It works pretty good, but now i want it to change the names of the files when it recognizes that they are already in use. The error msg i get is something like “Finder got a error, can’t continue change_name”, wich is the function that changes the name of the high ress file.
If it isn’t too much trouble, i would really apriciate your help.
Thank you very much.

PS: i’m new to Applescript, and it is my first and only language… sorry.


property pwg_foldername : "pwg_high"
property tn_foldername : "thumbnail"
--thumbnails y pgws se crean en .jpg
property low_ext : "jpg"
global name_increment

on adding folder items to this_folder after receiving these_items
	try
		tell application "Finder"
			set this_item to item 1 of these_items
			set relative_path to text 29 thru end of (container of this_item as string)
			set that_folder to "Marto:imagenesALFA:galerias:" & relative_path
			set error_folder to "Marto:imagenesERROR:galerias:" & relative_path
			set test_pwg to (that_folder & pwg_foldername)
			set test_tn to (that_folder & tn_foldername)
			if not (exists folder test_pwg) then
				make new folder at that_folder with properties {name:pwg_foldername}
			end if
			set pwg_folder to test_pwg as alias
			if not (exists folder test_tn) then
				make new folder at that_folder with properties {name:tn_foldername}
			end if
			set tn_folder to test_tn as alias
		end tell
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
	try
		repeat with i from 1 to number of items in these_items
			tell application "Finder"
				set name_increment to 0
				set this_item to item i of these_items
				my change_ext(this_item, "", pwg_folder)
				set the file_name to my change_ext(this_item, low_ext, pwg_folder)
				if (name_increment ≥ 1) then
					set this_item to change_name(this_item)
				end if
				set the source_file to (this_item) as alias
			end tell
			try
				process_pwg(source_file, file_name, pwg_folder, tn_folder)
				tell application "Finder" to move this_item to the that_folder
			on error error_process
				tell application "Finder" to move this_item to the error_folder
			end try
		end repeat
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 30
		end tell
	end try
end adding folder items to


on change_ext(this_item, new_extension, target_folder)
	tell application "Finder"
		set the file_name to the name of this_item
		set file_extension to the name extension of this_item
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		if the new_extension is "" then
			set target_name to file_name
			set target_extension to file_extension
		else
			set target_extension to new_extension
			set target_name to (the trimmed_name & "." & target_extension) as string
		end if
		if (exists document file target_name of target_folder) then
			set the name_increment to 1
			repeat
				set the new_name to (the trimmed_name & "-" & (name_increment as string) & "." & target_extension) as string
				if not (exists document file new_name of the target_folder) then
					-- rename to conflicting file
					set target_name to the new_name
					exit repeat
				else
					set the name_increment to the name_increment + 1
				end if
			end repeat
		end if
	end tell
	return the target_name
end change_ext


on change_name(this_item)
	try
		tell application "Finder"
			set the file_name to the name of this_item
			set file_extension to the name extension of this_item
			if the file_extension is "" then
				set the trimmed_name to the file_name
				display dialog file_name
			else
				set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
				display dialog file_name
				
			end if
			set the new_name to (the trimmed_name & "-" & (name_increment as string) & "." & file_extension) as string
		end tell
		return the new_name
	on error error_msg
		tell application "Finder" to display dialog error_msg buttons {"ERROR"} default button 1
	end try
end change_name


on process_pwg(source_file, file_name, pwg_folder, tn_folder)
	try
		set the pwg_path to ((pwg_folder as string) & file_name) as string
		set the tn_path to ((tn_folder as string) & "tn_" & file_name) as string
		with timeout of 900 seconds
			tell application "Image Events"
				launch
				set this_image to open file (source_file as string)
				scale this_image to size 400
				save this_image as JPEG in file pwg_path with icon
				close this_image
				set this_image to open file (pwg_path as string)
				scale this_image to size 120
				save this_image as JPEG in file tn_path with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_pwg

ok. i’ve “fixed” it, moved the whole ‘change_name’ function to the main. so it is a little bit messy, but it works.
never the less, i still get an error… it seems to have a problem when moving the high ress changed name file to the destination folder… it moves it, but then says NSReceiverEvaluationScriptError: 4.
what does this mean? how can i fix it? beacause the script is set to move the file to an error folder on error, but i can’t tell what’s wrong…
i leave the main function here…
thanks.

property pwg_foldername : "pwg_high"
property tn_foldername : "thumbnail"
--thumbnails y pgws se crean en .jpg
property low_ext : "jpg"
global name_increment


on adding folder items to this_folder after receiving these_items
	try
		tell application "Finder"
			set this_item to item 1 of these_items
			set relative_path to text 29 thru end of (container of this_item as string)
			set that_folder to "Marto:imagenesALFA:galerias:" & relative_path
			set error_folder to "Marto:imagenesERROR:galerias:" & relative_path
			set test_pwg to (that_folder & pwg_foldername)
			set test_tn to (that_folder & tn_foldername)
			if not (exists folder test_pwg) then
				make new folder at that_folder with properties {name:pwg_foldername}
			end if
			set pwg_folder to test_pwg as alias
			if not (exists folder test_tn) then
				make new folder at that_folder with properties {name:tn_foldername}
			end if
			set tn_folder to test_tn as alias
		end tell
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
	try
		repeat with i from 1 to number of items in these_items
			tell application "Finder"
				set name_increment to 0
				set this_item to item i of these_items
				my change_ext(this_item, "", pwg_folder)
				set the file_name to my change_ext(this_item, low_ext, pwg_folder)
				if (name_increment ≥ 1) then
					try
						tell application "Finder"
							set the file_name1 to the name of this_item
							set file_extension1 to the name extension of this_item
							if the file_extension1 is "" then
								set the trimmed_name to the file_name1
							else
								set the trimmed_name to text 1 thru -((length of file_extension1) + 2) of the file_name1
							end if
							set the new_name to (the trimmed_name & "-" & (name_increment as string) & "." & file_extension1) as string
						end tell
					on error error_msg
						tell application "Finder" to display dialog error_msg buttons {"ERROR"} default button 1
					end try
					set the name of this_item to new_name
				end if
				set the source_file to (this_item) as alias
			end tell
			try
				process_pwg(source_file, file_name, pwg_folder, tn_folder)
				tell application "Finder" to move this_item to the that_folder
			on error error_process
				tell application "Finder" to move this_item to the error_folder
			end try
		end repeat
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 30
		end tell
	end try

At first glance, M, it appears that you do a lot of work to change the extension and add the name_increment, but then pass the change_name handler the original name in this_item rather than the new ones you’ve constructed.