MacOS – AppleScript How to return first n elements of item in list

applescriptmacos

I have never written anything in AppleScript but I recently got a MBP and started playing around with it just to tweak the touchbar. In a downloaded preset I saw this line:

set theArray to every text item of theString

I assume that this line appends the entire string (every single character element) to the array.

How I can choose to append only a fixed n number of characters from that string/item, say maybe the first 10 characters of that string?

Best Answer

Here's an example:

set theString to "Ask Different"
set theArray to text items 1 thru 10 of theString

Result: {"A", "s", "k", " ", "D", "i", "f", "f", "e", "r"}

Since you are new to AppleScript, I encourage you to take the time to read though: AppleScript Language Guide

Just so you know, AppleScript does not have object designated with the name array, it uses list instead. It's a minor technicality however, since you're just starting to learn, its best you know what it's called within the AppleScript Language.

So, instead of using the variable theArray, I'd choose to name it theList, or more specifically I'd add an additional qualifier. For example, theCharList, or whatever is appropriate so as I read through the code I write, I readily know what the variable is for.