Determining if a script is called or run directly...

Hello,

I want to make a couple of scripts be callable from other scripts, but also runnable on their own. These scripts, and their caller take the time to set up the machine context (loading helpers, images, etc) and then tear it down at the end, and I don’t want to do this twice.

Is there a way to determine if a script is running on its own or has been called from somewhere else?

Thanks!

Allen

Yes, a script can find out how it was called by examining the list returned by the CallStack() function. In your case it seems like it might be more straightforward to just set a global variable when you do the setup, like:

set global setupDone to true

and then check it before doing the setup again:

if not global setupDone then setupContext

But if you prefer to use the CallStack, it returns a list of “stack frames” (property lists) containing information about each frame. The last item in the list represents the current script or handler. The previous item is the script or handler that called it. This will show you the name of the calling handler:

if the penultimate item of the callstack is not empty
then put the Handler of the penultimate item of the CallStack

Try using “the callstack”. If a handler is called the callstack will be greater than 1. The callstack will be 1 if the handler was invoked on its own.

Thanks guys!

You’re right Doug, it’s probably best to use a flag. You just wanted to use the penultimate operator :wink: .

LOL – you caught me! :lol: