Finder script help anybody

I was given this script from a Adobe.com PhotoShop user forum contributor and I need an applescripters eye to see what is out of place or missing with it. The script is used as a folder action script and it checks to see if " copy" is in the name of a file added to the folder. If so it removes the " copy" and moves the file up one folder in the hierarchy. This is a great script for PhotoShop users as PS adds a space and the word copy to all files save using the save a copy command so if you want to make a .tif from a psd you’re stuch with “extra” characters in the filename.
The script works but will crash the finder occasionally so I thought someone here might be able to spot why. Thanks
Here’s the script:

on adding folder items to theFolder ¬
after receiving stuff
tell application "Finder"
with timeout of 500 seconds
repeat with aFile in stuff
if the name of aFile contains " copy" then --tests to see if copy is in the name
set x to the name of aFile --gets the name of the file
set y to length of x -- gets the number of characters in the name
set newx to (text 1 thru (y - 9) of x) & (text (y - 3) thru -1 of x) --makes a new name subtracting the characters in " copy"
set name of aFile to newx -- renames the file
move aFile to (container of theFolder) with replacing --moves renamed file to folder above replacing same named file
end if
end repeat
end timeout
end tell end adding folder items to

Thanks tet.
I ran your script and I get a -48 error Also your script tests for the word “copy” as the last item in the list where the script I’m trying to troubleshoot removes characters 9 through 5 counting from the end efectivly changing filename copy.ext to filename.ext
Thanks for your help though! Cameron

I’m not sure why it would crash, but try this variation:

 on adding folder items to theFolder after receiving stuff 
 tell application "Finder" 
 with timeout of 500 seconds 
 set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "} 
 if stuff is not list then set stuff to (stuff as list) 
 repeat with myCount from 1 to count of stuff 
 set aList to ((name of item myCount of stuff) as text) 
 set aList to text items of aList 
 if the last item of aList is "copy" then --tests to see if copy is in the name  
 set aList to items 1 thru ((count of aList) - 1) of aList 
 set name of item myCount of stuff to (aList as text) 
 move item myCount of stuff to (container of theFolder) with replacing --moves renamed file to folder above replacing same named file  
 end if 
 end repeat 
 set AppleScript's text item delimiters to oldTID 
 end timeout 
 end tell 
 end adding folder items to 

Luck,

–tet

Ah, I was thinking “filename.ext copy”. Maybe more like this, then:

 to doSomething(theFolder, stuff) 
 tell application "Finder" 
 with timeout of 500 seconds 
 if stuff is not list then set stuff to (stuff as list) 
 repeat with aFile in stuff 
 if the name of aFile contains " copy" then --tests to see if copy is in the name  
 set x to the name of aFile --gets the name of the file  
 set y to offset of " copy" in x -- gets the number of characters in the name  
 set z to (count of characters in x) 
 set newx to (text 1 thru y of x) & (text (y + 5) thru z of x) --makes a new name subtracting the characters in " copy"  
 try 
 set name of aFile to newx -- renames the file  
 move aFile to (container of theFolder) with replacing --moves renamed file to folder above replacing same named file  
 on error theError 
 beep 
 display dialog (name of aFile & return & theError) 
 end try 
 end if 
 end repeat 
 end timeout 
 end tell 
 end doSomething 

A -48 error is a duplicate file name error. I’ve included a simple trap for errors that gives the problem file’s name, which may help you pin down what’s going on.

–tet