Page 1 of 1
Including files
Posted: Mon Aug 11, 2014 6:33 pm
by gabrielp
Is there any way to include scripting files? For example, if I had a function that I wanted to use in two different scripts, is there anyway to import those somehow? Something like a use or include/require statement?
Thanks in advance
Including files
Posted: Mon Aug 11, 2014 7:51 pm
by dkelly
The only way is to read file into string and call eval().
Including files
Posted: Mon Jan 19, 2015 3:14 pm
by gabrielp
Dwight,
Did some more thinking and I really need this functionality. For example, let's say in the AWS scripting project, I want a function that tests to see if AWS CLI is installed and accessible from Switch, which returns an error on failure. I wouldn't want to copy that functionality in each scripting configurator because if I make any updates to the function, I'd have to go back and do it in a bunch of places. Not to mention some of these scripts are kind of nuts to have all in one file.
I could have a separate repo of core scripts which each could eval() from. For example, if you clones the core repo to C:/core/ and then each script would open the file 'C:/core/core.js' and eval() the contents of that file, that would work, but I want everything to be contained in the script configurator itself.
So, since .sscript files are essentially just .zips, could you hack multiple JavaScript files into that zip, possibly update the manifest if needed, then within the script, read the files within the .sscript itself? Somehow we'd need to figure out if its possible to find the path of the script itself and then modify it to find the other included scripts. Does this sound at all possible?
Including files
Posted: Mon Jan 19, 2015 6:12 pm
by freddyp
I did an experiment quite some time ago with reading a file and eval()-ing it: no luck, I could not get it to work. A simple javascript expression works, but when you start putting functions in it (which you will undoubtedly do) it does not work.
Squeezing multiple pieces of code into the sscript file is also not going to work. Yes, it can contain multiple pieces of code, even in different languages, but only one is executed.
An include function is certainly a very useful extension of the scripting functionality, but it will have to be done by engineering. The feature request exists. If everybody who is interested posts his/her mail address I will add them to the feature request.
Freddy
Including files
Posted: Mon Jan 19, 2015 6:23 pm
by gabrielp
freddyp wrote: An include function is certainly a very useful extension of the scripting functionality, but it will have to be done by engineering. The feature request exists. If everybody who is interested posts his/her mail address I will add them to the feature request.
Good to know. I'd love it if you could add me to that request. My email is
gabrielp@shawmutprinting.com
Thanks
Including files
Posted: Thu Jan 22, 2015 10:33 am
by ArielRauch
Please add me to the request too:
ariel.rauch@gmail.com
Re: Including files
Posted: Tue Feb 24, 2015 5:00 pm
by jugganaut
I'm also interested in this:
john@midtownprinting.com
Re: Including files
Posted: Tue Mar 31, 2015 9:36 am
by MoodyBro
freddyp wrote:I did an experiment quite some time ago with reading a file and eval()-ing it: no luck, I could not get it to work. A simple javascript expression works, but when you start putting functions in it (which you will undoubtedly do) it does not work.
You have to define the function as a variable!
For example:
Code: Select all
myPlus = function(a,b) {
job.log(2,"Function: myPlus");
return a+b;
}
To use it in your script, you have to open and execute the file with eval. After that, you can use your function in the normal way:
Code: Select all
// PSEUDO-HEADERFILES
// Developed by Marc Listmayr <marc@loopart.at>
// Copyright 2015 -- All rights reserved
// open and read the included 'header'-file
eval(File.read(s.getPropertyValue(["headerpath"]), "UTF-8"));
// use function 'myPlus' and write it into the log
job.log(2,myPlus(6,4));
Re: Including files
Posted: Fri Apr 03, 2015 8:33 pm
by jskibbie
MoodyBro, thanks for that info. I'll have to try that out.
Freddy, I've requested this before, and I'm still interested in this feature. Please add me:
jskibbie@schawk.com.
Thanks.