0

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 では何もしません。
誰かが私が間違っていることを教えてもらえますか?

4

1 に答える 1

1

あなたの場合、次の行を追加すると、スクリプトは機能します (ただし、あなたの目標についてはわかりません)。

#targetengine session

コードの先頭に。

ここで「ダイアログ」を「パレット」に変更します。

var Labelpanel = new Window("palette");

問題は、ダイアログ ウィンドウが閉じられるまで、ダイアログ ウィンドウがドキュメントを変更できないことです。

常にドキュメントにアクセスできるパレットとは異なります。

パレットの代わりにダイアログ ウィンドウを使用するために、スクリプトを書き直すことができます。ただし、変更する必要があるのは 2 行だけではありません。

アップデート

以下は、ダイアログ ウィンドウで実行する方法の例です。

// 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;

}

ボタンを2つ作りました。次の質問は「ボタンを追加するにはどうすればよいですか?」でしょう。

要点: ドキュメントを変更する前に、ダイアログ ウィンドウを閉じる必要があります。パレットはすべきではありません。この「微妙な違い」がアルゴリズムを大きく変えます。

于 2021-10-30T17:20:37.913 に答える