AppleScript. Split the string into parts by the separator character.

This code does not make sense, I would just like to get information based on it.

set MyData to "MyData1|MyData2"
on MyScript(MyData)
	set MyVar1 to "MyData1"
	set MyVar2 to "MyData2"
	return MyVar1 & "|" & MyVar2
end MyScript

The text is passed in the code: "MyData1|MyData2". I need to divide it into two parts by the "|" symbol. And write each part into two variables: myVar1 and myVar2. How to do it?

P.S. The problem is that only one parameter can be passed to on...end. I need to pass two. I'll pass one, then split it into two.

Answered by red_menace in 761526022

Text item delimiters are what you are looking for, for example:

set myString to "MyData1|MyData2"
set {myVar1, myVar2} to splitString(myString)

to splitString(someString)
	try
		set tempTID to AppleScript's text item delimiters -- save current delimiters
		set AppleScript's text item delimiters to "|"
		set pieces to text items of someString -- split the string
		set AppleScript's text item delimiters to tempTID -- restore old delimiters
		set firstPart to item 1 of pieces
		set secondPart to item 2 of pieces
	on error errmess -- delimiter not found
		log errmess
		return {firstPart, ""} -- empty string for missing item
	end try
	return {firstPart, secondPart}
end splitString

Text item delimiters are what you are looking for, for example:

set myString to "MyData1|MyData2"
set {myVar1, myVar2} to splitString(myString)

to splitString(someString)
	try
		set tempTID to AppleScript's text item delimiters -- save current delimiters
		set AppleScript's text item delimiters to "|"
		set pieces to text items of someString -- split the string
		set AppleScript's text item delimiters to tempTID -- restore old delimiters
		set firstPart to item 1 of pieces
		set secondPart to item 2 of pieces
	on error errmess -- delimiter not found
		log errmess
		return {firstPart, ""} -- empty string for missing item
	end try
	return {firstPart, secondPart}
end splitString
AppleScript. Split the string into parts by the separator character.
 
 
Q