I’m looking for a way to call a handler in a main script that loaded a script that needs to call it.
– This code is in main script
set loadedScript to (load script ((path to desktop as string) & “loadedScript.scpt” as alias))
tell loadedScript to runLoadedScript()
on mainScript()
display dialog “Main Script Handler”
end mainScript
– This code is in the loaded script
on runLoadedScript()
display dialog “In Loaded Script”
mainScript()
end runLoadedScript
Hello.
A scheme that works, is to set a property when you load the script like this:
script childScript
property caller : missing value
on run
caller's main()
end run
end script
script callerScript
set a to childScript
set a's caller to me
tell a to run
on main()
log "hello"
end main
end script
tell callerScript to run
Can you show that with two separate files and the one loads the other from desktop?
Hello.
here is the child script saved as childscript at the folder, if I had the script clause inside it still,
then I would have to load script childscript of alias …
property caller : missing value
on run
caller's main()
end run
Here is the caller, that are unchanged:
script callerScript
set a to load script alias ((path to desktop folder as text) & "ChildScript.scpt")
set a's caller to me
tell a to run
on main()
log "hello"
end main
end script
tell callerScript to run
Ok this helps, thanks!. Not sure the reason for explicitly using a script block in main script or the run block in the loaded script. This also seems to work…
– Main script
set loadedScript to load script ((path to desktop folder as text) & “childScript.scpt”) as alias
set loadedScript’s mainScript to me
tell loadedScript to run
on hello()
display dialog “Hello”
end hello
– Loaded script file named childScript.scpt
property mainScript : missing value
mainScript’s hello()
I just showed you the easiest way to make it work for you, and I was too lazy to remove the script block from my former example.
I tend to use script blocks, since I then avoid script to share global address-space for their variable, now, that is a technique that also can be utilized: Another way of doing this, could be:
Caller
global main
set main to me
on mHandler()
display dialog "message"
end mHandler
set childScript to load script alias ((path to desktop as text) & "ChildScript.scpt")
tell childScript to run
Child
global main
main's mHandler()
(It would be nice if you bothered to embellish your code within the applescript tags.)
If you follow the parenting the chain you don’t need an variable but only set the parent of the script object. Because you’re unable to set the parent of an loaded script object I put an handler newInstance() in it which will return an script object (this handler is in all my external script files). Here I explain how you can use it properly but the external script file will look like:
External file:
on newInstance(_parent)
script testObject
property parent : _parent
on test()
mainHandler()
end test
end script
end newInstance
Main file:
set theScript to newInstance(me) of (load script file "Macintosh HD:Users:user:Desktop:test.scpt")
theScript's test()
on mainHandler()
#this will be called following the parenting chain
display dialog "hello world!"
end mainHandler
You can send any script object with the handler newInstance() and that object will be the parent or missing value. In this case you send the main script as an argument to set as parent.