【问题标题】:Can Adobe .jsx scripts include other script files?Adobe .jsx 脚本可以包含其他脚本文件吗?
【发布时间】:2019-08-22 02:23:03
【问题描述】:

我们正在编写一堆 .jsx 脚本,在每个脚本中我都必须模拟一些函数,以便我可以使用 Array.map() 和 String.trim() 之类的东西,但我不想必须这样做在每个脚本的顶部包含该代码。

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

【问题讨论】:

标签: scripting adobe adobe-illustrator extendscript


【解决方案1】:

或者您可以简单地在脚本顶部使用#include#includepath 预处理器指令。 您可以在Adobe's "JavaScript Tools Guide"找到详细说明。

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

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

【讨论】:

  • 一直在努力!谢谢:)
  • 我尝试通过 BridgeTalk 将 jsx 文件内容从 Premiere 注入 AfterEffects。这行得通,但如果我尝试以这种方式发送 JSON 库,它就行不通。解决方案是只发送#include "' + pathToScriptFile + '"; 而不是脚本内容。
【解决方案2】:

把这个留给像我这样正在寻找这个的人。在 Adob​​e Illustrator CC 2015 中,我无法让 #include 工作,但 @include 可以。比如:

外部文件:foo.jsx

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

脚本文件:bar.jsx

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

免责声明:我找不到任何文档,但已使用 Adob​​e Illustrator CC 2015 对其进行了测试,并且可以正常工作。

希望这对某人有所帮助。如果有人有任何问题,尽管问!

【讨论】:

  • Adobe CC 2017 (v21.0.0) #include 和 //@include 都适合我。在forums.adobe.com/thread/290259,我看到“//@ 是向后兼容 CS 和 CS2 所必需的。这在 ESTK 章节的 JS 工具指南中有记录。”所以我想#include 更可取。
  • 这会起作用,但是您必须在 manifest.xml 中添加一个 CEFCommandLine 参数以允许文件访问,否则主 JSX 似乎不会加载或评估外部文件!
【解决方案3】:

我们现在使用 Illustrator 中的 $ 帮助器和 $.evalFile() 方法。传递给它一个路径,它会评估文件并返回结果。

我创建了一个小帮手,我可以在我的 .jsx 脚本顶部包含(当然是缩小版),这样我就可以执行Libraries.include("my-other-script"),在我的情况下,这将包括一个位于我的adobe_scripts root 中的文件文件夹,位于名为 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

【讨论】:

【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 2015-07-31
    • 2011-09-21
    • 2020-11-21
    • 2012-07-07
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多