Skip to main content
josiearz
Participating Frequently
February 8, 2022
Answered

Open new document in landscape orientation

  • February 8, 2022
  • 2 replies
  • 727 views

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! 

This topic has been closed for replies.
Correct answer renél80416020

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

2 replies

Disposition_Dev
Legend
February 8, 2022

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();
josiearz
josiearzAuthor
Participating Frequently
February 8, 2022

Thank you! Would this function return the document so I can put it in a variable?

Disposition_Dev
Legend
February 8, 2022

It certainly could. On the last line inside the function body, right above the closing bracket, type:

 

return doc;

Charu Rajput
Community Expert
Community Expert
February 8, 2022

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.

 

 

Best regards