Applescript text delimeters

I need to trunctate a file name to rename a JPEG at creation ie

AA00~1234_Rest of heading.pdf

1234.jpg

I have managed it with two underscores or two tildes, courtesy of mg, how can I alter this to allow for the ~ (tilde) and an underscore.

on getChars(t)
set {oTid, text item delimiters} to {text item delimiters, “~”}
set l to text items of t – split text using tilde as the separator, the result is a list of items
if (count l) > 1 then – At least 1 tilde
–set charsAfter1_ to (items 3 thru -1 of l) as text – the text after the 1st tilde
set charsBetween1_ to item 2 of l – the text between the 1st and 2nd tilde
set t to charsBetween1_ --& “~” & charsAfter3_ – concatenate one string
end if
set text item delimiters to oTid – reset the text item delimiters
return t
end getChars
set jpgName to my getChars(mgName)

Thanks for any advice

In this case I’d recommend to use offset rather than text item delimiters

on getChars(t)
	set tildeOffset to offset of "~" in t
	set underscoreOffset to offset of "_" in t
	if underscoreOffset is 0 then set underscoreOffset to (count t) + 1
	return text (tildeOffset + 1) thru (underscoreOffset - 1) of t
end getChars

Here is a short version using multiple Text item delimiters

on getChars(t)
	set {oTid, text item delimiters} to {text item delimiters, {".", "~", "_"}}
	set l to text items of t -- split text using 3 delimiters, the result is a list of items
	set text item delimiters to oTid -- reset the text item delimiters
	return item 2 of l & "." & item 4 of l
end getChars

set mgname to "AA00~1234_Rest of heading.pdf"
set jpgName to my getChars(mgname)

It uses 3 delimiters to break up the text in one shot

@robertfern

It seems that you missed a point.
The original asker want to rename its file as a jpeg.

Would be edited as :

on getChars(t)
	set {oTid, text item delimiters} to {text item delimiters, {"~", "_"}}
	set l to text items of t -- split text using 3 delimiters, the result is a list of items
	set text item delimiters to oTid -- reset the text item delimiters
	return item 2 of l & ".jpg"
end getChars

set mgname to "AA00~1234_Rest of heading.pdf"
set jpgName to my getChars(mgname)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 4 décembre 2018 18:47:57

Thanks to all, they all work and I see what fits into my scenario better