Executing a custom script as a gateway script with hidden parameters

Hello all!

First of all, my apologies for the cryptic title, it’s hard to explain.

I have a scripting funcion that uses two parameters, for example:

system.mymodule.myfunction(Date date1, Date date2)

This funcion uses a project resource to perform some task. When I execute it on the script console it works great because when I create the script module I pass the project from where I’m executing as a parameter (that way the module can gather the resource using the projectName), like this:

//IN DESIGNER HOOK
public void initializeScriptManager(ScriptManager manager) {
        super.initializeScriptManager(manager);

        manager.addScriptModule(
                "system.mymodule",
                new DesignerScriptModule(DesignerHook.getDesignerContext().getProjectName()),
                new PropertiesFileDocProvider()
        );
    }

The problem is that when I add the script module in the gateway, I obviously cannot add a project name since we can have multiple projects in a gw.

//IN GATEWAYHOOK
public void initializeScriptManager(ScriptManager manager) {

        super.initializeScriptManager(manager);
        manager.addScriptModule(
                "system.performanceRatio",
                new GatewayScriptModule(),
                new PropertiesFileDocProvider());
    }

Is there any way to have the gateway know which project set the script up? Of course I can always add another parameter to the function and let the user write the project name but that seems less elegant.

Thanks in advance!

That hook method will be called for each scope in the gateway. It is ugly, but the project will already be set for system.util.getProjectName. You can use the given script manager to execute a one-liner that assigns that to a local variable, and extract that local variable’s value for your use.

There may be a less hacky way to do this.

1 Like

As always, thanks for your answer @pturmel, that is a really clever solution. Do you happen to know how to invoke the funcion system.util.getProjectName from the sdk?

About a less hacky way, I just found the ProjectScriptModule class which sounds perfect for my use case but there is no info other than “A ScriptModule representing a project resource”. If anyone has any info on how to use this it would be greatly appreciatted :slight_smile: !

In most circumstances, we’ll automatically set various values on the com.inductiveautomation.ignition.common.script.ScriptContext class before actually invoking a script. You can use the defaultProject() method on that class.

2 Likes

Works like a charm, thank you both!