folder action to add integer to name of added item if exists

hi,
can any one please help me with this folder action,
i am trying to write a script so that if a file is dropped into a folder where a file of the same name exists, the script will add a “1” after the file name and before the name extension. if that file aleady exists, replace the “1” with a “2”, and so on.

i have a folder action that kind of works, but it only works when i add loads of files at once and doesn’t retain any infomation if i add more files.

heres the script so far


on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set {fileCount} to {1}
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
		
			
			if name of this_item contains ".lso" then
				set FileName to name of this_item
				copy length of FileName to Filenamelength
				set TopLimit to Filenamelength
				copy characters -3 through TopLimit of FileName as string to NewFilename
				set theText to FileName
				set ReplaceString to ""
				
				set OldDelims to AppleScript's text item delimiters
				set AppleScript's text item delimiters to NewFilename
				set newText to text items of theText
				set AppleScript's text item delimiters to ReplaceString
				set finalText to newText as text
				set AppleScript's text item delimiters to OldDelims
				finalText
				
				move file this_item to "system HD:Users:joshhughes:Desktop:images"
				
				
				set name of file this_item to ((finalText) &fileCount & ".lso")
				
				set fileCount to fileCount + 1
				
				
			end if
	
		end repeat
		
	end tell
end adding folder items to


i’m thinking maybe i could try an on error, then get the scipt to find the integer in the file name and replace it with the next number up!
any pointers to where im going wrong would be greatfully recieved.
many thanks,
truth
peace

Would a repeat loop work? (Like the one I use in my New Text File app.)

cheers bruce,
that looks like just what i need.
i,ll go and have a play and let you know the result

ok,
i seem to have it just about working, but for some reason, occasionally the number suddenly jumps up by either 10 or 100,
i dont quite get it as other tha that, th script works fine,
can anyone see any areas that could be improved?
heres the script


property max : "99"
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			try
				move this_item to folder "system HD:Users:joshhughes:Desktop:images"
				
			on error
				copy name of this_item to FileName
				copy length of FileName to Filenamelength
				set TopLimit to Filenamelength
				copy characters -4 through TopLimit of FileName as string to the_extension
				copy characters 1 through -5 of FileName as string to NewFilename
				copy characters 1 through -5 of FileName as string to NewFilename2
				
				repeat with i from 1 to max
					set name of this_item to NewFilename & i & the_extension
					try
						move this_item to "system HD:Users:joshhughes:Desktop:images"
						exit repeat
					end try
				end repeat
			end try
		end repeat
	end tell
end adding folder items to

thanks in advance
peace
truth

i think i got it
i ended up going back to th text delimiters combined with a repeat rutine.
looks good so far


property max : "99"
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			try
				move this_item to folder "system HD:Users:joshhughes:Desktop:images"
				return result as alias
			on error
				set AppleScript's text item delimiters to {"/"}
				set thefile to this_item
				set fname to POSIX path of thefile
				set b to last text item of fname
				set AppleScript's text item delimiters to {"."}
				set c to text item 1 of b
				set AppleScript's text item delimiters to {"."}
				set d to last text item of b
				repeat with j from 2 to max
					set name of this_item to {c & "." & j & "." & d}
					try
						move this_item to "system HD:Users:joshhughes:Desktop:images"
						exit repeat
					end try
				end repeat
			end try
		end repeat
	end tell
end adding folder items to

nice one!
peace
truth

Hi truth. It may be worth noting that folder actions can be rather unpredictable and, for the moment, I try to use them only as a last resort. (For example, check out this discussion - which covers only one of several issues…)

Since your script appears to move every dropped file, you might like to consider using a droplet in place of a folder action. I have a feeling that your results would be more reliable. (OK, you may not be able to option-drag (copy) items to a droplet, as you can with a folder - but in almost every other respect, it should be a good alternative for what you appear to be doing.)

In case you want to try it out, here’s an example (which contains one or two techniques that you could also use in a folder action)…

(* save script as application *)

property targetFolder : (path to desktop as Unicode text) & "images" (* modify as required *)
property tempName : ASCII character 1

on open l
	set f to list folder targetFolder without invisibles
	tell application "Finder" to repeat with i in l
		tell item i
			set n to name
			if n is in f then
				set e to name extension
				tell (count e) to if it is 0 then
					set b to n & " ["
					set e to "]"
				else
					set b to n's text 1 thru -(it + 2) & " ["
					set e to "]." & e
				end if
				set v to 1
				repeat while n is in f
					set v to v + 1
					set n to b & v & e
				end repeat
				set name to tempName
				move to targetFolder
				set name to n
			else
				move to targetFolder
			end if
		end tell
		set f's end to n
	end repeat
end open

thanks kai,
i must admit, i do find folder action’s a bit unpredictable, and dont like using them unless i really have to,
i havent really checked out dropplets yet but i will deffinatly do so,

the intention of this folder action is so that when i save a song in logic pro, the song is saved to my “logic” folder which also has a folder action that moves any song files to my “logic songs” folder which has the above folder action. the folder action then makes a new folder in “logic songs” named after the song file. any additional song files with the same name that enter “logic songs” are moved to the correct folder but with an increment of 1 added to the name so that i can save multiple vrsions of the same song and easely go back to any stage of the song’s developement and either take an idea that i abandond or take the song on an entirely different direction,

i was having a lot of trouble with the “logic songs” folder being moved or anything other than an “.lso” file entering the “logic songs” folder, but i made a few emendments to the script and it seems a lot more solid,

seems like a lot of work for something logic should do anyway, but hay, at least i know where my files are giong.

anyway, thanks very much for the feedback, new methods and ideas are alway welcome.

heres the ammended script :


property max : "99"
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			if name of this_item contains "lso" then
				try
					set AppleScript's text item delimiters to {"/"}
					set thefile to this_item
					set fname to POSIX path of thefile
					set b to last text item of fname
					set AppleScript's text item delimiters to {"."}
					set c to first text item of b
					set AppleScript's text item delimiters to {"."}
					set d to last text item of b
					if not (exists folder c of folder this_folder) then
						make new folder at folder this_folder with properties {name:c}
					end if
					move this_item to folder c of folder this_folder
					exit repeat
					return result as alias
				on error
					repeat with j from 2 to max
						set name of this_item to {c & "." & j & "." & d}
						try
							move this_item to folder c of folder this_folder
							exit repeat
						end try
					end repeat
				end try
			end if
		end repeat
	end tell
end adding folder items to

peace
truth