Is it possible to run multiple scripts as functions from one “main” script?

Is it possible to run multiple scripts as functions from one “main” script?

something like,

var returnedData1 = LL.runScript(“my first script”, “data”);

var returnedData2 = LL.runScript(“my other script”, “data”);

I know that it’s, at least, somewhat possible applying self to the function script, which I don’t know how exactly it works but it works, however, when I tried to apply it to multiple scripts as functions, it wasn’t that reliable and LL usually throw me an error of not having one of the functions, which was on a second script called/initialized the same way.

If what’s wrong with the self method could be explained, that would be even better than my first simple suggestion, because, to my knowledge, using the self behaves more like a class, so multiple functions can be added in one script vs having one function per script.

9 Commentsto Is it possible to run multiple scripts as functions from one “main” script?

  1. eval(anotherScript.getCode()). Eval is very powerful, but also error prone.

  2. Lukas Morawietz thanks, also found it on the API under the Script class

    public String getText ()

    Retrieve the script’s text.

    Experimental: it is possible to structure a script library using

    eval(some_script.getText()), at the expense of execution speed.

    isn’t eval() highly discouraged because of the potential to run malicious code?

    at least something like that is what’s described in the MDN

  3. It’s a little bit different in this environment. Normally, js is executed in a web environment, where you do not know what you are evaluating. But in lightning you know exactly what it is that you’re loading.

  4. on my search to try to answer my questions I tried the eval() method, super easy and pretty straight forward, but there’s a few exceptions. The external script can’t have an alert() or return in the main code (outside of a function).

    “my_other_script”

    function external_test() {return “external test”;}

    “main script”

    eval(getScriptByName(“my_other_script”).getText());

    var test = external_test(); //”external test”

    The other method I found (from the repository), was the http://self.myfunction

    this method requires you to “initialize” the main/caller script with the other scripts, exit the script and then run it again. Then the other script will remain “binded” until you restart the phone and then you have to do it again

    I tried to self call the same script after the initialization, but that didn’t worked out, it has to be fully ended then restarted

    the functions in the external script need to be declared with “self” or “this”, I don’t know what’s the exact difference, but the functions declared with “self” need to be called with “self”, whereas the ones declared with “this” can be called with or without it

    “my_other_script”

    self.external_test_a = function() {return “external test a”;}

    this.external_test_b = function() {return “external test b”;}

    “main script”

    if(typeof external_test_a == “undefined”) {getScriptByName(“my_other_script”).run(getActiveScreen(),null);return;}

    var test_a = self.external_test_a(); //”external test a”

    var test_b = this.external_test_b(); //”external test b”

    var test_c = external_test_b(); //”external test b”

  5. magic to make return and alert work:

    eval(“function toEval(){\n”+script.getText()+”\n}”);

    toEval();

  6. Lukas Morawietz in simple terms, what the eval() does in this case, is insert the other script code into the one performing the eval()?

    deciding where to execute it will determine what variables or functions will be the ones defined for the rest of the code in case of duplicates functions or variables?

  7. I’m not sure if js handles duplicate definitions well. You’ll have to test that.

  8. Lukas Morawietz just did, it’s like code insertion (if that’s a thing), duplicates are not handled, just get replaced by the latest code.

  9. I use the eval() in an initial loading script specially to be sure I do not have duplicates.

Leave a Reply

Your email address will not be published. Required fields are marked *