Script to rename files

Let me preface this by stating I have absolutely no idea what I'm doing in this arena, but I'm in need of a little help here.

I need to take filenames like this: amwed_0402c-slug~1-cp.jpg
And reduce them to slug~1.jpg

That is, I need to remove the first 12 and last 3 characters. The characters will frequently be different, but they will always be the first 12 and last three (before the file extension).

Anyway, I need to be able to execute this using Automator on the Mac so I can incorporate it into some photoshop actions.

Can anyone tell me how to do this? Any help would be much appreciated.

Anyway, the Automator reference is a red herring, I see how to execute it using this program, but I don't know how to write the script.

Thanks,
CP

file=amwed_0402c-slug~1-cp.jpg
## Save the extension
ext=${file##*.}

## Extract the name without the extension
name=${file%.*}

## Remove the first 12 characters from the name
name=${name#????????????}

## Remove the last three characters
name=${name%???}

## Put the new filename together
newfile=$name.$ext

something like:

# echo "amwed_0402c-slug~1-cp.jpg" | awk '{FS=".";print substr($1,13,length($1)-15)"."$2}'
slug~1.jpg

should work...