Batch Processing made easy
Batch Processing made easy
- Subject: Batch Processing made easy
- From: Richard 23 <email@hidden>
- Date: Sun, 12 Nov 2000 23:51:59 -0800
This has got to be the most useful one-handler wonder I've come up
with in a long time and haven't seen it done anywhere else so thought
I'd pass it along.
Often a handler is useful enough to be called once or several times
in a row from several different locations within a script. It's easy
enough to incorporate batch behavior into the handler itself (using a
repeat loop). But if there's more than one handler that this applies
to, you end up adding similar code for each handler.
Not any more!
What follows is an example using the BatchProcess handler I came up with.
It calls AskQuestion twice, once in single-item mode, the other in batch.
Then a brief description of how it works:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
on run
AskQuestion("Wanna Play?")
if result is not equal to "yes" then play sound "laugh" -- Jon's
Commands
AskQuestion({"your name", "your quest", "airspeed velocity of an
unladden swallow"})
end run
-- ----------------------------------------------------------------------
-- ask a single question, or annoy the user with several in a row
-- ----------------------------------------------------------------------
on AskQuestion(theThing)
if theThing's class = list then
BatchProcess(AskQuestion, theThing)
else
display dialog "What is " & theThing & "?" default answer ""
result's text returned
end if
end AskQuestion
-- ----------------------------------------------------------------------
-- Richard 23 showed me how! <
http://homepage.mac.com/richard23/>
-- ----------------------------------------------------------------------
on BatchProcess(theHandler, theList)
global theRef
set {theRef, myResult} to {theHandler, {}}
repeat with theItem in theList
try
theRef(theItem's contents)
on error errTxt number errNum
log "Error " & errNum & ": " & errTxt
missing value
end try
set end of myResult to result
end repeat
myResult
end BatchProcess
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do you want to play?
--> "no"
What is your name? (etc)
--> {"Richard 23", "I seek the Grail", "Dunno"}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
First, to use BatchProcess in a handler that takes a single argument:
* insert a check to see if the argument's a list
* if it is, call BatchProcess with a reference to the handler
(the handler's label without parentheses) and the argument.
* the returned result is a list of results (missing value
for each call that produced no result
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Brief Description of the odd bits (warning: kinda advanced):
NOTE: I'll put a more detailed discussion of how this works and how
to use handler references to one's advantage on my site so the
propellorheads can read all the details in glorious html (rather
than droll email) and I won't put everyone else to sleep.
It'll be up tomorrow if not tonight (web address in the script)
Getting a reference to a handler is easy. Just figuring out what
to do with it isn't. It's not really covered in the books.
It's possible to get a reference to a handler by invoking its name.
BatchProcess
--> <<handler BatchProcess>>
Once assigned to a variable, the handler can be called without using
the handler's name (or label or whatever you prefer to call it).
Instead, the handler is called using the ** variable's name **.
In order for this to work, the variable needs to be declared global
even if you don't use it anywhere else in the script.
Since a variable is local to the handler unless declared global, it
can't "see" beyond the handler in which it's contained. So trying
to call another handler using a local variable doesn't work.
And although the reference is passed to BatchProcess in its argument,
you can't call the handler using the argument variable's name.
"Parameter" (argument) variables are always local and cannot be
declared otherwise. That's why 'theHandler' gets assigned to 'theRef'.
I think the rest is pretty straightforward.
Hope someone else finds this as useful as I did tonight!
R23