Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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'")
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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"?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
with mac it does not work.
Copy link to clipboard
Copied
Yes, it does (or at least for inDesign, maybe not for photoshop?) see thread included in answer above
Copy link to clipboard
Copied
I don't know how to access site, but I know how to access a server that site is on. It needs to start a path from double slash:
eval('#include //SERWER-NAME/Scripts/Polyfill.jsx')
btw a question to humble r-bin If not to use 'include' like you indicated, then what?
Also the third method I proposed in my last post let you to include other script only at the moment (so when necessary). I mean if you use #include... in any part of script it is going to be seen by script in first instance (irrespectively in which line it exists), but when you use eval('#include...') it's going to trigger it only when code reaches a line it is wrote to. And then if that is precedeed by condition (like in my higher post example) it's either including refered .jsx file or skiping it, so will not make script to work slower.
Anyway what is your method to "exclude" 'include'?
Copy link to clipboard
Copied
I do not understand, that's what you want from me. )
By the way, try to execute your eval ("# include ...") when there are spaces in the path.
Copy link to clipboard
Copied
Actually I missed double quotes there should be used inside of single quotes. It's because in my other scripts I don't use quoted paths directly in single quotes (after '#include '), but variables, so like:
pth = 'some path'
eval('#include ' + pth)
...or something like this. Anyway this way it works (including spaces in names).
That I asked you refered to your answer for my previous post, that I originally answered to, but now I noticed something interesting. I agree '#include...' slows down script as it loads each time other file content, but to avoid it, so loads it only at moment it's necessary, there is easy way to do it by nesting it in eval().
The other question was, what is your way to replace '#include...' to something else that gives the same result?
Copy link to clipboard
Copied
I use $.evalFile in 99.9% of cases.
Copy link to clipboard
Copied
sorry for the files in my google driver that I use at work or other work stations
how can I use them with scripts
I put this path but it does not work?
eval (' #include / / https://drive.google.com/open?id=1wB7rJILvXaCO5FVcGkqEEPbHPpAMFB6X/SCRIPT.jsx')
Where I'm wrong
Copy link to clipboard
Copied
I said I don't know how to access site but a server a site is on (on the condition your computer is in the local web, where both computers can see each other).
Copy link to clipboard
Copied
There is no way to do the one mentioned above
would solve many of my problems
Copy link to clipboard
Copied
Probably you need computer / server IP. Then instead of scripting try to connect that other computer via web using system commands. When you know how then either you can rewrite it for pure extendscript or access that method by extendscript.
Copy link to clipboard
Copied
Thanks for the reply