(* Stock Quote Example Script May 15, 1999 Copyright 1999 Geoff Duncan Pulls stock quote information for a select symbol off Yahoo Finance, using Mac OS 8.6's URL Access. While this script works, it's purely for demonstration purposes, is very quick and dirty, and is hardcoded to Yahoo's HTML output. Naturally, this script will break as soon as they change their markup. Requires: Mac OS 8.6 standard install No additional OSAXen or applications required -- which is a pity, because a number of OSAXEN and utilities could make this simpler. Improvements are left as an exercise for the scripter... Permission is given to redistribute this script non-commercially so long as it remains unaltered. You may also use this script as a basis for further efforts, if you desire. All other rights reserved. *) on run set dlgReply to (display dialog "Get a Yahoo! stock quote for this symbol:" default answer "AAPL" buttons {"Get Quote", "Cancel"} default button 1 giving up after 30) if button returned of dlgReply is "Cancel" then return set daSymbol to allCaps(text returned of dlgReply) -- make a temp file set myFile to ((path to temporary items) as string) & "Stock Quote Script Temp" -- fetch quote page tell application "URL Access Scripting" download "http://finance.yahoo.com/q" to file myFile replacing yes form data "s=" & daSymbol & "&d=v1" quit end tell -- read HTML into a variable open for access file myFile set quoteHTML to (read file myFile to (get eof file myFile)) close access file myFile -- find the lines with the last trade date and last price -- Note: Yahoo changes their HTML format, this needs to change too. set startTag to daSymbol & "" & (ASCII character 10) set daStart to (the offset of startTag in quoteHTML) + (the length of startTag) set daCount to 1 copy "" to lastTrade copy "" to lastPrice repeat if text from character (daStart + daCount) to (daStart + daCount + 1) of quoteHTML is "/t" then if lastTrade is "" then set lastTrade to text from character daStart to (daStart + daCount - 2) of quoteHTML set daStart to daStart + daCount + 5 set daCount to 1 else set lastPrice to text from character daStart to (daStart + daCount - 2) of quoteHTML exit repeat end if end if set daCount to daCount + 1 end repeat -- tell application "Finder" to delete file myFile set lastTrade to stripHTMLTags(lastTrade) if lastTrade contains ":" then -- markets are open, so it's a time set lastTrade to "at " & lastTrade else -- martets are closed, so it's a date set lastTrade to "on " & lastTrade end if display dialog "Yahoo says " & daSymbol & " last traded " & lastTrade & " at $" & stripHTMLTags(lastPrice) & "." with icon 1 buttons "OK" default button 1 giving up after 30 end run on allCaps(daText) -- very messy capitalization routine copy "" to newText repeat with i from 1 to the count of character in daText set curChar to character i of daText set curNum to ASCII number curChar if (curNum > 96) and (curNum < 123) then set newText to (newText & (ASCII character (curNum - 32))) as string else set newText to (newText & curChar) as string end if end repeat return newText end allCaps on stripHTMLTags(HTML) -- slow, and fails on bad HTML that doesn't use entities for < and > -- but, requires no OSAXen and is good enough for now. set intag to false set newText to "" repeat with i from 1 to count of character in HTML set curChar to character i of HTML if curChar is "<" then set intag to true if not intag then set newText to (newText & curChar) as string if curChar is ">" then set intag to false end repeat return newText end stripHTMLTags