Get info based on desktop folders, need to revise an existing script.

At the bottom is a fully working script that i would like to revise.

Currently it prompts for the user to input the week number and which brands to send emails to.
Then it sends out a link to each brand.

I would like to change the way it behaves, rather than prompting the user, all it needs to do is look at the folders of the desktop if the folder contains something then use the folders name and wk number to send the email. if its emtpy then check the next folder.

Folders of the desktop look like this.

Matt_WK23_PSD
and
Martyn_WK22_PSD

where the week number changes week to week.
So the brand in this case are Matt and Martyn.

How might I do this please?

I have this bit that gets the Brand name and the week number

set dtF to paragraphs of (do shell script "ls -F ~/Desktop | grep '/' | cut -d'/' -f1")
set tc to (count dtF)
repeat with i from 1 to tc
          set folderName to item i of dtF --<:  is the folder name, no need to use text item delimiters -->":"
          if folderName does not start with "2_" and folderName does not start with "Hot" and folderName does not start with "Press" and folderName does not start with "Design" and folderName does not start with "Keywords" and folderName does not start with "Season" and folderName does not start with "Sue" and folderName does not start with "Design" then
 
                    set {oldTID, my text item delimiters} to {my text item delimiters, "_WK"}
                    set FolderEndName to last text item of folderName
                    set brandName to first text item of folderName
                    set my text item delimiters to "_PSD"
                    set weekNumber to first text item of FolderEndName
                    set my text item delimiters to oldTID
 
          end if
end repeat

I also have this little bit to get quanity of images. but have no idea how I might get it to function.
I’ve used a try function as the folder may not exist.

tell application "Finder"
		try
			set folderA to (get first folder of desktop whose name begins with "MAR")
			tell application "System Events"
				set contentsA to (number of files in folderA)
			end tell
		on error
			set contentsA to "0"
		end try
	end tell
tell application "Mail" to quit
 
--while mail is closed, add in the default Reply-To key in a UserHeaders dictionary
do shell script "defaults write com.apple.mail UserHeaders '{\"Reply-To\" = \"allphotography@here.com\"; }'"
 
display dialog "Please enter the current week number." default answer ""
set weekNumber to text returned of the result
 
set brandsToUse to choose from list {"BA", "CA", "DA", "ES", "DV", "FR", "IN", "MA", "TM", "WA"} with prompt "Choose brands to send out." with multiple selections allowed
 
-- get the html template
set htmlTemplate to read "Users/matthew/WorkFlow/EMAIL PROJECT/content.html"
considering case
  -- this section splits the template text at every XX, then rejoins it using the week number
          set htmlTemplate to tid(htmlTemplate, "XX")
          set htmlTemplate to tid(htmlTemplate, weekNumber)
end considering
 
tell application "Mail"
          set subjectLine to "Week " & weekNumber & " images"
 
          repeat with thisBrand in brandsToUse
                    considering case
  -- this section adds the brand name, done inside the loop because it's different for each email
  -- the 'my' keyword is needed because you're calling a handler from within a tell block
                              set htmlContent to my tid(htmlTemplate, "*LOGO*")
                              set htmlContent to my tid(htmlContent, thisBrand as list) -- thisBrand is an item in the full list
                    end considering
  -- create a new message.  visible must be false for the 'html content' line to work correctly
                    set newMessage to make new outgoing message with properties {subject:subjectLine, visible:false}
                    tell newMessage
  -- create a variable recipient address based on brand name
  make new to recipient at end of to recipients with properties {address:"All" & thisBrand & "Team@dck.com"}
                              set html content to htmlContent
  -- send the email
  send
                    end tell
          end repeat
end tell
 
on tid(input, delim)
  -- generic handler for doing text item delimiters
          set {oldTID, my text item delimiters} to {my text item delimiters, delim}
          if class of input is list then
                    set output to input as text
          else
                    set output to text items of input
          end if
          set my text item delimiters to oldTID
          return output
end tid
 
tell application "Mail" to quit
 
--wait till Mail is closed, then remove the dictionary
delay 3
do shell script "defaults delete com.apple.mail UserHeaders"
 
 
tell application "Mail" to activate
delay 5
say "All done."

To determine the right folder I would use a regular expression.

do shell script "ls -F $HOME/Desktop | egrep '_WK[0-9]{2}_PSD/'"