Skip to main content
Inspiring
August 30, 2021
Answered

How can i add a Dropdown menu or Button to select between sizes?

  • August 30, 2021
  • 1 reply
  • 435 views

I have 2 scripts to resize the artboard 1 for mm and 1 for inches. is there a way to have the script use a drop down to change between the 2 Codes?

I got the Inches code here and adjusted it for mm. The inches one is the exact same code but with a multipier of 72 instead of 2.83....... Also is it possible to keep the Prompts on the same page? right now i have to enter the Width press ok then enter the Height. I think it would be a little nicer looking if it were all on the same menu.

 As usual thanks for your help.


var w = prompt("width (in mm)",14,) * 2.8346456692913;
var h = prompt("height (in mm)",7.5) * 2.8346456692913;
var docs = app.documents;
for (var i = 0; i < docs.length; i++) {
docs[i].activate();
for (var j = 0; j < activeDocument.artboards.length; j++) {
var AB = activeDocument.artboards[j];
var xCentre = AB.artboardRect[0] + ((AB.artboardRect[2]-AB.artboardRect[0])/2);
var yCentre = AB.artboardRect[1] + ((AB.artboardRect[3]-AB.artboardRect[1])/2);
AB.artboardRect = [xCentre - w/2, yCentre + h/2, xCentre + w/2, yCentre - h/2];
}
}
This topic has been closed for replies.
Correct answer femkeblanco
var w = new Window("dialog", "Resize Artboards");
    w.orientation = "column";
var group1 = w.add("group");
    group1.orientation = "row";
var group2 = group1.add("group");
    group2.preferredSize.height = 60;
    group2.orientation = "column";
var static1 = group2.add("statictext", undefined, "Width");
    static1.preferredSize.height = 20;
var static2 = group2.add("statictext", undefined, "Height");
    static2.preferredSize.height = 20;
var group3 = group1.add("group");
    group3.preferredSize.height = 60;
    group3.orientation = "column";
var edit1 = group3.add("edittext", undefined, "1");
    edit1.preferredSize.width = 60;
    edit1.preferredSize.height = 20;
    var wInPs = Number(edit1.text);
    edit1.onChange = function () {
        wInPs = Number(edit1.text);
    };
var edit2 = group3.add("edittext", undefined, "1");
    edit2.preferredSize.width = 60;
    edit2.preferredSize.height = 20;
    var hInPs = Number(edit2.text);
    edit2.onChange = function () {
        hInPs = Number(edit2.text);
    };
var drop1 = group1.add("dropdownlist", undefined, ["mm","-","inches"]);
    drop1.selection = 0;
    var units = drop1.selection.text;
    drop1.onChange = function () {
        units = drop1.selection.text;
    };
var button1 = w.add("button", undefined, "OK");
    button1.onClick = function () {
        main();
        w.close();
    };
w.show();

function main() {
    var f;
    if (units == "mm") {
        f = 2.835;
    } else {
        f = 72;
    }
    var w = wInPs * f;
    var h = hInPs * f;
    var docs = app.documents;
    for (var i = 0; i < docs.length; i++) {
    docs[i].activate();
            for (var j = 0; j < activeDocument.artboards.length; j++) {
            var AB = activeDocument.artboards[j];
            var xCentre = AB.artboardRect[0] + ((AB.artboardRect[2]-AB.artboardRect[0])/2);
            var yCentre = AB.artboardRect[1] + ((AB.artboardRect[3]-AB.artboardRect[1])/2);
            AB.artboardRect = [xCentre - w/2, yCentre + h/2, xCentre + w/2, yCentre - h/2];
        }
    }
}

1 reply

cbishop01Author
Inspiring
August 30, 2021

Something like this.  Where the "14" is at that is what it looks like now.

femkeblanco
femkeblancoCorrect answer
Legend
August 30, 2021
var w = new Window("dialog", "Resize Artboards");
    w.orientation = "column";
var group1 = w.add("group");
    group1.orientation = "row";
var group2 = group1.add("group");
    group2.preferredSize.height = 60;
    group2.orientation = "column";
var static1 = group2.add("statictext", undefined, "Width");
    static1.preferredSize.height = 20;
var static2 = group2.add("statictext", undefined, "Height");
    static2.preferredSize.height = 20;
var group3 = group1.add("group");
    group3.preferredSize.height = 60;
    group3.orientation = "column";
var edit1 = group3.add("edittext", undefined, "1");
    edit1.preferredSize.width = 60;
    edit1.preferredSize.height = 20;
    var wInPs = Number(edit1.text);
    edit1.onChange = function () {
        wInPs = Number(edit1.text);
    };
var edit2 = group3.add("edittext", undefined, "1");
    edit2.preferredSize.width = 60;
    edit2.preferredSize.height = 20;
    var hInPs = Number(edit2.text);
    edit2.onChange = function () {
        hInPs = Number(edit2.text);
    };
var drop1 = group1.add("dropdownlist", undefined, ["mm","-","inches"]);
    drop1.selection = 0;
    var units = drop1.selection.text;
    drop1.onChange = function () {
        units = drop1.selection.text;
    };
var button1 = w.add("button", undefined, "OK");
    button1.onClick = function () {
        main();
        w.close();
    };
w.show();

function main() {
    var f;
    if (units == "mm") {
        f = 2.835;
    } else {
        f = 72;
    }
    var w = wInPs * f;
    var h = hInPs * f;
    var docs = app.documents;
    for (var i = 0; i < docs.length; i++) {
    docs[i].activate();
            for (var j = 0; j < activeDocument.artboards.length; j++) {
            var AB = activeDocument.artboards[j];
            var xCentre = AB.artboardRect[0] + ((AB.artboardRect[2]-AB.artboardRect[0])/2);
            var yCentre = AB.artboardRect[1] + ((AB.artboardRect[3]-AB.artboardRect[1])/2);
            AB.artboardRect = [xCentre - w/2, yCentre + h/2, xCentre + w/2, yCentre - h/2];
        }
    }
}
cbishop01Author
Inspiring
August 30, 2021

Thanks a ton this does exactly what i needed.