How to pass execution to different hander in calling script

I have an applet which I’m trying to restructure (main.scpt is large and is slow to edit in Script Debugger, which sometimes crashes). I have lifted some handlers from “main.scpt” and placed them in another script in the bundle “loaded_script.scpt”. I have then loaded the loaded_script.scpt file. So, instead of going to one of its own handlers, execution in main.scpt goes to the relevant handler in loaded_script.scpt. That works well. This is a prototype of what is working:

Applets’ main.scpt:

get_the_data()
on get_the_data()
	set folder_path to path to me as string
	set path_to_script to (folder_path & "Contents:Resources:Scripts:loaded_script.scpt") as string
	set theScriptInside to path_to_script as alias
	set run_this to load script file theScriptInside
	set data_returned to run_this's run_this_handler(folder_path)
	display dialog data_returned
end get_the_data

and in loaded_script.scpt I have:

on run_this_handler(folder_path)
	set data_to_be_returned to "This has been returned " & folder_path
end run_this_handler

But, for my applet to work, I need execution to return to a different handler. So, the prototype main.scpt has this:

 get_the_data()
on get_the_data()
	set folder_path to path to me as string
	set path_to_script to (folder_path & "Contents:Resources:Scripts:loaded_script.scpt") as string
	set theScriptInside to path_to_script as alias
	set run_this to load script file theScriptInside
	set data_returned to run_this's run_this_handler(folder_path)
end get_the_data
on return_to_this_handler()
	display dialog "Called by theScriptInside"
end return_to_this_handler

I have got that to work by loading main.scpt into loaded_script.scpt. So loaded_script.scpt has:

on run_this_handler(folder_path)
	set data_to_be_returned to "This has been returned " & folder_path
	set return_to_different_handler to load script file folder_path
	return_to_different_handler's return_to_this_handler()
end run_this_handler

This redirection works. But it’s at the expense of loading all of main.scpt into loaded_script.scpt. main.scpt in my applet is over 3,200 lines. Is there another way to redirect execution to a handler different to that which called the loaded script ?

I have looked at specifying a parent-child relationship between the two scripts but all my attempts at that have failed. For example, I can’t figure out how to make a parent-child relationship between two script files. Even if that worked, I don’t know if it will do what I need without effectively loading all 3,200 lines of the parent’s code into the child so it can call a handler in the parent.

Thanks.

Garry

Model: iMac (late 2009) Core i5, macOS X 10.15.7
Browser: Safari 14.0.2
Operating System: Other

If you use script libraries instead of load script, AppleScript caches a single copy of each library for your app. So calling between libraries dosen’t entail loading multiple times. (You’d need to put more of your main script in a library, by the sound of things — but given it’s size, that’s probably a sensible thing to do anyway.

The only wrinkle is packaging the libraries at export time: you need to make the libs .scptd files, and put links in each bundle so the libraries can find each other when part of an applet bundle. You can find a script that does this linking over on the Late Night Software Web site. (It should also happen automatically in Script Debugger 8.)

This may not be what the OP wanted, but I think the OP just need to build the main script differently:


set {run_this, folder_path} to get_libraryScript() of me
return_to_this_handler(run_this, folder_path) of me

on get_libraryScript()
	set folder_path to path to me as string
	set path_to_script to (folder_path & "Contents:Resources:Scripts:loaded_script.scpt") as string
	set theScriptInside to path_to_script as alias
	set run_this to load script file theScriptInside
	return {run_this, folder_path}
end get_libraryScript

on return_to_this_handler(run_this, folder_path)
	set data_returned to run_this's run_this_handler(folder_path)
	display dialog data_returned
end return_to_this_handler

KniazidisR, at first I thought it wouldn’t help but actually it did. The handlers in loaded_script.scpt can return a variable to main.scpt which indicates where processing should go. It’s a kludge but, it works. Many thanks.

Yes, I should do that in the long run. I’ve got around the inter-library communication problem by always copying the libraries into ~/[home]/library/script libraries. It seems odd that of all the scripts inside a bundle, only main.scpt can see libraries located within the bundle. I’ve found that other scripts can’t see any resource including icon files for which I’ve had to pass the file path in the call.

One of the things about AS that I’ve not found yet is how it handles memory. For example, the command:

set some_text to localized string "Here is some text."

Now that the variable some_text has been initialised and has a value read from the Localizable.strings file, does AS read the Localizable.strings file again when that command is executed again or does AS just get the value from memory ? But, that’s a whole other topic.

Cheers.

If you see it somewhat kludge, then you can try passing the results directly to other handler, this way:


my return_to_this_handler()

on get_libraryScript()
	set folder_path to path to me as string
	set path_to_script to (folder_path & "Contents:Resources:Scripts:loaded_script.scpt") as string
	set theScriptInside to path_to_script as alias
	set run_this to load script file theScriptInside
	return {run_this, folder_path}
end get_libraryScript

on return_to_this_handler()
	set {run_this, folder_path} to my get_libraryScript()
	set data_returned to run_this's run_this_handler(folder_path)
	display dialog data_returned
end return_to_this_handler

My guess is that the relevant .strings file is usually cached somewhere. Looking up localized resources from .strings files is something most apps do zillions of times.