Saving files with part of Photoshop CS3 layer name

I’m writing a complicated script to export multiple JPEGs from a given Photoshop file that has multiple “color” layers. The layer structure for my Photoshop files is something like this:

-“(Product 1)” - This is a group of layers representing a product - needs to stay visible for the exported JPEG
-“(Product 2)” - Another group that needs to stay invisible for the first round of saving out the images. Will be made visible for round two.
-“COLOR: White - S01”
-“COLOR: Red - S12”
-“COLOR: Black - S04”
-“(Other)” - One or more layers that need to stay visible for the exported JPEG

As part of my script, I need to be able to pull the “S” number from each color layer and use it as part of the resulting JPEG file name. How can I pull just part of each of those layer names? The resulting files would be named something like this: “PRODUCT_S12.jpg”

Thanks in advance!


the result of this script is a list with the S numbers( colorList)

tell application "Adobe Photoshop CS3"
	set n to name of layers of document 1 whose name begins with "COLOR:"
end tell
set colorList to {}
repeat with i in n
	set end of colorList to last word of i
end repeat

That works perfectly! Thanks!