Set Artboard Size to specific size with selected art centered

Contributor ,
Jan 14, 2022 Jan 14, 2022

Copy link to clipboard

Copied

Hello Everyone,

 

I have a Script here but it is not quite doing what I hoped it would.   I want to be able to resize an artboard to a specific size based off of a dropdown list.  When I run my script I get the correct size artboard but it opens up a new document.  Any Help would be appreciated!

var choice;

//Creates new Popup
var Pallette = new Window ("dialog", "Artboard Resizer");
Pallette.add ("statictext", undefined, "Pick a size");
Pallette.orientation = "row";

//Dropdown Menu
var myDropdown =  Pallette.add ("dropdownlist", undefined, [
"8.5 x 11",
"11 x 8.5",
"12 x 16",
"16 x 12",
"24 x 12",
"32 x 20"
]);
var myButtonGroup =  Pallette.add ("group");
myButtonGroup.orientation = "column";
myDropdown.selection = 0;
var btnCreate = myButtonGroup.add ("button", undefined, "OK");
btnCreate.onClick = function () {
    choice = myDropdown.selection.text;
    w.button.onClick = function () {

        w.close();
    
    }  
};
Pallette.show ();


var doc;
if (choice == "8.5 x 11") {
    //var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 8.5 * PTS_IN;
    var height = 11* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var doc = new DocumentPreset();
    doc.units = RulerUnits.Inches;
    doc.width = width;
    doc.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
    // 
} else if (choice == "11 x 8.5") {
    var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 11 * PTS_IN;
    var height = 8.5* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var docPreset = new DocumentPreset();
    docPreset.units = RulerUnits.Inches;
    docPreset.width = width;
    docPreset.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
    // 
} else if (choice == "12 x 16") {
    var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 12 * PTS_IN;
    var height = 16* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var docPreset = new DocumentPreset();
    docPreset.units = RulerUnits.Inches;
    docPreset.width = width;
    docPreset.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
    // 
} else if (choice == "16 x 12") {
    var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 16 * PTS_IN;
    var height = 12* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var docPreset = new DocumentPreset();
    docPreset.units = RulerUnits.Inches;
    docPreset.width = width;
    docPreset.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
    // 
} else if (choice == "24 x 12") {
    var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 24 * PTS_IN;
    var height = 12* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var docPreset = new DocumentPreset();
    docPreset.units = RulerUnits.Inches;
    docPreset.width = width;
    docPreset.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
    // 
}  else if (choice == "32 x 20") {
    var doc = app.activeDocument;
    var PTS_IN = 72;
    var width = 32 * PTS_IN;
    var height = 20* PTS_IN;
    var presets = app.startupPresetsList;
    var preset = presets[5];
    var docPreset = new DocumentPreset();
    docPreset.units = RulerUnits.Inches;
    docPreset.width = width;
    docPreset.height = height;
    var doc = app.documents.addDocument(preset, docPreset, false);
}
TOPICS
Scripting

Views

102

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jan 14, 2022 Jan 14, 2022
It's because addDocument() creates a new doc.  Try thisvar choice; //Creates new Popup var Pallette = new Window ("dialog", "Artboard Resizer"); Pallette.add ("statictext", undefined, "Pick a size"); Pallette.orientation = "row"; //Dropdown Menu var myDropdown = Pallette.add ("dropdownlist", undefined, [ "8.5 x 11", "11 x 8.5", "12 x 16", "16 x 12", "24 x 12", "32 x 20" ]); var myButtonGroup = Pallette.add ("group"); myButtonGroup.orientation = "column"; myDropdown.selection = 0; var btnCreate ...

Likes

Translate

Translate
Advocate ,
Jan 14, 2022 Jan 14, 2022

Copy link to clipboard

Copied

LATEST

It's because addDocument() creates a new doc. 

 

Try this

var choice;
//Creates new Popup
var Pallette = new Window ("dialog", "Artboard Resizer");
Pallette.add ("statictext", undefined, "Pick a size");
Pallette.orientation = "row";
//Dropdown Menu
var myDropdown = Pallette.add ("dropdownlist", undefined, [
"8.5 x 11",
"11 x 8.5",
"12 x 16",
"16 x 12",
"24 x 12",
"32 x 20"
]);
var myButtonGroup =  Pallette.add ("group");
myButtonGroup.orientation = "column";
myDropdown.selection = 0;
var btnCreate = myButtonGroup.add ("button", undefined, "OK");
btnCreate.onClick = function () {
    choice = myDropdown.selection.text;
    w.button.onClick = function () {
        w.close();
    }  
};
Pallette.show ();

// new code
if (choice == "8.5 x 11") {
    resizeArtboard(8.5 * 72, 11 * 72);
} else if (choice == "11 x 8.5") {
    resizeArtboard(11 * 72, 8.5 * 72);
} else if (choice == "12 x 16") {
    resizeArtboard(12 * 72, 16 * 72);
} else if (choice == "16 x 12") {
    resizeArtboard(16 * 72, 12 * 72);
} else if (choice == "24 x 12") {
    resizeArtboard(24 * 72, 12 * 72);
}  else if (choice == "32 x 20") {
    resizeArtboard(32 * 72, 20 * 72);
}
function resizeArtboard(w, h) {
    var AB = app.activeDocument.artboards[0];
    var ABR = AB.artboardRect;
    var xC = ABR[0]+((ABR[2]-ABR[0])/2);
    var yC = ABR[1]+((ABR[3]-ABR[1])/2);
    AB.artboardRect = [xC-w/2, yC+h/2, xC+w/2, yC-h/2];
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines