Adobe .jsx脚本可以包含其他脚本文件吗?

rho*_*son 4 scripting adobe extendscript adobe-illustrator

我们正在写一堆.jsx脚本,每一个我都要模拟一些函数,所以我可以使用像Array.map()和String.trim()这样的东西,但我不想要包含那些代码在每个脚本的顶部.

有没有办法在.jsx脚本文件中"包含"其他.jsx脚本?

小智 11

或者您可以在脚本顶部使用#include#includepath预处理器指令.您可以在Adobe的"JavaScript工具指南"中找到详细说明.

例如,如果要包含scripts/helper.jsx.jsx文件中:

#include "scripts/helpers.jsx"
// the rest of your code below ...
Run Code Online (Sandbox Code Playgroud)


rho*_*son 0

我们现在使用$Illustrator 中提供的帮助程序和$.evalFile()方法。向其传递一个路径,它将评估该文件并返回结果。

我创建了一个小助手,我可以将其包含在 .jsx 脚本的顶部(当然是缩小版),这样我就可以Libraries.include("my-other-script")在我的例子中包含adobe_scripts根文件夹中名为 .jsx 的目录中的一个文件lib

// indexOf polyfill from https://gist.github.com/atk/1034425
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});

var Libraries = (function (libPath) {    
    return {
        include: function (path) {
            if (!path.match(/\.jsx$/i)) { 
                path = path + ".jsx"; 
            }
            return $.evalFile(libPath + path);
        }
    };
})($.fileName.split("/").splice(0, $.fileName.split("/").indexOf("adobe_scripts") + 1).join("/") + "/lib/");
Run Code Online (Sandbox Code Playgroud)

我包括的缩小版本:

/**
 * Libraries.include(path) -- path must be relative to adobe_scripts/lib/
 * See: https://gist.github.com/jasonrhodes/5286526
 */
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});var Libraries=function(a){return{include:function(b){return b.match(/\.jsx$/i)||(b+=".jsx"),$.evalFile(a+b)}}}($.fileName.split("/").splice(0,$.fileName.split("/").indexOf("adobe_scripts")+1).join("/")+"/lib/");
Run Code Online (Sandbox Code Playgroud)

请参阅此处的要点:https ://gist.github.com/jasonrhodes/5286526