Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iostream.h

macrumors regular
Original poster
Mar 4, 2004
231
0
Albuquerque, NM
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

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
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.

Code:
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.