Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
Indesign 2020 ScriptUI add text frame

I have a problem with a script for Indesign 2020.
I am not a programmer and don't know javascript (I just created some simple script).
I want to create a simple panel with a button that add a textframe, but doesn't seems to work.
Here's the code

    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();

The code in the button normally works, but in ScriptUI it does nothing.
Can someone tell me what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your case the script will work (I'm not sure about your goal, though) if you add this line:

#targetengine session

at the start of the code.

And change "dialog" with "palette" here:

var Labelpanel = new Window("palette");

The problem is a dialog window can't change a document until the dialog will be closed.

Not like a palette that has access to documents always.

Your script can be rewritten in order to use a dialog window instead of palette. But it will need to change more than just two lines.

Update

Here is the example how it can be done with a dialog 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;

}

I've made two buttons. I suspect it would be your next question: "How to add more buttons?"

The main point: A dialog window should be closed before you try to change a document. A palette should not. This 'subtle difference' changes the algorithm significantly.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda