Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old May 4, 2005, 04:27 PM   #1
macrumors regular
Join Date: Mar 2004
Location: Albuquerque, NM
Send a message via AIM to iostream.h
Question: Parsing a file with Applescript

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?
Macintosh Applications
iostream.h is offline   0 Reply With Quote
Old May 5, 2005, 04:32 AM   #2
HexMonkey's Avatar
Join Date: Feb 2004
Location: 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.

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
HexMonkey is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript: How do I scan an image to a file? moonman239 Mac Programming 2 May 11, 2014 04:40 PM
Opening a file in AppleScript Chemical666 Mac Programming 2 May 7, 2014 06:47 PM
Formatting text file in AppleScript dam316 Mac Programming 1 May 5, 2014 10:21 AM
Applescript to Change File Permissions hfthomp Mac Programming 1 Oct 25, 2012 02:19 PM
Resolved: Applescript: List/txt.file question h1068 Mac Programming 5 Jul 3, 2012 06:30 PM

Forum Jump

All times are GMT -5. The time now is 05:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC