【问题标题】:How can I create the resource string without a big string?如何在没有大字符串的情况下创建资源字符串?
【发布时间】:2021-11-29 17:31:25
【问题描述】:

在 After Effects 脚本中,如果您希望脚本能够停靠在程序的工作区中,据我所知,唯一的方法是使用这样的资源字符串:

var res = "Group{orientation:'column', alignment:['fill', 'fill'], alignChildren:['fill', 'fill'],\
    group1: Group{orientation:'column', alignment:['fill', ''], alignChildren:['fill', ''],\
        button1: Button{text: 'Button'},\
    },\
}";
                
myPanel.grp = myPanel.add(res);

上面的代码创建了一个脚本 UI,其中一个按钮(“button1”)在一个组(“group1”)内。

我想知道创建相同资源字符串的其他方法。是否可以使用 JSON 对象然后对其进行字符串化??

我知道它可以通过某种方式完成,因为我检查了可停靠的 Duik Bassel 脚本,例如,添加了如下元素:

var button1 = myPal.add( 'button' );

但我自己不明白该怎么做。

TL;DR:我想制作一个可停靠的 scriptUI,而不是一次编写一个巨大的字符串,而是一点一点,就像一个浮动脚本。

【问题讨论】:

    标签: scripting adobe extendscript after-effects adobe-scriptui


    【解决方案1】:

    UI 容器元素有一个 add() 方法,允许您向其中添加其他 UI 元素,并且您可以将它们视为普通对象。

    var grp = myPanel.add("group");
    grp.orientation = "column";
    grp.alignment = ['fill', 'fill'];
    grp.alignChildren = ['fill', 'fill'];
    
    var group1 = grp.add("group");
    …
    var button1 = group1.add("button");
    button1.text = 'Button'
    

    更多细节和例子在这里:https://extendscript.docsforadobe.dev/user-interface-tools/types-of-controls.html#containers

    还值得一试https://scriptui.joonas.me/,它是一个可视化的 scriptUI 界面构建器。您必须对它生成的代码做一些工作才能获得 AE 面板,但这并不难。

    【讨论】:

    • 哇,好用!!!非常感谢您的回答。我知道 scriptUI builder 但它仅适用于不可停靠的脚本。如果我错了,请纠正我。
    • 它生成的代码是不可停靠的,但是那里有解释如何将它们变成可停靠的代码。
    • 谢谢@stib。 scriptUI 构建器实际上具有内置功能!我刚刚找到它:imgur.com/a/hEWlW6o
    • 哦,很好的发现。我想知道这是否是新增内容。
    【解决方案2】:

    extendscript 仍然使用 20 世纪版本的 JavaScript,它没有内置 JSON,但我已经成功地使用了 JSON polyfill。

    我使用json2.js 将结构化数据输入和输出 Illustrator,它运行良好,但我可以看到现在有一​​个 json3.js,无论出于何种原因,它可能会更好。 This stackoverflow question 解决了差异。

    要将另一个 .js 文件(例如 polyfill)加载到您的脚本中,您需要执行类似的操作

    var scriptsFolder = (new File($.fileName)).parent; //  hacky but effective
    $.evalFile(scriptsFolder + "/lib/json2.js"); // load JSON polyfill
    

    这些文件位置在其他 Adob​​e 应用程序中可能有所不同。不确定 AfterEffects 中的内容。我似乎记得 InDesign 有不同的脚本位置。当然,您也可以对路径进行硬编码。

    祝你好运!

    【讨论】:

    • 感谢您的回答。我其实很想知道如何构造res字符串的方法。例如,我可以使用哪些命令创建新按钮或组等。为了将 json2 加载到我的脚本中,我已将库转换为包含在我的脚本中的单行,因此没有额外的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多