explode_ignoring() [split/delimit text]

This handler will “explode” a string (using text item delimiters) while ignoring content inside another delimiter. (It was created for this thread: Ignore Delimiter Inside Quotes)

on explode_ignoring(someDelimiter, someText, ignoreInsideThis)
	local someDelimiter, someText, ignoreThis, explosion, ASTID, thisItem
	
	set explosion to {}
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {ignoreInsideThis}
	set someText to text items of someText
	set AppleScript's text item delimiters to {someDelimiter}
	get count someText
	
	repeat with i from 1 to result
		if (i mod 2) is 1 then -- only process odd-numbered items
			get text items of someText's item i
			
			repeat with thisItem in result
				tell thisItem's contents to if it is not "" then set explosion's end to it
			end repeat
		else
			set explosion's end to someText's item i
		end if
	end repeat
	
	set AppleScript's text item delimiters to ASTID
	return explosion
end explode_ignoring

-- Example
explode_ignoring(",", "22,\"Hello, World!\",a", "\"")
--> {"22", "Hello, World!", "a"}

See also: explode()