Skip to main content
Known Participant
July 13, 2023
Question

How to make editable variables dynamic so they change with user input?

  • July 13, 2023
  • 2 replies
  • 276 views

Thanks to other replies here, I'm moving forward with a simple script that will let me draw guides at proportions of my document.


But, the main editable variables I'm creating don't seem to change when I change the text, and I don't know why. I'm new to JavaScript, so if anyone could explain how to fix this, I'd appreciate it.

 

I've configured a dialog with editable text fields. These text fields are meant to control the proportions with which guides are drawn across the length of a document, such as 20% of the document's length, 40%, 60% and so on. The script works the first time I run it, but when I edit the text, nothing changes.

How do I get the variables to dynamically update with new user inputs?

 

var myWindow = new Window("dialog");

    var ProportionsGroup = myWindow.add("group");
    ProportionsGroup.orientation = "column";
    ProportionsGroup.alignment = "left";
    ProportionsGroup.add("statictext",undefined,"Proportions (enter values between 0 and 1)");
        var ProportionEntries = ProportionsGroup.add("group");
        ProportionEntries.alignment = "left";
        var ProportionA = ProportionEntries.add("edittext",undefined,"0.2");
            ProportionA.characters = 6;
            ProportionA.active = true;

var A = ProportionA.text;

var docWidth = Number(app.activeDocument.documentPreferences.pageWidth);
var docHeight = Number(app.activeDocument.documentPreferences.pageHeight);

// guide test
for (var i = 0; i<4;++i){
        app.activeWindow.activePage.guides.add({location: docWidth*i*Number(A), orientation: HorizontalOrVertical.VERTICAL});
}
myWindow.show();

 

 

This topic has been closed for replies.

2 replies

Known Participant
July 13, 2023

Here is again an attempt, which does not return an error, but which STILL does nothing while utilizing the onChange event handler. I'm intending it to delete all previously created guides and then redraw new ones, but it doesn't. 

 

var myWindow = new Window("dialog");

    var ProportionsGroup = myWindow.add("group");
    ProportionsGroup.orientation = "column";
    ProportionsGroup.alignment = "left";
    ProportionsGroup.add("statictext",undefined,"Proportions (enter values between 0 and 1)");
        var ProportionEntries = ProportionsGroup.add("group");
        ProportionEntries.alignment = "left";
        var ProportionA = ProportionEntries.add("edittext",undefined,"0.2");
            ProportionA.characters = 6;
            ProportionA.active = true;

var A = ProportionA.text;

var docWidth = Number(app.activeDocument.documentPreferences.pageWidth);
var docHeight = Number(app.activeDocument.documentPreferences.pageHeight);

// guide test
for (var i = 0; i<4;++i){
        app.activeWindow.activePage.guides.add({location: docWidth*i*Number(A), orientation: HorizontalOrVertical.VERTICAL});
}
A.onChange = function(){
    for (var i = 0; i<4;++i){
    app.activeWindow.activePage.guides[i].remove();
    }
    for (var i = 0; i<4;++i){
        app.activeWindow.activePage.guides.add({location: docWidth*i*Number(A), orientation: HorizontalOrVertical.VERTICAL});
}
}
myWindow.show();
Known Participant
July 13, 2023

Here's a piece of script I tried to get started, but again it does nothing and I don't know why

 

for (var i = 0; i<4;++i){
        app.activeWindow.activePage.guides.add({location: docWidth*i*Number(A), orientation: HorizontalOrVertical.VERTICAL});
        if(A != 0.2){app.activeWindow.activePage.guides[i].remove()}
}
Known Participant
July 13, 2023

As an additional piece of information, there MIGHT maybe possibly be an "onChange" event handler for adobe's scripting, though I don't know how I would implement this here for text fields.