duplicate file and increment

I’ve got files named this: 2855-0001.jpg and 2855-0001_01.jpg
I need to duplicate the file and increment the -0001 by one. ideally i would like to specify how many times for it to do this and it would increment by that many with duplication.

Here’s what i have:


on run
	tell application "System Events"
		set {tName, nameExt} to {name, name extension} of thisFile
		set itemNumberLast to {text 6 thru 9 of tName} + 1
		set itemNumberFirst to {text 1 thru 5 of tName}
		set tName to (itemNumberFirst & itemNumberLast & nameExt)
		duplicate thisFile and rename to tName
	end tell
end run

this is really just a one off duplicate and increment based on dropping a file onto it and making the duplication happen at the location of the file.
Obviously this doesn’t work, but it’s at least so you can see where my thinking is.
I’ve looked around and found some scripts that do something similar but i’m not sure how to adapt to my specific scenario.

You may try :

on run
	set aFile to choose file
	my germaine(aFile)
end run

on open sel
	set aFile to sel's item 1
	my germaine(aFile)
end open

on germaine(thisFile)
	set incr to 4 # edit this increment to fit your needs or define it thru a dialog.
	set posixPath to POSIX path of thisFile
	tell application "System Events"
		set tName to name of thisFile
		set itsFolder to POSIX path of container of thisFile
	end tell
	set nameAsList to my decoupe(tName, {"-", "_", "."})
	set itemNumberLast to (item 2 of nameAsList as integer) + incr
	set itemNumberLast to text -4 thru -1 of ((10000 + itemNumberLast) as text)
	if (count nameAsList) = 3 then
		set newName to item 1 of nameAsList & "-" & itemNumberLast & "." & item 3 of nameAsList
	else
		set newName to item 1 of nameAsList & "-" & itemNumberLast & "_" & item 3 of nameAsList & "." & item 4 of nameAsList
	end if
	
	if itsFolder does not end with "/" then set itsFolder to itsFolder & "/"
	set newPath to itsFolder & newName
	
	do shell script "cp " & quoted form of posixPath & space & quoted form of newPath
end germaine

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

Oops, I missed that you wanted to be able to drop the file to treat upon the script’s icon.
I completed the script accordingly.
You will have to save it as an application.
If you double click the new app, it’s the run handler which will be triggered and you will be asked to choose a file in a dedicated dialog.
If you drop the file onto the script’s icon, it’s the open handler which will be triggered.
The variable sel will contain a list so the script will extract the first item of this list to treat it.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 28 novembre 2016 19:43:55

This is amazing. Thank you!