I want to #include
all scripts from target folder dynamically in the runned script. Without any need of writing out each script name/path, each time.
That’s mean:
=> you drop any script into target folder => when you run script, scripts/script from target are automatically included to the runned script.
EDIT: You can also invoke any functions or variable from injected piece of code in any scope.
I wrote this script so far:
var runnedScriptPath = $.fileName;
var runnedScriptFolderPath = getScriptFolder(runnedScriptPath);
var targetScriptsFolderPath = runnedScriptFolderPath + "/foo";
var targetScriptsFolder = Folder(targetScriptsFolderPath);
includeScriptsFromTargetFolder(targetScriptsFolder);
function includeScriptsFromTargetFolder(targetFolder) {
var targetFolderFiles = targetFolder.getFiles();
var scriptFilesToInclude = new Array;
for (var i = 0; i < targetFolderFiles.length; i++) {
if (targetFolderFiles[i] instanceof File) {
if (targetFolderFiles[i].toString().match(/.jsx$/)) { // .jsx is extension from Adobe's scripts
scriptFilesToInclude.push(decodeURIComponent(targetFolderFiles[i]) ); // decodeURIComponent method is used becouse path is URl
}
}
}
for (var i = 0; i < scriptFilesToInclude.length; i++) {
#include scriptFilesToInclude[i]; // <===== this line gives error!
}
}
function getScriptFolder(scriptPath) {
return scriptPath.match(/^(.*[\/])/g); // match(/^(.*[\/])/g) "Select everything before the last forward slash" //
}
But then I got this error:
I couldn’t find any reasonable solution in javascript tools guide cs6.pdf in chapter “Preprocessor directives”, page 235.
My reasoning is:
scriptFilesToInclude[i]
outcome is string, so it should be treated as "some path/runned code folder/foo/bar.jsx"
. And then string path be loaded to the script. But instead of it, I suppose it tried to find a file in runned script’s folder named: scriptFilesToInclude[i]
.
==============================================================
Notes:
I tried to use eval:
eval("#include scriptFiles[i]");
And also JSON.pars():
JSON.parse("#include scriptFiles[i]");
To bypass error, but it didn’t work either.
Thanks in advance for any help.
2
Answers
As @Sergey Kritskiy has mentioned earlier it has to be used
$.evalFile()
function.But it is needed to be invoked in global scope, to has abilities to upload variable or function from included file anywhere where you need.
Code:
Use
$.evalFile()
instead ofeval()
: