folder action to rename added files to...

i need a folder action to rename added files to “name_xxx” where name is constant and xxx is a numerical value that changes acording to the last numerical value available in that folder. So basically when i add a file to this folder, the folder should rename the file to let’s say “flowers_” and after the underscore, the script should figure out how many of these files there are in this folder and add the latest number, let’s say “006” to the file name. I know many people have already asked how to create a consecutive renaming of files script, but i need something that allows me to add a constant name followed by a consecutive unconstant numbering of files. thanks you for anyone that can give me some clear pointers and help!

: i need a folder action to rename added files to
: “name_xxx” where name is constant and xxx is a
: numerical value that changes acording to the last numerical
: value available in that folder. So basically when i add a file
: to this folder, the folder should rename the file to let’s say
: “flowers_” and after the underscore, the script
: should figure out how many of these files there are in this
: folder and add the latest number, let’s say “006” to
: the file name. I know many people have already asked how to
: create a consecutive renaming of files script, but i need
: something that allows me to add a constant name followed by a
: consecutive unconstant numbering of files. thanks you for
: anyone that can give me some clear pointers and help!

Here’s the beginning of a solution, but there is a major problem, you can only add one file at the time.

on adding folder items to (alias MyFolder) after receiving thoose_files --Where MyFolder is the folder where to attach the action folder script
	set default_name to "flower_"
	tell application "Finder"
		set y to count (files in (alias MyFolder) whose name contains default_name)
		repeat with x from 1 to count thoose_files
			set argument to (x + y - 1)
			set name of item x of thoose_files to default_name & argument
		end repeat
	end tell
end adding folder items to

The following script should work.

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"

		try
			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

Jean-Baptiste LESTANG

: The following script should work.
:
: 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”
: try
: 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
:
: Jean-Baptiste LESTANG

thanks everyone, i will try to make these scripts work, just for the sake of learning better actionscript, but in the meanwhile i got hold of a utility called “File Buddy” and this is something i have to say, essential to every mac use. it has many more otions than sequentially renaming files and ist’s a very good little program. thnaks you for posting a reply though!