AppleScript_ The Definitive Guide - Matt Neuburg [220]
set d to d & "&Article+HTML=" & t
set d to d & "&Article+Author=&Article+Title=&-operator"
set d to d & "=eq&RawIssueNum=&-operator=equals&ArticleDate"
set d to d & "=&-sortField=ArticleDate&-sortOrder=descending"
set d to d & "&-maxRecords=20&-nothing=MSExplorerHack&-nothing"
set d to d & "=Start+Search' "
set u to "http://db.tidbits.com/TBSrchAdv.lasso"
set f to POSIX path of file ((path to temporary items as string) & "tempTidBITS")
do shell script "curl -d " & d & " -o " & f & " " & u
set perlScript to ... --where is the Perl script?
set r to do shell script "perl " & perlScript & " " & f
set L to paragraphs of r
set half to (count L) / 2
set L1 to items 1 thru half of L
set L2 to items (half + 1) thru -1 of L
set choice to (choose from list L2) as string
repeat with i from 1 to half
if item i of L2 is choice then
open location (item i of L1)
exit repeat
end if
end repeat
The only unsettled question is the line in the middle of the AppleScript code where the variable perlScript must be set to the POSIX pathname of the Perl script file. If we happen to know the name and location of this file, then of course the problem is solved and we can just hardcode this information into the AppleScript code; for example :
set perlScript to POSIX path of file ((path to desktop as string) & "perlScript.pl")
But this seems a rather fragile approach, as we are relying on an important file to be in a certain location; the file could accidentally be moved or renamed. This situation is just the sort where the script bundle format comes in handy (see "Compiled Script Files" in Chapter 3). We'll put the Perl script file inside the AppleScript compiled script file's bundle; the user will never see it, the two files won't be accidentally separated, and the AppleScript code will know where the Perl file is.
Create the AppleScript compiled script file using a script editor application, and save it as a script bundle (let's call it searchTidBITS). Create the Perl script in some other way (using a text editor such as BBEdit), save it as parseHTML.pl, and then move it into Contents/Resources inside searchTidBITS—you can do this manually using the Show Package Contents command in the Finder, or you can use Apple's Script Editor, which represents Contents/Resources in the Bundle Contents drawer of the script window (you can drag parseHTML.pl directly from the Finder into that drawer). The missing line of our code will then read:
set perlScript to quoted form of POSIX path of (path to resource "parseHTML.pl")
Note, when testing, that this script won't work when run from Apple's Script Editor, because path to resource in that context looks inside the Script Editor bundle, not the script bundle. Instead, use Script Debugger for testing, or else put the script bundle into your ~/Library/Scripts folder and run it from the Script Menu. (In Chapter 27 we'll take this approach one step further with AppleScript Studio: we'll build the Perl script into the application bundle and we'll wrap a nicer interface around our AppleScript code.)
Osascript
Three command-line tools are provided for accessing AppleScript from Unix—osalang, osacompile, and osascript—of which osascript is the most important. So let's talk about the other two first.
osalang lists the scripting components present on your machine (see "The Open Scripting Architecture" in Chapter 3):
% osalang -l
ascr appl cgxervdh AppleScript
scpt appl cgxervdh Generic Scripting System
If you have other OSA components installed, they will also appear. (For example, if you use Script Debugger, you'll see AppleScript Debugger X and JavaScript.) The two four-letter codes identifying each component are used by OSA programmers, but typically won't arise in the context of your AppleScript experience. Then comes a series of flags describing the capabilities of this scripting component (see the osalang man page for their meanings). Finally, we have the name of the component. The "Generic Scripting System" is the general front end to the OSA (what Chapter 3 calls the generic