Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [241]

By Root 1556 0
1 of window "search"

set textSought to my urlEncode(content of cell 1)

set titleSought to my urlEncode(content of cell 2)

set authorSought to my urlEncode(content of cell 3)

end tell

if length of textSought < 5 and length of titleSought < 5 and ¬

length of authorSought < 5 then

beep

return

end if

doTheSearch( )

end clicked

on urlEncode(what)

set text item delimiters to "+"

return (words of what) as string

end urlEncode

Example 27-5 is a utility hander, feedbackBusy, purely for manipulating the interface to provide some user feedback. We're going to be talking to the Internet by way of curl, and while we're doing this, nothing will be happening. The user might think that the application is idle or broken. Therefore we spin the progress indicator and disable the Search button to give the user a sense that the application is busy and that he should keep his hands off while it does whatever it's doing. The handler is called with a boolean parameter telling whether to begin or end this feedback. Once again there's a use of call method to make up for a deficiency in the bridging: here, we force the window to update its display so that the disabled or enabled Search button will look disabled or enabled.

Example 27-5. The feedbackBusy handler

on feedbackBusy(yn)

tell window "search"

if yn then

set enabled of button 1 to false

start progress indicator 1

else

set enabled of button 1 to true

stop progress indicator 1

end if

call method "display" of it

end tell

end feedbackBusy

Example 27-6 shows the doTheSearch handler. This should seem familiar, being nearly unchanged from the code in Chapter 25. The main differences are:

The post argument now incorporates values from the three different form fields the user is allowed to fill out.

We ask for 2,000 articles instead of 20. The reason is that we're hoping to capture the titles of all the found articles. The search was originally constructed so that its results could be displayed in a web page, where you're supposed to find the first 20 results, then ask for another page showing the next 20, and so forth. I originally thought of trying to emulate this in our application. But then it struck me that we've got this nice scrolling table view to play with, and displaying a large number of titles is no problem, so we may as well gather lots of them in one search and be done with it.

Interface feedback is provided to the user through calls to feedbackBusy before and after the call to curl.

The call to curl now has a few more parameters—for example, we provide some timeout values, because the TidBITS search server can be rather slow.

The intermediary file is now located in the temporary items directory, where the user won't see it (it will be deleted automatically when the user logs out).

Error handling has been added. It's primitive—if there's a problem, we beep—but this is enough to prevent any mysterious error messages from appearing before the user's eyes. The problem will usually be either that no results were obtained from the search or that the search was never run because we couldn't connect to the server; in real life it might be nice to distinguish these cases and to provide nice error messages, but this is left as an exercise to the reader (meaning that I was too lazy to do it myself).

Example 27-6. The doTheSearch handler

on doTheSearch( )

local d, f, r

set d to "'-response=TBSearch.lasso&-token.srch=TBAdv"

set d to d & "&Article+HTML=" & textSought

set d to d & "&Article+Author=" & authorSought

set d to d & "&Article+Title=" & titleSought

set d to d & "&-operator"

set d to d & "=eq&RawIssueNum=&-operator=equals&ArticleDate"

set d to d & "=&-sortField=ArticleDate&-sortOrder=descending"

set d to d & "&-maxRecords=2000&-nothing=MSExplorerHack&-nothing"

set d to d & "=Start+Search' "

set u to "http://db.tidbits.com/TBSrchAdv.lasso"

set f to (POSIX path of (path to temporary items)) & "tempTidBITS"

feedbackBusy(true)

try

do shell script ¬

"curl -s --connect-timeout 25 -m 120 -d " & d & " -o " & f & "

Return Main Page Previous Page Next Page

®Online Book Reader