rename files in multiple folder script

I am trying to make a script that will rename files in 2 seperate folders that end with H.jpg to W.jpg

Is the best way to run the routine on each folder seperately or can i run them together somehow?

I am having trouble with the offset command
I am sure it is something simple

attached is my script

set the source_folder1 to “Macintosh HD:Users:jbradfield:Desktop:Move to Thumbnail:”
set the source_folder2 to “Macintosh HD:Users:jbradfield:Desktop:Move to Enlarged:”

set the item_list to list folder source_folder1 without invisibles
set source_folder1 to source_folder1 as string
repeat with i from 1 to count item_list
set this_item to item i of the item_list
set this_item to (source_folder1 & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info
if current_name ends with “H.jpg” then
set the_offset to the (offset of “H.jpg” in current_name) as number
set the name of this_item to (characters 1 thru (the_offset - 1)) of current_name & “W.jpg”
end if
end repeat

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

I think you’re making your life much harder than it needs to be.

A much simpler solution would be something like (untested):

set source_folders to {"Macintosh HD:Users:jbradfield:Desktop:Move to Thumbnail:" , "Macintosh HD:Users:jbradfield:Desktop:Move to Enlarged:" }

tell application "Finder"
   repeat with aFolder in source_folders
      set filesToRename to every file of folder aFolder whose name ends with "H.jpg"
      repeat with eachFile in filesToRename
         set name of eachFile to ( characters 1 through -5 of (name of eachFile) & "W.jpg")
      end repeat
   end repeat
end tell

The tricks here are to use a whose clause to identify the files to rename (“every file… whose name ends with H.jpg”), then iterate through the list.
Additionally, since you know you’re removing the “H.jpg” at the end, you can use “characters 1 through -5” of the original file name. The “-5” works from the end of the filename, which is easier than trying to work some variable number of characters from the beginning.

Thank you very much for simplifying this for me
I tested the script and it returned:
. . .
“Finder got an error: Can’t get characters 1 thru -5 of name of document file “AB3623_S002H.jpg” of folder “Move to Thumbnail” of folder “Desktop” of folder “jbradfield” of folder “Users” of startup disk.”

i tried to play with it but still reports error
any ideas?

set source_folders to {“Macintosh HD:Users:jbradfield:Desktop:Move to Thumbnail:”, “Macintosh HD:Users:jbradfield:Desktop:Move to Enlarged:”}

tell application “Finder”
repeat with aFolder in source_folders
set filesToRename to (every file of folder aFolder whose name ends with “H.jpg”)
repeat with eachFile in filesToRename
set thename to name of eachFile as string
set Newname to ((characters 1 through -6 of (thename) & “W.jpg”) as text)
set name of eachFile to Newname
end repeat
end repeat
end tell

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan