An array is just a list in applescript, so you need a 2d array or list of lists in applescript-talk. If you understand applescript text separators, then your task is simply to manipulate the conversion of strings to lists and vice versa. So I wrote you a couple of handlers to make the task easy for you; textToTwoDArray () and twoDArrayToText (). This first example shows how to convert your string to a list of lists using textToTwoDArray ().
NOTE. You should be careful with the line ends in a text file because they can be either carriage returns (character code 13) or a line (character identifier 10). You can see that I used the id 10 character in my code, but if you did not get the correct results, try "13".
set fileText to "family | type Doctor | Pediatrics Engineer | Chemical" textToTwoDArray(fileText, character id 10, " | ") on textToTwoDArray(theText, mainDelimiter, secondaryDelimiter) set {tids, text item delimiters} to {text item delimiters, mainDelimiter} set firstArray to text items of theText set text item delimiters to secondaryDelimiter set twoDArray to {} repeat with anItem in firstArray set end of twoDArray to text items of anItem end repeat set text item delimiters to tids return twoDArray end textToTwoDArray
Here's how to convert a list of lists to your string using twoDArrayToText ().
set twoDArray to {{"family", "type"}, {"Doctor", "Pediatrics"}, {"Engineer", "Chemical"}} twoDArrayToText(twoDArray, character id 10, " | ") on twoDArrayToText(theArray, mainDelimiter, secondaryDelimiter) set {tids, text item delimiters} to {text item delimiters, secondaryDelimiter} set t to "" repeat with anItem in theArray set t to t & (anItem as text) & mainDelimiter end repeat set text item delimiters to tids return (text 1 thru -2 of t) end twoDArrayToText
So now all you have to do is figure out how to read and write to a text file using applescript. Good luck;)
source share