Folder Action to append serial number to file name, upon entry ??

So I have an ongoing query in another post. I’ll be re-addressing that in the very near future. However, the current question, here, is just an extension of the one elsewhere…I hope you don’t mind.

On this go round:
I’d like to develop a simple Folder Action, such that when a file gets dumped into said folder, the count of the files of that folder is appended to the name of the file, upon entry. Sound’s odd by description, but it’s more obvious, I think, when described like this:

  1. When file “first one.jpg” enters the folder, it’s name then becomes “001_first one.jpg”.
  2. When the second file enters the folder, it’s name is appended with “002_”.
  3. When the third file enters the folder, it’s name is appended with “003_”.

I’m sure that makes sense.

(BTW - Thanks for all the help with all the scripts folks! This forum is an incredibly helpful resource. I’ve learned to really make computers do what computers were meant to do - by things I’ve learned on this forum. And I’m still just a noob, sort of a copy/paste/tweak man still… Anyway, sorry for the aside, but thanks are in order.)

Give this a try

property _counter : 0

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		repeat with anItem in addedItems
			try
				set _counter to _counter + 1
				set oldName to name of anItem
				set name of anItem to (text -3 thru -1 of ("00" & _counter) & "_" & oldName)
			end try
		end repeat
	end tell
end adding folder items to

Won’t the new name trigger the folder action again? Doesn’t the folder have to be moved?

It didn’t in my test. I added one file the first time… it became 001_… and then I dropped another two files simultaneously and they became 002_… and 003_… And it stopped there. Now that you mention it though that is unexpected behavior isn’t it?

Perhaps that’s been fixed?

Under some light testing, this script seems to work just fine for me. Thanks so much for the input. I’ll post back if I encounter an anomalies along the way. Otherwise, assume that no news is good news and I’m off & running!

Whoa now…

When I started moving from OS9 script to OSX there was a big issue with properties not being saved, and the solution was to use new preferences options (plists?) or something instead of storing a variable as a property in a script application, script file, etc. I’d do a lot of this (basically):

property countProperty : 0
--property mainFolder : "a folder path" --for example
--property prefScript : "path:preferences.scpt" --containing various property values I want to share
--set prefThings to (load script prefScript)
--prefThing's folderLocation
--prefThing's fileLocation
--and various variables shared among scripts, some that change with use others that don't
--or in the sequential numbering example of this thread
display dialog "counter is " & countProperty
set countProperty to countProperty + 1

That’s working - YAY!!! I have a whole lotta scripts that store preferences as property values, even if they don’t have to change, and share them among other scripts. Regardless of which method is more efficient or manageable there’s the relearn and rewrite issue of many, many, many old scripts.

I wanted to follow up and say that when I first reported success, the script was being tested on my home machine - Mac Pro, 10.5.2 and whatever version of the script editor came with it. Again worked fine.

However, when I attempted to run it on the machine where it’s ultimately expected to be run - for the “REAL” process, it began looping and adding recursive numbers. This machine is running 10.4.11 and Script Editor 2.1.1.

So I have to (want to, really) figure out how to work around this.

I guess it’s sort of funny: this is precisely one of those scenarios, now, where I’ve spent more time trying to come up with a slick solution to a long, laborious process, than it would have taken me to have just sat down and waded through the process in question. Oh well - that’s how it goes sometimes I guess. Still not done trying to automate it, if I can though!

Thanks.

Okay this would be the issue Adam referred to. In this scenario when the file is renamed it makes the folder actions think that a new file has been added and thus keeps launching the script. In Leopard that had been fixed apparently.

The work around is to move the files inside the script to a 3rd location and change the names once they are there.

I have seen this “move files out of the Folder Action folder before operating on them (especially renaming them)” workaround before and it does seem like the best idea to me.

Another workaround occurs to me, but it requires confidence that none of the files added to the folder will ever have the desired prefix at the time they are initially added to the folder (or if such ˜incoming files’ will already have the desired prefix then it is OK that they not be renamed and that their addition to the folder does not cause the counter to increment). It will not work for all “rename files inside the Folder Action folder” scenarios, but it might work for this one.

If the user is confident that the ˜incoming files’ will not already have the prefix (or that such already prefixed files will not be modified to increment the counter), then the handler could check the file’s name and only add a prefix if it does not already have one of the desired prefixes. The Folder Action would still ˜fire’ for the renamed files, but the handler would just ignore them since the name would already have the desired format.

Here is a slight rework of James Nierodzik’s script from earlier in the thread:

property _counter : 0
property _digits : "0123456789"

on adding folder items to thisFolder after receiving addedItems
	repeat with anItem in addedItems
		try
			tell application "Finder" to set oldName to name of anItem
			tell oldName to set alreadyNumbered to ¬
				character 1 is in _digits ¬
				and character 2 is in _digits ¬
				and character 3 is in _digits ¬
				and character 4 is "_"
			if not alreadyNumbered then
				set _counter to _counter + 1
				set newName to text -3 thru -1 of ("00" & _counter) & "_" & oldName
				tell application "Finder" to set name of anItem to newName
			end if
		end try
	end repeat
end adding folder items to

Note: If you make these changes and re-save your Folder Action script via Script Editor, the counter will be reset to 0. If you want to restart with a different number, just change the initial value of the _counter property before saving the script.