Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 4, 2005, 04:27 PM   #1
iostream.h
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
http://specere.net
iostream.h is offline   0 Reply With Quote
Old May 5, 2005, 04:32 AM   #2
HexMonkey
Administrator
 
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.

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

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Consolidating files with applescript jdtx63 Mac Programming 2 Jun 23, 2011 03:09 PM
sharing files with the guest account iamrawr Mac Basics and Help 1 Feb 5, 2011 05:13 PM
Permanently Delete Files With Applescript noobsauce Mac Programming 7 Dec 30, 2010 12:11 PM
Transferring files with AppleScript?? j26 Mac Basics and Help 5 May 6, 2006 02:25 PM
Consolidating files with applescript iostream.h Mac Programming 1 May 7, 2005 10:21 PM


All times are GMT -5. The time now is 08:49 AM.

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

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