Passing a Handler at Runtime
Passing a Handler at Runtime
- Subject: Passing a Handler at Runtime
- From: Rick Gordon <email@hidden>
- Date: Tue, 13 Sep 2011 13:14:45 -0700
Title: Passing a Handler at Runtime
I am looking for a way to further generalize the script below
which is already generalized to use an external script containing a
handler, in order to be able to choose the script AND the handler at
runtime.
Right now, the line ...
set vScriptName to "HANDLER-Set Object Style
Properties.scpt"
... could be replaced with user input, but I'm not sure how to
accomplish that with the line ...
set vHandler to vScript's fSetObjectStyleProperties
I'd appreciate some input here. Thanks.
Rick Gordon
------------
--A script to process user-chosen chapters of the active InDesign
book document with an arbitrary external script
set vList to {}
try
set vScriptName to "HANDLER-Set Object Style
Properties.scpt"
set vScript to my fLoadScript(vScriptName)
set vHandler to vScript's fSetObjectStyleProperties --the token of the
desired handler, in this case
on
error
error "Can't load a
script."
end
try
tell application "Adobe InDesign
CS5"
set vApp to it
set vOldUserInteractionLevel to user interaction
level
of
script preferences
set user interaction level of script
preferences to never interact
set
vTargetList to my fChooseChapters(active
book)
if
vTargetList is {} then error "Nothing was
chosen."
repeat with vItem in
vTargetList
set vChapterAlias to full name of vItem
set vCurrentDoc to open vChapterAlias
without showing window
tell vCurrentDoc
if my fPreprocess(it)
then
set vProcessResult to my fProcess(it, vHandler)
set end of vList to {name, vProcessResult}
set end of last item of vList to
my
fPostprocess(it)
else
set end of last item of vList to
my
fPostprocessFail(it)
end if
set end of last item of vList to return & return --for
readability
end tell
end repeat
set user interaction
level
of
script preferences to vOldUserInteractionLevel
return vList
end
tell
on fChooseChapters(aBook)
tell
application "Adobe InDesign CS5"
set vContents to a reference to book
contents of aBook
set vContentsList to name of vContents
tell me to set
vReturnedNames to (choose from list vContentsList with
title
"Open Selected Chapters" default items
vContentsList with multiple selections allowed
without empty selection allowed)
set vChapterList to items of vContents where name is
in
(vReturnedNames as text)
return vChapterList
end
tell
end fChooseChapters
on fLoadScript(aScriptName)
--assumes this script and
the script containing the handler are in the same folder
set
vScriptPath to path to me
tell
application "Finder"
set vScriptFolder to (container of vScriptPath) as alias as text
end tell
set vScriptPath to (vScriptFolder &
aScriptName) as alias
return load script
vScriptPath
end fLoadScript
on fPreprocess(aDoc)
tell aDoc
return true --a stub placeholder to allow for a
preprocessing step
end tell
end fPreprocess
on fProcess(aDoc, aHandler)
script sPassHandler
property pHandler : aHandler
return pHandler(aDoc)
end script
run
sPassHandler
end fProcess
on fPostprocess(aDoc)
try
tell aDoc
save
close
end tell
return true
on
error
return false
end try
end fPostprocess
on fPostprocessFail(aDoc)
tell aDoc
close without saving
end tell
return -1
end fPostprocessFail
--
___________________________________________________
RICK GORDON
EMERALD VALLEY GRAPHICS AND CONSULTING
___________________________________________________
WWW: http://www.shelterpub.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden