Script 7

Presentation Notes

The line where we run the search is geting a little unwieldy. Let's use a handler I wrote that wraps around the curl utility and makes our code easier to read.

This handler has a few options, including lettings us save the results to a file, and whether we want to include form-data for POSTs, or just the URL for GETs. Let's use it for now.

The options for URL Access Scripting are very similar. Look at the dictionary for the URL Access Scripting scripting addition to see how to do the same thing. Of course, it will want a file specification, not just a string that lists the path.

Script Source

property searchBase : "http://www.petfinder.com/pet.cgi"

property searchTerms : "action=1&pet.Animal=Dog&pet.Breed=malamute&pet.Age=Adult&pet.Size=&pet.Sex=M&location=NJ"

-- get the source as a string

set sourceHTML to curlSimpleDownload(searchBase, "", searchTerms)

sourceHTML

on curlSimpleDownload(downloadURL, destExpected, theFormInfo)

-- version 1.1, Daniel A. Shockley - public domain

-- downloadURL is STRING

-- saves to destExpected (Mac path as STRING, FILE SPEC, or ALIAS), if given

-- if destExpected is "", returns source result directly as string

-- optional form data for POST - use "" for no form data

try

-- basic download to standard output

set curlCode to "curl \"" & downloadURL & "\""

if (length of theFormInfo) > 0 then

set curlCode to curlCode & " -d \"" & theFormInfo & "\""

end if

-- now, add on the desired file location, if there is one given

if destExpected is not "" then

set unixDestExpected to quoted form of POSIX path of (destExpected as string)

set curlCode to curlCode & " --output " & unixDestExpected & " --write-out \"%{http_code}\""

else -- result as string

set curlCode to curlCode & " | vis" -- pipe into vis to strip nonprintable characters

end if

set curlResponse to do shell script curlCode

return curlResponse

(*

curlResponse will be the http success code ("200"), or an error code.

If no destination was given, curlResponse will be the source

returned, and no file will be saved

*)

on error errMsg number errNum

error "curlSimpleDownload FAILED: " & errMsg number errNum

end try

end curlSimpleDownload