skip to Main Content

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:

enter image description here

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


  1. Chosen as BEST ANSWER

    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:

    var runnedScriptPath = $.fileName;
    var runnedScriptFolderPath = getScriptFolder(runnedScriptPath);
    
    var targetScriptsFolderPath = runnedScriptFolderPath + "/foo";
    var targetScriptsFolder = Folder(targetScriptsFolderPath);
    
    var scriptsToInclude = includeScriptsFromTargetFolder(targetScriptsFolder);
    
    for (var i = 0; i < scriptsToInclude.length; i++) { 
        $.evalFile(scriptsToInclude[i]); // <========= this function has to be invoked in global scope
    }
    
    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
                }
            }
        }
    
        return scriptFilesToInclude;
    }
    
    function getScriptFolder(scriptPath) {
    
        return scriptPath.match(/^(.*[\/])/g); // match(/^(.*[\/])/g) "Select everything before the last forward slash" //
    }
    

  2. Use $.evalFile() instead of eval():

    $.evalFile(scriptFiles[i]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search