Sequential numbering when added to folder

I am looking to be able to drop files into a folder and have them renamed “001.jpg” “002.jpg”, etc.

I tried looking at Automator, but that doesn’t seem to do the job I need, I’m thinknig AppleScript is more the right approach.

I found this script doing a search, which would work, but after the first file it doesn’t work again. I don’t really know much about AppleScript, although it reminds me of doing BASIC back when I was a kid.

Thanks for all your help and advice in advance!

on adding folder items to this_folder after receiving thoose_files
set defaultname to “MyName_” – the name you want to give to your files
set leadingZeros to 3 – the size of the argument of the file

tell application “Finder”

       set z to count (files in thoose_files whose name contains defaultname)
   on error
       set z to 0
   end try

   try --count the number of files in the folder with the default name
       set y to count (files in this_folder whose name contains defaultname)
   on error
       set y to 0
   end try

   repeat with x from 1 to count thoose_files

       set argumentnumber to (x + y - z - 1) as string -- create the correct argument

       tell application "Finder"

           repeat until (count (argumentnumber)) > leadingZeros - 1
               set argumentnumber to "0" & argumentnumber
           end repeat

       end tell
       set name of item x of thoose_files to defaultname & argumentnumber --change the name
   end repeat

end tell

end adding folder items to


renaming items in a folder action will cause a big problem.
If the first item has been renamed the handler will recognize it as a new item
and will rename it again, and so on…

So it would be better to use a droplet or move the items to a different location before renaming them.

A renaming routine can look like this:

set theFolder to choose folder
set renameText to text returned of (display dialog "Please enter the text to rename" default answer "")

tell application "Finder" to set these_files to document files of theFolder
set idx to 1
repeat with this_item in these_files
	set fExt to name extension of (info for this_item as alias)
	tell application "Finder" to set name of contents of this_item to renameText & "_" & text -3 thru -1 of ("000" & (idx as string)) & "." & fExt
	set idx to idx + 1
end repeat