Script 1

Presentation Notes

The web is a great source of information. You can go to a web page and look up almost anything. Unfortunately, your computer can't really read. If you want to have an AppleScript program that can find out what the current weather is, see if your favorite sports team won last night, or track a stock you've invested in, you need to tell AppleScript where to look and how to read the results. That's what we'll do tonight.

Here is a short script that runs a search on Yahoo's search engine.

We have two properties. Properties are very useful, but for now let's just call them a value the script keeps, and that can be used anywhere in the script. One is the base of the search URL for Yahoo's search form, the other is what we are looking for. We combine those two strings into one, then use a command-line utility in OS X named curl to get the result. To call curl we use the AppleScript command do shell script. Note a few things: we use the quoted form of searchURL so that any characters that have special meaning in "the shell" are passed on to curl only. Also, we use a shell tool called "pipe" to send the results through another utility called vis. All that vis does is translate any characters that do shell script cannot turn back into an AppleScript string. Many sites won't send those characters, but this one does, unfortunately.

I'm using curl for this presentation because it allows you to get the results back directly, rather than forcing you to write them to a file. In OS 9, you could use URL Access Scripting to do very similar things, although curl can do much more. I'll show you how to do this later.

A few important points:

1. You need to find the base URL you need.

2. Some sites don't allow this way of accessing their site (although it is hard to stop). Google recently made it difficult (there may be a way around, but you're violating their "terms of service") to download search results directly, without a web browser. Curl can mimic web browsers in sophisticated ways, but the Terms of Service are Google's prerogative.

3. The search engine you wish to use may use the POST method, rather than GET (using GET means the search terms are in the URL, using POST means they are attached apart from the URL). We will look at how to do a POST search later.

Right now, all we're getting is the raw HTML source code of the page that the site is giving back. If we were viewing this in a web browser it would be much easier to read. [EXTRA CODE]

NEXT

Script Source

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

property searchTerms : "applescript+user+group"

set searchURL to searchBase & searchTerms

-- is "http://search.yahoo.com/bin/search?p=applescript+user+group"

-- curl is a utility that downloads what you ask it to get

-- get the source as a string

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

sourceHTML