Indesign 2020 ScriptUI 添加文本框

Indesign 2020 ScriptUI add text frame

我的 Indesign 2020 脚本有问题。
我不是程序员,也不知道 javascript(我只是创建了一些简单的脚本)。
我想创建一个带有添加文本框的按钮的简单面板,但似乎不起作用。
这是代码

    var document = app.activeDocument;
    document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
    app.activeDocument.zeroPoint = [0,0];
    var Labelpanel = new Window("dialog");
    Labelpanel.text = "Printer";
    Labelpanel.orientation = "column";
    Labelpanel.alignChildren = ["center","top"];
    Labelpanel.spacing = 10;
    Labelpanel.margins = 16;
    var Button1 = Labelpanel.add("button", undefined, undefined, {name: "Button 1"});
    Button1.text = "Magenta 1";
    Button1.onClick =  function() {
        var Label = document.pages[0].textFrames.add();
        Label.properties = {
            geometricBounds : [ 25,284.5,15,226.5 ],
            contents : "1234_Mag1"
            }
        Label.paragraphs[0].appliedFont = app.fonts.item("Arial");
        Label.paragraphs[0].pointSize = "30";
        Label.paragraphs[0].justification = Justification.RIGHT_ALIGN;
        Labelpanel.close();
        }
    Labelpanel.show();

按钮中的代码正常工作,但在 ScriptUI 中它什么也不做。
有人可以告诉我我做错了什么吗?

在您的情况下,如果您添加以下行,脚本将起作用(不过我不确定您的目标):

#targetengine session

在代码的开头。

并在此处将“对话框”更改为“调色板”:

var Labelpanel = new Window("palette");

问题是对话框 window 在关闭对话框之前无法更改文档。

不像总是可以访问文档的调色板。

可以重写您的脚本以使用对话框 window 而不是调色板。但它需要更改的不仅仅是两行。

更新

这是如何使用对话框完成的示例 window:

// set the dialog window
var labelDialog = new Window("dialog");
labelDialog.text = "Printer";
labelDialog.orientation = "column";
labelDialog.alignChildren = ["center","top"];
labelDialog.spacing = 10;
labelDialog.margins = 16;

// set a variable that we will convey to the main function
var contents = '';

// a couple buttons
var button1 = labelDialog.add("button", undefined, undefined, {name: "Button 1"});
button1.text = "Magenta 1";
var button2 = labelDialog.add("button", undefined, undefined, {name: "Button 2"});
button2.text = "Magenta 2";

// if click --> change the variable an close the dialog
button1.onClick = function() {contents = '1234_Mag1'; labelDialog.close()};
button2.onClick = function() {contents = '5678_Mag2'; labelDialog.close()};

labelDialog.show(); // show the dialog

// if the variable is not empty --> run the main function
if (contents != '') main(contents);

// the main function, get the variable and does the work
function main(contents) {

    app.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;
    app.activeDocument.zeroPoint = [0,0];

    var document = app.activeDocument;
    document.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    document.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

    var label = document.pages[0].textFrames.add();
    label.properties = {
        geometricBounds: [ 25,284.5,15,226.5 ],
        contents: contents // <-- our variable goes here
    }
    label.paragraphs[0].appliedFont = app.fonts.item("Arial");
    label.paragraphs[0].pointSize = "30";
    label.paragraphs[0].justification = Justification.RIGHT_ALIGN;

}

我做了两个按钮。我怀疑您的下一个问题是:“如何添加更多按钮?”

要点:在尝试更改文档之前应关闭对话框 window。调色板不应该。 'subtle difference' 显着改变了算法。