I really need expert help here.
How can I make a handler and how to call it, so that it wraps around a few code lines in the main script and then passes the result to the script for further processing. For instance:
-- A script snippet
-- ==============
-- do many things .
set myPBtitle to "title"
set myPBheader to "header"
set myPBiconPath to ""
tell application "Finder"
activate
set myFolderPath to choose folder
set myFolderContainer to container of myFolderPath as string
set myFolderName to (name of myFolderPath) as string
set myFolderPOSIX to quoted form of POSIX path of myFolderPath
set myDmgNamePOSIX to quoted form of ((POSIX path of myFolderContainer) & myFolderName & ".dmg")
-- make the dmg file.
tell current application
do shell script "hdiutil create -srcfolder " & myFolderPOSIX & " " & myDmgNamePOSIX
end tell
open (myFolderContainer & myFolderName & ".dmg")
end tell
-- do more things .
-- Progress Bar Handler
-- ===================
on ProgressBarHandler(myPBtitle, myPBheader, myPBiconPath)
tell application "SKProgressBar" -- StefanK's app at http://macscripter.net/viewtopic.php?id=36409
set floating to true
set position to {600, 550}
set width to 500.0
set title to myPBtitle
set header to myPBheader
set header alignment to left
set footer to "100%"
set footer alignment to right
set image path to myPBiconPath
tell progress bar
set minimum value to 0.0
set maximum value to 100.0
set current value to 0.0
end tell
set show window to true
tell progress bar
activate
set indeterminate to false
start animation
(* === Here goes the code to be handled ===*)
repeat 10 times
increment by 10.0
delay 1
end repeat
stop animation
end tell
quit
end tell
end ProgressBarHandler
The code to be handled in this example is
tell current application
do shell script "hdiutil create -srcfolder " & myFolderPOSIX & " " & myDmgNamePOSIX
end tell
I know that I could make a handler specific for this script:
-- A script snippet
-- ==============
-- do many things .
set myPBtitle to "title"
set myPBheader to "header"
set myPBiconPath to ""
tell application "Finder"
activate
set myFolderPath to choose folder
set myFolderContainer to container of myFolderPath as string
set myFolderName to (name of myFolderPath) as string
set myFolderPOSIX to quoted form of POSIX path of myFolderPath
set myDmgNamePOSIX to quoted form of ((POSIX path of myFolderContainer) & myFolderName & ".dmg")
-- insert the handler to make the dmg file with a progress bar.
my SpProgressBarHandler(myPBtitle, myPBheader, myPBiconPath, myFolderPOSIX, myDmgNamePOSIX)
open (myFolderContainer & myFolderName & ".dmg")
end tell
-- do more things .
-- Specific Progress Bar Handler
-- ===========================
on SpProgressBarHandler(myPBtitle, myPBheader, myPBiconPath, myFolderPOSIX, myDmgNamePOSIX)
tell application "SKProgressBar" -- StefanK's app at http://macscripter.net/viewtopic.php?id=36409
-- the first part of the progress bar handler code
tell current application
do shell script "hdiutil create -srcfolder " & myFolderPOSIX & " " & myDmgNamePOSIX
end tell
-- the second part of the progress bar handler code
end tell
end SpProgressBarHandler
but, I want a vanilla Progress bar handler, callable from my library of handlers to handle any process which could benefit from a progress bar.
An alternative solution, which just occurred to me while writing this, is to write a vanilla progress bar handler with a variable where the code to be handled goes, pointing to a local handler containing the code to be handled. Kind of handler with a nested handler:
-- A script snippet
-- ==============
-- do many things .
set myPBtitle to "title"
set myPBheader to "header"
set myPBiconPath to ""
set ProcessToBeHandled to (my MakeDmgHandler(myFolderPOSIX, myDmgNamePOSIX))
tell application "Finder"
activate
set myFolderPath to choose folder
set myFolderContainer to container of myFolderPath as string
set myFolderName to (name of myFolderPath) as string
set myFolderPOSIX to quoted form of POSIX path of myFolderPath
set myDmgNamePOSIX to quoted form of ((POSIX path of myFolderContainer) & myFolderName & ".dmg")
-- insert the handler to make the dmg file with a progress bar.
my VanProgressBarHandler(myPBtitle, myPBheader, myPBiconPath, myFolderPOSIX, myDmgNamePOSIX)
open (myFolderContainer & myFolderName & ".dmg")
end tell
-- do more things .
-- Vanilla Progress Bar Handler
-- ==========================
on VanProgressBarHandler(myPBtitle, myPBheader, myPBiconPath, myFolderPOSIX, myDmgNamePOSIX, ProcessToBeHandled)
tell application "SKProgressBar" -- StefanK's app at http://macscripter.net/viewtopic.php?id=36409
-- the first part of the progress bar handler code
ProcessToBeHandled
-- the second part of the progress bar handler code
end tell
end VanProgressBarHandler
-- Make dmg Handler
-- =================
on MakeDmgHandler(myFolderPOSIX, myDmgNamePOSIX)
tell current application
do shell script "hdiutil create -srcfolder " & myFolderPOSIX & " " & myDmgNamePOSIX
end tell
end MakeDmgHandler
Is any of this possible? Can somebody please help me with a clever handler solution?
Cheers,
Chris