Rename mixed filenames to proper sequence

Hi all. Just learning to use Applescript with Automater. Need some help. I have a long list of image files that need to be renamed to match SKU numbers of an ecommerce site.

Current mixed filenames.
111 sometext-S.jpg
111 sometext-M.jpg
111 sometext-L.jpg
222-sometext S.jpg
222-sometext M.jpg
222-sometext L.jpg
333 S.jpg
333 M.jpg
333 L.jpg

Need to clean files to show _01 for S (small), _02 for M (medium), and _03 for L (large). Always keeping first 3 numbers which is the item number.

Final filenames.
111_01.jpg
111_02.jpg
111_03.jpg
222_01.jpg
222_02.jpg
222_03.jpg
333_01.jpg
333_02.jpg
333_03.jpg

I’m pretty sure this is simple if and elseif kind. Any help would be appreciated. Thanks in advance.

Hi,

try this AppleScript droplet, save it as application.
You can either drop files onto it or choose a folder, if you double click it.
The script considers only jpg files


on run
	set theFolder to choose folder
	tell application "Finder" to set theFiles to files of theFolder as alias list
	open theFiles
end run

on open theseItems
	repeat with oneItem in theseItems
		set {name:Nm, name extension:Ex} to info for oneItem
		tell Nm to set {pF, sF} to {text 1 thru 3, text -5}
		if Ex is "jpg" then
			if sF is "S" then
				tell application "Finder" to set name of contents of oneItem to pF & "_01.jpg"
			else if sF is "M" then
				tell application "Finder" to set name of contents of oneItem to pF & "_02.jpg"
			else if sF is "L" then
				tell application "Finder" to set name of contents of oneItem to pF & "_03.jpg"
			end if
		end if
	end repeat
end open

Thank you kindly Stefan. This did wonders. Saved me lots of time.