我们正在编写许多.jsx脚本,每个脚本我都必须模拟一些函数,以便可以使用像Array.map()和String.trim()这样的函数,但我不想在每个脚本的顶部包含那些代码。
有没有一种方法可以在.jsx脚本文件中“包含”其他.jsx脚本?
有没有一种方法可以在.jsx脚本文件中“包含”其他.jsx脚本?
或者,您可以在脚本顶部使用#include
和#includepath
预处理指令。您可以在Adobe“JavaScript工具指南”中找到详细描述。
例如,如果您想在.jsx
文件中包含scripts/helper.jsx
:
#include "scripts/helpers.jsx"
// the rest of your code below ...
#include "' + pathToScriptFile + '";
而不是脚本内容。 - Guntram对于和我一样正在寻找这个问题答案的人,我在Adobe Illustrator CC 2015中使用#include
失败了,但是@include
成功了。所以举个例子:
function alertTheWordNo(){
alert('NO');
}
//@include 'foo.jsx';
alertTheWordNo();
只是想在Ike10的回答中添加一个注释。未记录的文档很慷慨 - 这是我写代码20多年来遇到的最糟糕的“文档”。 在主要的JSX文件加载/评估外部文件之前,似乎您必须将CEFCommandLine参数添加到manifest.xml文件中:
<Resources>
<MainPath>./whatever.html</MainPath>
<ScriptPath>./jsx/whatever.jsx</ScriptPath>
<CEFCommandLine>
<Parameter>--allow-file-access</Parameter>
<Parameter>--allow-file-access-from-files</Parameter>
</CEFCommandLine>
</Resources>
我们现在正在使用Illustrator中可用的$
助手和$.evalFile()
方法。将路径传递给它,它将评估文件并返回结果。
我创建了一个小助手,可以在我的.jsx脚本顶部包含(当然是缩小的),这样我就可以执行Libraries.include("my-other-script")
,它将包含一个文件,在我的情况下,该文件位于我的adobe_scripts
根文件夹中,名为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/");
我包含的压缩版本:
/**
* 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/");
请查看此处的代码片段:https://gist.github.com/jasonrhodes/5286526
var _ = require('underscore');
,查看此源代码 https://github.com/theiviaxx/PSLib/blob/master/lib/template.jsx - jkutianski