Script 3

Presentation Notes

So, we need to replace those spaces with Ôplus signs' to allow the search to run. I threw in my replaceSimple() handler here. It's very helpful to have reusable chunks of code saved somwhere. Other languages may call them functions or procedures. I found myself having to replace characters often, and so I wrote a simple handler. It uses a very fast method of changing characters based on AppleScript's text item delimiters. For now, let's just use it to get the job done. You can read more about it, or someone here can help later, if needed.

Now if we run this, we get the expected results.

So, now we can get raw HTML source code. What next? We could learn how to try to "parse out" the part we want, or I could show you how to POST forms. Let's cover POST first, then the parsing routine will apply to both. The POST example will also be more fun to read results from.

Script Source

property searchBase : "http://search.yahoo.com/bin/search?p="

property searchTerms : ""

set searchTerms to text returned of Â

(display dialog "Enter your search terms:" default answer "")

set searchTerms to replaceSimple(searchTerms, space, "+") -- change spaces to pluses

set searchURL to searchBase & searchTerms

-- get the source as a string

set sourceHTML to (do shell script "curl " & quoted form of searchURL & " | vis")

sourceHTML

on replaceSimple(thisText, oldChars, newChars)

-- version 1.1, Daniel A. Shockley http://www.danshockley.com

-- 1.1 coerces the newChars to a STRING, since other data types do not always coerce

-- (example, replacing "nine" with 9 as number replaces with "")

set oldDelims to AppleScript's text item delimiters

set AppleScript's text item delimiters to the oldChars

set the parsedList to every text item of thisText

set AppleScript's text item delimiters to the {(newChars as string)}

set the newText to the parsedList as string

set AppleScript's text item delimiters to oldDelims

return newText

end replaceSimple