PDA

View Full Version : Question: Parsing a file with Applescript




iostream.h
May 4, 2005, 04:27 PM
Just for the example, say I set a variable to the source of a page in Safari. Now, I want to create an array of values from certain tags in this file.

For instance:

<a href="http://clockworkapple.net/justanexample;SerialNumber=61A5D1GF6B92S4">

Let's say I have about 100 of those throughout the source. What would be the best way to parse out the "61A5D1GF6B92S4" into an array?



HexMonkey
May 5, 2005, 04:32 AM
Here's an example that looks for serial numbers in your example format in a file chosen when running the script. It finds strings that immediately follow a given string (the serialBeginning property) and then adds the beginning of these strings (up to where the serialEnd string is found) to an array.

property serialBeginning : "<a href=\"http://clockworkapple.net/justanexample;SerialNumber="
property serialEnd : "\">"

--Read a chosen file and prepare to search for serialBeginning
set theContents to read (choose file)
set originalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {serialBeginning}

--Split the file into a list of strings that start with serialBeginning
--Ignore the first item, which is just the text before the first occurence
set theItems to text items 2 thru (count of text items of theContents) of theContents

--Iterate through the items to look for serial number terminations strings
set serialArray to {}
set AppleScript's text item delimiters to {serialEnd}
repeat with nextItem in theItems
--Add text before serialEnd to serialArray
set serialArray to serialArray & first text item of nextItem
end repeat
set AppleScript's text item delimiters to originalDelimiters

--serialArray now contains a list of the serial numbers