Skip to main content
Known Participant
March 23, 2011
Question

evalFile vs //@include

  • March 23, 2011
  • 3 replies
  • 8043 views

What to use when, and why?


Some background: I'm currently working on a panel in Adobe Configurator which calls scripts via links in a html widget.

Basically it looks like this:

var scriptFile = new File(app.path + '/Presets/Scripts/myscript.jsx');

$.evalFile(scriptFile, 30000);

myFunction(args);

I'm wondering when the following is preferred:

//@include myscript.jsx

myFunction(args);

This topic has been closed for replies.

3 replies

carlp33822801
Inspiring
September 20, 2018

Is there anyway to include a remote file that isn't on your computer? Say from a site such as "http://remote-site.com/script.js"?

JJMack
Community Expert
Community Expert
September 20, 2018

It would not be posible via HTML  using a UNC may work but is not a good idea IMO. For the users machine would need to be authorized to use(share) the remote file

JJMack
Geppetto Luis
Legend
September 20, 2018

I started my own thread and got a nice fix if anyone is interested: Include remote .js-file in script-file (and use it's variables)

ps. sorry for high-jacking thread, just got a bit desperate and this thread was closest to what I was after.


with mac it does not work.

Kukurykus
Legend
December 16, 2017

There is 3rd way to do it:

1) I wasn't happy with $.evalFile() as you have to put inside brackets full path to to the file, even if this file is in the same folder of your working script, for example you are running first script with a path /d/scripts/someScript1.jsx which uses:

$.evalFile('/d/scripts/someScript2.jsx')

This examplary path is short, but if that was longer then it looked ugly and that wasn't handy to repeat entire path.

2) the kind of solution is using then //@include or #include. For example #include 'someScript2.jsx'. Unfortunately you can't write any code in the same line whether it's started with # or //@ chracters. Additionally when you want to use if / else if / else statements then you have to put it in separate line of their blocks, so:

if (true) {

     //some code

     //@include 'someScript2.jsx'

}

if you only include some other file then you can do:

if (true) //@include 'someScript2.jsx'

but it won't work with #include, so you had to write that in separate block line, whaterver there is more code before 'include' or not:

if (true) {

     #include 'someScript2.jsx'

}

3) the solution I found is to combine methods one and two, so you can use it in one line with other code as well as put only name of a file you want to run:

if (true) /* some code, */ eval("#include 'someScript2.jsx'")

Legend
December 16, 2017

IMHO:

Using include unnecessarily increases the execution time of the script by loading and compiling code that is not needed at the moment, and which may not even be needed.

Kukurykus
Legend
December 16, 2017

Yes I know it's slower, but it's unnoticable. Or else I had to see some tests I believe they shows it slower, but how much slower? If that was problem during my job I would use other method. Probably for some kind of scripts it's not good to use it, but with some of them increased time doesn't make difference. So it depends on what you use them for. But yes there are some situations that automation in range of few hours of a work day takes few minutes more... and if you can't run your script on another machine while working on yours then waiting for end of process can be really boring thing

Inspiring
July 26, 2013

Hi Reimund,

Just came across this post so I'm not sure if you've recieved an answer on this but here are my thoughts.  If your only goal is to include the file, they can be used pretty well interchangeably.  However, there are a few advantages to using $.evalFile() over //@include. 

For one, you can make the call to evalFile conditional by wrapping it in an if statement.  That way, you could set flags or test for a certain state within the app (# of layers, a certain named layer, etc.) before importing.  With this, you can make your script that much more modular by potentially loading different scripts based upon different criteria as below.

if(parentLayerExists) {

     $.evalFile("parentLayerScript.jsx");

} else {

     $.evalFile("childLayerScript.jsx");

}

The 2nd advantage of evalFile is that it can be wrapped in a try/catch block and you can actually require the imported file to load without error.  If the external script you load has errors and doesn't load, then later in the importing script...when you try and access the external scripts functionality, your script will fail.  Although not necessary (since the ultimate outcome is a script that doesn't run), you can safe guard against this and have your script fail gracefully.  It also means that whatever your script does to your PSD (or AE comp or AI file, whatever you're scripting in) doesn't half execute...leaving it in a tenuous state.  And, safeguarding/failing gracefully is just a good programming practice.

try {

     $.evalFile("scriptThatFails.jsx");

} catch(e) {   

     //Uh oh! The script we rely on didn't work, no need to continue;

     //I could also alert the user that an error occurred or even what that error was.

     alert("ERROR: "+e.message);

     return false;

}

That might be more than you want/need to think about for your script...but hopefully that helps!