Adobe .jsx脚本能够包含其他脚本文件吗?

7
我们正在编写许多.jsx脚本,每个脚本我都必须模拟一些函数,以便可以使用像Array.map()和String.trim()这样的函数,但我不想在每个脚本的顶部包含那些代码。
有没有一种方法可以在.jsx脚本文件中“包含”其他.jsx脚本?

我意识到这更适合于图形设计的堆栈交换,网址为:http://graphicdesign.stackexchange.com/questions/17007/can-adobe-illustrator-jsx-scripts-include-other-script-files/17014 - rhodesjason
4个回答

18

或者,您可以在脚本顶部使用#include#includepath 预处理指令。您可以在Adobe“JavaScript工具指南”中找到详细描述。

例如,如果您想在.jsx文件中包含scripts/helper.jsx

#include "scripts/helpers.jsx"
// the rest of your code below ...

一直在尝试!谢谢 :) - Chris Sprague
我尝试通过BridgeTalk将Premiere中的jsx文件内容注入到AfterEffects中。 这种做法管用,但是如果我尝试用这种方式发送JSON库,它就不起作用。 解决方案是仅发送 #include "' + pathToScriptFile + '";而不是脚本内容。 - Guntram

3

对于和我一样正在寻找这个问题答案的人,我在Adobe Illustrator CC 2015中使用#include失败了,但是@include成功了。所以举个例子:

外部文件:foo.jsx

function alertTheWordNo(){
  alert('NO');
}

脚本文件:bar.jsx

//@include 'foo.jsx';
alertTheWordNo();

免责声明:我找不到任何相关文档,但我已经在Adobe Illustrator CC 2015上测试过并且可以使用。
希望这能帮助到某些人。如果有任何问题,请随时问!

Adobe CC 2017(v21.0.0)中,#include和//@include都适用于我。在https://forums.adobe.com/thread/290259上,我看到“//@与CS和可能的CS2向后兼容性有关。这在JS工具指南的ESTK章节中有记录。”所以我想#include更可取。 - Andy Swift
这个方法可以运行,但你必须在manifest.xml中添加CEFCommandLine参数以允许文件访问,否则主要的JSX似乎无法加载或评估外部文件! - ineedhelp

0

只是想在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>

0

我们现在正在使用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


1
更好的方法是使用CommonJS for Photoshop中的require。我认为这适用于套件中的所有应用程序。要包含require.jsx,只需使用“#include require.jsx”。 - jkutianski
我该如何使用require在jsx中包含一个js文件?@jkutianski - FabricioG
1
只需使用var _ = require('underscore');,查看此源代码 https://github.com/theiviaxx/PSLib/blob/master/lib/template.jsx - jkutianski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接