Copy link to clipboard
Copied
I'm new to scripting and I'm trying to open a new document in landscape orientation using Javascript. Is there an option for this with the app.documents.add() function? I'm having trouble finding it in the scripting guide.
Thanks in advance!
Nothing wrong with Charu's reply. Here's a simpler version that doesn't worry about the exact position of the artboard since you're creating a brand new blank document. If you wanted to convert an existing artboard with artwork on it to landscape, you'd want to have some logic to make sure the centerpoint (or any other reference point you choose) stays consistent.
function NewDocLandscapeArtboard()
{
const doc = app.documents.add();
const ab = doc.artboards[0];
var rect = ab.artboardRect;
...
Salut!
Je donnerais un conseil plus simple comme:
// JavaScript Document
var w = 800;
var H = 400;
var docRef = app.documents.add( DocumentColorSpace.CMYK,W,H);
docRef .rulerOrigin = [0,H]; // origine globale en haut à gauche
Si on supprime la dernière ligne l'origine globale est en bas à gauche.
De elleere
Copy link to clipboard
Copied
Hi,
I am sure sure if there is a firect way to do this.
You can rotate the artboards after adding the new document if heightwidth is less than to have landscape view. Sample code below and references
var doc = app.documents.add()
if (doc.width < doc.height) {
var currentArtboard = doc.artboards.getActiveArtboardIndex();
var currentArtboardRect = doc.artboards[currentArtboard].artboardRect; // Rect, which is an array;
var currentLeft = currentArtboardRect[0];
var currentTop = currentArtboardRect[1];
var currentRight = currentArtboardRect[2];
var currentBottom = currentArtboardRect[3];
var currentWidth = Math.abs(currentRight - currentLeft);
var currentHeight = Math.abs(currentBottom - currentTop);
var centerX = currentLeft + (currentWidth / 2);
var centerY = currentTop - (currentHeight / 2); // Y axis is inverted
var newArtboardRect = [];
var newWidth = currentHeight;
var newHeight = currentWidth;
// Rotate the artboard 90 degrees
newArtboardRect[0] = centerX - (newWidth / 2);
newArtboardRect[1] = centerY + (newHeight / 2); // Y axis is inverted
newArtboardRect[2] = centerX + (newWidth / 2);
newArtboardRect[3] = centerY - (newHeight / 2); // Y axis is inverted
doc.artboards[currentArtboard].artboardRect = newArtboardRect;
}
https://github.com/mprewitt/AI-Rotate-Artboard
If I found a direct way, will post here.
Copy link to clipboard
Copied
Nothing wrong with Charu's reply. Here's a simpler version that doesn't worry about the exact position of the artboard since you're creating a brand new blank document. If you wanted to convert an existing artboard with artwork on it to landscape, you'd want to have some logic to make sure the centerpoint (or any other reference point you choose) stays consistent.
function NewDocLandscapeArtboard()
{
const doc = app.documents.add();
const ab = doc.artboards[0];
var rect = ab.artboardRect;
const w = rect[2]-rect[0];
const h = rect[1] - rect[3];
const cp = [rect[0] + w/2,rect[1] - h/2];
if(w < h)
{
ab.artboardRect = [rect[0],rect[1],rect[0] + h,rect[1] - w];
}
}
NewDocLandscapeArtboard();
Copy link to clipboard
Copied
Thank you! Would this function return the document so I can put it in a variable?
Copy link to clipboard
Copied
It certainly could. On the last line inside the function body, right above the closing bracket, type:
return doc;
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
Salut!
Je donnerais un conseil plus simple comme:
// JavaScript Document
var w = 800;
var H = 400;
var docRef = app.documents.add( DocumentColorSpace.CMYK,W,H);
docRef .rulerOrigin = [0,H]; // origine globale en haut à gauche
Si on supprime la dernière ligne l'origine globale est en bas à gauche.
De elleere
Copy link to clipboard
Copied
Merci beaucoup!