Help with folder action script to move and rename a file

I wrote a script as a folder action to take a saved file, check to see if it is a bank statement,move the file into a folder, and rename it. When I run it using choose file instead of as a folder action (in an effort to debug it) it works fine. When run as a folder action, it gets hung up, I think, on the line set name of new_item to newFileName. I cannot figure out why

Here is the script:

on adding folder items to folder_ after receiving new_item
–set new_item to choose file for debugging
set MonthNames to {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”}
set MonthNumbers to {“01”, “02”, “03”, “04”, “05”, “06”, “07”, “08”, “09”, “10”, “11”, “12”}
set fileName to name of (info for new_item)
set fileExtension to name extension of (info for new_item)
ignoring case
if fileName contains “statement” then
repeat with i from 1 to 12
if fileName contains item i of MonthNames then
set thisMonth to item i of MonthNumbers
end if
end repeat
if thisMonth is “” then
tell me to quit
end if
set thisYear to do shell script “date +%Y”
display dialog “Enter Year For Statement” default answer thisYear
set thisYear to text returned of result
set newFileName to “APG Statement.” & thisYear & “.” & thisMonth & “.” & fileExtension
tell application “Finder”
set name of new_item to newFileName
move new_item to folder “Macintosh HD:Users:iMacHome:Documents:APG Statements:”
end tell
end if
end ignoring

end adding folder items to

Hi,

the after receiving parameter returns a list of alias specifiers, so you have to implement a repeat loop


on adding folder items to folder_ after receiving new_items
	repeat with new_item in new_items
		set MonthNames to .
		-- .
	end repeat
end adding folder items to

Thanks Stefan. Since I am only adding one file when I save the bank statement, I did not think I needed a repeat. The problem I am experiencing is that the file does not get renamed then moved, hence the problem is in the tell finder block. If I switch those statements to move then rename, the file gets moved, but not renamed. It does it if I use choose file and run the script selecting the file, but as a folder action, the rename part of the tell finder block fails. I don’t know why. Your help is appreciated.

Michael

Again, Thanks Stefan,

I tried putting it all in a repeat block and it worked fine. You were right (as usual). I just don’t understand why it didn’t work otherwise.

instead of a repeat loop you could just grab the first item of the list


on adding folder items to folder_ after receiving new_items
	set new_item to first item of new_items
	set MonthNames to .
	-- .
end adding folder items to

but I would keep the repeat loop