在运行脚本中自动包含目标文件夹中的所有脚本 - CS6

Automaticly include all scripts from target folder in runned script - CS6

我想 #include 目标文件夹 中的所有脚本动态地 到 运行ned 脚本中。无需每次 写出 每个脚本 name/path

这意味着:
=> 你将任何脚本放入目标文件夹 => 当你 运行 脚本时,来自目标的 scripts/script 会自动包含到运行ned 脚本。

编辑:您还可以从任何范围内的注入代码段调用任何函数或变量。

到目前为止我写了这个脚本:

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" //
}


但是后来我得到了这个错误:



我在第 "Preprocessor directives" 章第 235 页的 javascript tools guide cs6.pdf 中找不到任何合理的解决方案。

我的推理是:
scriptFilesToInclude[i] 结果是字符串,所以它应该被视为 "some path/runned code folder/foo/bar.jsx"。然后将字符串路径加载到脚本中。但我想它不是它,而是试图在 运行ned 脚本的文件夹中找到一个名为:scriptFilesToInclude[i].

的文件

============================================= =================

备注:

我尝试使用 eval:
eval("#include scriptFiles[i]");
还有 JSON.pars():
JSON.parse("#include scriptFiles[i]");
绕过错误,但也没用

在此先感谢您的帮助。

使用$.evalFile()代替eval():

$.evalFile(scriptFiles[i]);

正如@Sergey Kritskiy 之前提到的,它必须使用 $.evalFile() 函数。
但需要在全局范围内调用,才能在任何需要的地方从包含的文件上传变量或函数。

代码:

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" //
}