Also note that there might be times where an item is not on the list, for example:
Red Apples: 45
Big Oranges: 23
Small Oranges: 34
Green Small Bananas: 34
I guess what I’m really after is an applescript replacement for the terminal’s grep command, so I could grep for a specific keyword and then get the variable value using text item delimiters.
Looks simple enough. Awhile ago when I was using AppleScript Studio I had this method to search and replace strings inside of strings. I think you can find something useful in it:
on searchAndDeleteStringFromString_theStringToSearch_theStringToDelete(theStringToSearch, theStringToDelete)
set OldASItemsDelimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to theStringToDelete
set theStringToSearchCleanedOfStringToDelete to text items of theStringToSearch as list
set AppleScript's text item delimiters to OldASItemsDelimiter
set theStringToSearchCleanedOfStringToDelete to (theStringToSearchCleanedOfStringToDelete) as string
return theStringToSearchCleanedOfStringToDelete
end searchAndDeleteStringFromString_theStringToSearch_theStringToDelete
The secret is the AppleScript’s text item delimiters. The only thing you need to absolutely observe is to preserve the previous text item delimiter and restore it once you are done. In this method the variable OldASItemsDelimiter does the trick.
So in your case the first thing you need to do (inside a repeat loop for each paragraphs) is to set the ASTID to a colon plus the space after it. Then you get the two separated text items from the previous one like this (untested):
--assumes you have set the variable listOfItems to the text you need to parse
set listOfItems to "Red Apples: 45
Big Oranges: 23
Small Oranges: 34
Tiny Green Tomatoes: 56
Green Small Bananas: 34"
set OldASItemsDelimiter to AppleScript's text item delimiters
set parsedListOfFruits to {}
repeat with currentParagraph in paragraphs of listOfItems
set AppleScript's text item delimiters to ": " --that's a colon and a space
set theSeparatedTextItems to text items of currentParagraph as list
set fruitTextItemsToParse to (item 1 of theSeparatedTextItems) as text
set fruitNumberOfItem to (item 2 of theSeparatedTextItems) as number
set AppleScript's text item delimiters to " " --that's a space
set fruitTextItems to text items of fruitTextItemsToParse as list --will divide the
set parsedListOfFruits to parsedListOfFruits & {{fruitTextString:fruitTextItemsToParse as text, fruitTextItemsAsList:fruitTextItems, fruitNumberOfItem:fruitNumberOfItem}}
end repeat
set AppleScript's text item delimiters to OldASItemsDelimiter
Then afterward to retrieve any item from the list parsedListOfFruits you call it either individually by index like this:
set fruitNumber to (item |fruitNumberOfItem| of item 1 of parsedListOfFruits) as number
in which you just need to change item 1 for the index you need the data from. Or if you need to find one specific item inside the list you can do it like this:
--for example let's say you are looking for the first occurrence of the word Apples in the list and get back the number associated with it
set wordToSearch to "Apples"
set fruitNumberItem to 0
repeat with currentItem in parsedListOfFruits
if (item |fruitTextString| of currentItem) contains wordToSearch then
set fruitNumberItem to (item |fruitNumberOfItem| of currentItem) as number
exit repeat --we can stop looking, saving some time especially if the list is very long
end
end
Of course these are just examples, it all depends one what you need in the end, as I don’t know that I just provided generic code for you to get started.
Does this help?
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)