adding 'auto-open' to script which add '.doc' to files

The following is a great script which I got from a post on this site. It works fine, however you must manually run the script and then choose the folder, at which point all of the files in that folder will have a ‘.doc’ appended. I would really appreciate it if someone could advise on adding code which will allow me to drag the folder over the application script and then execute automatically for the files in this folder. I have another script which does the auto execution (but for a different function), so I can kind of see how it works – but as I don’t know applescipt I am apprehensive to tweak!

the script follows: :slight_smile:

set theFolder to choose folder with prompt “choose a folder of files to rename”
tell application “Finder” to set folderItems to get the name of every file of theFolder --get the name of every file in the folder - skip folders you could add code to limit the listing to only doc files

repeat with thisName in folderItems --repeat with every file name listed in the chosen folder
set thisName to thisName as string
set thisPath to (theFolder as string) & thisName as string --full path to the file so we can work with it
–check to see if it already has the extension
if thisName does not end with “.doc” then --then continue, we’ll add it
–check to see how long the name is now, if it is too long to add “.doc” we’ll have to trim it
if the (count of characters in thisName) is greater than or equal to 28 then
–max character count for file names is 31 so if it is already 28 or longer
–get characters 1 thru 27, then piece the extension to get the new name
set newName to characters 1 thru 27 of thisName & “.doc” as string
else
–or, if the file name is short enough just piece this name with the extension
set newName to thisName & “.doc” as string
end if
tell application “Finder” to set the name of file thisPath to newName
end if
end repeat

Simply add an “open” handler to your app and you are done!
When you drop some stuff onto your new droplet, it receives an alias list. We will process only the first item of such list (the folder you dropped).
If instead drag & drop, you double-click your app, the var “theFolder” won’t be defined and it will prompt you to choose a folder:

property theFolder : ""
on open thisFolder
     set theFolder to item 1 of thisFolder
     run
     set theFolder to ""
end open

if theFolder = "" then set theFolder to choose folder with prompt "choose a folder of files to rename" 
 tell application "Finder" to set folderItems to get the name of every file of theFolder --get the name of every file in the folder - skip folders you could add code to limit the listing to only doc files  
  
 repeat with thisName in folderItems --repeat with every file name listed in the chosen folder  
    set thisName to thisName as string 
    set thisPath to (theFolder as string) & thisName as string --full path to the file so we can work with it  
    --check to see if it already has the extension  
    if thisName does not end with ".doc" then --then continue, we'll add it  
       --check to see how long the name is now, if it is too long to add ".doc" we'll have to trim it  
       if the (count of characters in thisName) is greater than or equal to 28 then 
          --max character count for file names is 31 so if it is already 28 or longer  
          --get characters 1 thru 27, then piece the extension to get the new name  
          set newName to characters 1 thru 27 of thisName & ".doc" as string 
       else 
          --or, if the file name is short enough just piece this name with the extension  
          set newName to thisName & ".doc" as string 
       end if 
       tell application "Finder" to set the name of file thisPath to newName 
    end if 
 end repeat

Yes, I added the open handler and there is one strange thing. If I take a folder full of files and drop in on the app, it works to change over all of the files. Then if I go into the folder and change a few of the files back, and then drop it again, sometimes it will change them and sometimes it won’t. I’ve played with it endlessly doing the exact same dropping and can’t figure out what is different when it works. Could it be some variable is not initialized or is there some special procedure for invoking?

The line which asks for a list of items within the dropped folder is this:

tell application "Finder" to set folderItems to get the name of every file of theFolder

And it should work fine and get refreshed every time you run the app… So, I have no idea about what’s going on… The only thing I allways do is avoid using the Finder, since it has been proved to have some bug, be sloooow and so on.
In a regular script, I would substitute such line with:

set folderItems to (list folder theFolder without invisibles)

But I don’t know if this will solve your problem :?: