Looking for TIPS on breaking script into handlers

1) Looking for any advice and tips on breaking my script down into handlers.

I definitely realize I am reusing segments over In different places in my script that I could breakout into Seperate handlers.

This handler would get passed 3 variables. {list1, list2, string3}
In my previous usage depending on what string3 was it would process the data differently,
sometimes creating a 4th string Variable to be used.

2) question:

2A) should I create 2 new seperate handlers then.

2A-H1)
On evaluateSTRING3{string3}
–evaluates string3 variable.
–some if/thens
– creates new string4 variable
return string4
End evaluateSTRING3

Then I can simpfly the new main handler so that it just processes
2A-H2)
On newMAINHANDLER(list1, list2, string4)
–process the data
–without any if ands or buts (hehe)
End newMAINHANDLER


3) Now how should I best “call” these handlers?

3A-H3) with a 3rd separate handler

set theMAINRESULTS to my processTHEDATA (list1, list2, string3)

On processTHEDATA (list1, list2, string3)
set string4 to my evaluteSTRING3(string3)
set theResults to my newMAINHANDLER(list1, list2, string4)
return theResults
end processTHEDATA

or 3A-H2-B insert the evaluteSTRING3 handlers into the newMAINHANDLER

set theMAINRESULTS to my newMAINHANDLERV2 (list1, list2, string3)

On newMAINHANDLERV2(list1, list2, string3)
set string4 to my evaluteSTRING3(string3)
–process the data with string4
–without any if ands or buts (hehe)
End newMAINHANDLERV2

or 3B in my main script have 2 separate call lines

set string4 to my evaluteSTRING3(string3)
set theMAINRESULTS to my newMAINHANDLER (list1, list2, string4)


4) Overall is there any advantage / disadvantage to create Handlers that just call other handlers?

5) This just came to me… is there anyway to have a handler execute a list of handlers passed to it?

for my script I’m seeing there being 3 main operations that my script will do for them.
I want to offer them the Choose from List. Of on of those 3 processes. Then based on that
selection can I pass a handlers a list of handlers to execute?

property handlerSET1 : {my setupVARHANDLER1, my getDATAHANDLER1, my processDATAHANDLER1, my selectDATAHANDLER1, my processDATAFURTHER1, my showRESULTS}

property handlerSET2 : {my setupVARHANDLER2, my getDATAHANDLER2, my processDATAHANDLER1, my selectDATAHANDLER2, my processDATAFURTHER1, my showRESULTS}

property handlerSET3 : {my setupVARHANDLER2, my getDATAHANDLER3, my processDATAHANDLER1, my selectDATAHANDLER3, my processDATAFURTHER2, my showRESULTS}

set fullHANDLERSET to (handlerSET1, handlerSET2, handlerSET3)

set pickOPERATION to choose from list {“Process 01”, “Process 02”, “Process 03”}

set theTORUNSET to pickOPERATION

my runaLISTOFHANDLERS(theTORUNSET)

this would be awesome if possible.

??? as a script object ???
tell myrunaLISTOFHANDLERS to theTORUNSET

  1. You should create handlers based on subject/task and to avoid redundancy.

  2. Give them a proper name so it is immediately clear what the handler does. processData() is a bad example because processing nor data means anything. Better naming is parseCSV() for example, it’s clear that csv(data) content is parsed(processing).

How complete the naming must be depends also on the structure of your code. Are you going to use one big fat library or different libraries for different subjects? For instance the parseCSV() example could be simple parse() if the handler is inside an CSV script object for example. The calling code would look like CSV’s parse() which is self explanatory.

In the Foundation framework (which is written in C) there is only a global namespace. Functions (handlers) names like CFBundleGetValueForInfoDictionaryKey are very long but when you read it between all other C code it explains what this function does. A long name can be very useful, don’t try to keep it short as possible per se.

  1. From a security point of view recursive handlers are bad (handlers who calls self) because every programming language has an limited stack size (AppleScript little less than 300). Handlers calling other handlers is good and are in general more efficient.

  2. You can set an handler to an variable and call that handler from the variable. Unfortunately it’s only usable within the same script object. So settings handlers in a list, loop through the list and call each handler is possible as I’ll show with the example below:

set theHandlers to {handler1, handler2, handler3}

repeat with i from 1 to count theHandlers
	set h to item i of theHandlers
	h()
end repeat

on handler1()
	display dialog "handler1"
end handler1

on handler2()
	display dialog "handler2"
end handler2

on handler3()
	display dialog "handler3"
end handler3

Question regarding Script Object’s as I’m just learning about them, understanding them.
Say I have a number of handlers I’ve made that are specific to a particular task.
Say let’s call them “Search In Playlists”.

Should I create a script object and call it “searchINPLAYLISTS” and then include my
variables that are particular to that and my handlers within that script object?
This would definitely help me organize my code more and make it more understandable for
myself to call the functions… and then even utilize this code in other scripts?

very cool thanks for the example.

one other question I have is, often from code examples or dictionaries etc. often see stuff like:

set theResults to aList of aRecord … aDic etc

is it ok to leave these “standard names” in the handlers as long as those variables aren’t
global variables or properties? I’ve been been renaming all of mine just to be safe.
Like such:

on findNODES(xPathMaster)
set theFNRESULTS to …
end findNODES

I’m guessing I don’t need to do this?

thanks

You could create an script object iTunesPlaylists (playlist is still a very broad name) and create an handler search() in it. Every handler or property related to iTunes playlists should be inside the script object.

Just give local variables names you’re comfortable with. Only use an strict naming convention for public functions and properties.

Hi, I once did a script so big dad i used blocks of code that repeated several times! it was a nightmare so what I did was create a 2 databases using FileMaker one for the blocks and one to set the order and reuse of the blocks. so I just called the blocks by creating a new record adding the block ID and I was done :lol: … at the end I just copied all records and pasted in the editor adn saved it as an app:cool:. hope it helps!

PS I found using Nigel’s asOBJc version from here very handy
https://macscripter.net/viewtopic.php?id=46008

I was able to pick my handlers and then see what code it contained
And also any subhandlers that were called

Very handy