GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Call one script from another

I am evaluating Directory Opus, and writing my first script function.

Looking ahead, I shall want to avoid code repetition – is it possible to call another script from an OnClick function, so that I can put my common code into separate script file(s) somewhere? If so, where would I need to put those scripts? (I am writing in VBScript, but am happy to use another language if that is the only way.)

Assuming you have some script code which you want to trigger (maybe in slightly different ways) from various buttons, the usual thing to do is create a script add-in which adds its own command.

You can then run the command (and pass it different arguments to change what it does) from normal (i.e. non-script) buttons. Scripts can also run commands added by other scripts, of course, but that's rarely needed.

Here's an example: https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/Adding_a_new_Internal_Command.htm

Thank you for your prompt reply. Internal commands look interesting and I shall investigate them.
For my current needs, however, I don't think that they will do what I want. I was envisaging something like this (very simple) example:

Function OnClick(ByRef clickData)
   dim result
   result = MultiplyByThree(2)
End function

Another script somewhere:

Function MultiplyByThree(ByVal i)
   MultiplyByThree = i * 3
End function

I don't want to duplicate the code everywhere I need it, so I want to put code like MultiplyByThree in a separate script so I can call it from multiple OnClick functions (or even, OnClick calls Sub Foo in another script, which in turn calls MultiplyByThree).

I would put everything into one script add-in and have it add multiple commands for the different buttons to call (or one command with args, or a mix of the two, depending on how related different buttons are). Each command can then use functions in the same script.

Windows has ways for scripts to include other scripts, but they're a bit of a pain to use and also potentially use more memory and slow down startup vs only including the things you need in each script or combining everything into a single-script add-in:

If you have need for something that a lot of scripts might find useful, we could consider adding it to the scripting API that Opus provides.

Thank you. I am obviously not the first person to want to write a library of reusable code. I shall read that link very carefully and learn from others' experience.