I’m a Applescript novice and its string processing (such as it is) is pretty much beyond my abilities at this point ( I’m struggling to get out of the C, PHP Javascript mentality where string processing is cake and not nearly as wordy). I have a string (returned from a do shell script) that contains multiple lines. Within each line is a keyword terminated by a “:” followed by a substring representing that keyword value. What I want to do is first seperate the lines so I can process each individually, then I want to create an array of keywords and their respective values for each line. I know how to process the the array once I have it in that format.
I think I need to change Applescript’s text delimiter to an ascii 10 or 13 then some how use that to seperate the lines. Then set the text delimiter to “:” in a repeat to process each line but beyond that I don’t know what to use to actually seperate the lines or each line into an array. Can someone help me on this? TIA.
Wow! Thanks. I was just expecting some pointers. Here is the output.
Each line is terminated by a newline character (not sure if it is ASCII 10 or 13) from a shell script. I need to process each line to put each keyword in an array with its respective value. Then I can process the array in a ‘tell application’ segment.
set thetext to do shell script "blah"
set thearray to textToList(thetext, return)
on textToList(theText, theSep)
set soFar to {}
set textSoFar to theText
repeat until theSep is not in textSoFar
set thePos to the offset of theSep in textSoFar
set thenewPos to thePos
if thenewPos is 1 then set thenewPos to 2
set nextBit to text 1 through (thenewPos - 1) of textSoFar
if textSoFar is not theSep then
set textSoFar to text (thePos + (count of text items in theSep)) through -1 of textSoFar
copy nextBit to the end of soFar
else
set textSoFar to ""
end if
end repeat
copy textSoFar to the end of soFar
return soFar
end textToList
As always, don’t worry about the line endings, just let AppleScript do it for you by using the paragraphs property (this works no matter what the line endings are):
set the_list to (do shell script "blah")'s paragraphs
I found one glitch that I don’t know why it happens or how to fix it. One of the values is an emial address. While ‘textToList’ extracts everything the way I need is seems to be doing extra work that I don’t want. It is stripping out the ‘.’ and ‘@’ from that particular item. So instead of that item being ‘my.name@mydomain.com’ it comes out ‘mynamemydomaincom’. How can I fix this? TIA.
After a little more debugging I found the problem is not with ‘textToList’ but with one of my statements. The following statement is where it strips the '.'s and '@'s:
set value to words 1 thru (cnt-1) of item j of line_array
What I am trying to do is get all but the last word of the string that is the jth item of a list of strings. This statment works except that for some reason it is stripping the aforementioned characters. Suggestions? TIA.