text delimiter usage

If I have a variable read from a FileMaker file cell like “2035, 3476, 8921” or “Dog, Cat, Horse” - how do I convert this into a list instead of text or a 1 item list?

I am sure it involves text item delimiters but I have totally forgotten how to do it.

Getting old stinks.

Thanks in advance for your help.
Oakley

Model: 10.9.5 OSX on new iMac
AppleScript: Script DeBugger 5.0.9
Browser: Safari 537.85.10
Operating System: Mac OS X (10.9)

set theNumbers to split("2035, 3476, 8921", ", ")
set theAnimals to split("Dog, Cat, Horse", ", ")

on split(str, sep)
	tell AppleScript
		set oldTIDs to text item delimiters
		set text item delimiters to sep
		set textItems to text items of str
		set text item delimiters to oldTIDs
	end tell
	return textItems
end split

Thank you this works great. You don’t know how much I appreciate you help on this.
Thank you again.
Oakley

This would work without Text item delimiters :

words in "Dog, Cat, Horse" --> {"Dog", "Cat", "Horse"}

@Bernamo

Words doesn’t behave the same according to localization.
Using text item delimiters give the same behavior worldwide.
It’s why it’s a better scheme than using words.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) mercredi 27 juillet 2016 16:41:58

In addition to what Yvan said, when we put a two worded animal in there it will fail:

words in "Dog, Cat, Horse, Komodo Dragon" --> {"Dog", "Cat", "Horse", "Komodo", "Dragon"}

Thanks to Yvan and to you for highlighting the limit of my solution.
But, when used with one-value data’s, without space(s), I like this pretty one liner script; words in, that’s it !

Anyway, with a small loop it works fine too :


set {str, j, split} to {"Dog, Cat, Horse, Komodo Dragon", 1, {}}
set L to length of str

repeat with i from 1 to L
	if text i of str = "," or i = L then
		set {split, j} to {split & text j thru (i - 1) in str, i + 2}
	end if
end repeat
split

Here an optimized and better version. When you read the end of the string you should copy all characters, including the last one. Your version did not.

set str to "Dog, Cat, Horse, Komodo Dragon"
set len to length of str
set l to 1
set r to 1
set split to {}

repeat until r > len
	if text r of str = "," then
		set split to split & text l thru (r - 1) in str
		set r to r + 2
		set l to r
	else
		set r to r + 1
	end if
end repeat

if l is not r then set split to split & text l thru (r - 1) in str

return split

p.s. merging multiple setters into one-liners doesn’t make your code faster, so therefore I removed it to keep better readability.

Adding a final comma to the string resolves the missing character => set str to str & « , »

Here making it more readable, as you rightly suggest :


set str to "Dog, Cat, Horse, Komodo Dragon"
set str to str & "," -- ends string with comma
set L to length of str

set split to {}
set j to 1

repeat with i from 1 to L
	if text i of str = "," or i = L then
		set split to split & text j thru (i - 1) in str
		set j to i + 2
	end if
end repeat
return split

Hm . OK the « Text item delimiters » solution - I hate, like all beginners - is nice, I agree after all. Thanks for sharing !

While logically the best way to handle (manipulate) strings is character by character. However the higher you go in programming languages all kind of automated tasks are executed for you. With AppleScript the amount of overhead of handling a single character is so massive it is like asking for a banana but you get a gorilla holding a banana for you and the entire jungle (a quote from Joe Armstrong about lack of reusability in OOP languages compared to functional programming). With all this implicit and unwanted code to be executed by AppleScript you end up with a cumbersome solution using text item delimiters that is in fact more efficient because you avoid that “Jungle” as much as you can. What Dijkstra said about BASIC is partly true for AppleScript as well: “it mutilates your mind beyond recovery.”.

While I completely understand the hatred against text item delimiters we have to be aware that AppleScript is for the hobbyist, the efficient desktop user and not for the professional programmer. With that in mind the language is actually quite good with all its quirks and the reason is simple: you don’t need to be a programmer to write working code.