Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Open new document in landscape orientation

Community Beginner ,
Feb 08, 2022 Feb 08, 2022

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! 

TOPICS
Scripting

Views

508
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 2 Correct answers

Community Expert , Feb 08, 2022 Feb 08, 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;
	
...

Votes

Translate
Advocate , Feb 08, 2022 Feb 08, 2022

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

Votes

Translate
Adobe
Community Expert ,
Feb 08, 2022 Feb 08, 2022

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.

 

 

Best regards

Votes

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
Community Expert ,
Feb 08, 2022 Feb 08, 2022

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();

Votes

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
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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

Votes

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
Community Expert ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

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

 

return doc;

Votes

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
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

Thanks!

Votes

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
Advocate ,
Feb 08, 2022 Feb 08, 2022

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

Votes

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
Community Beginner ,
Feb 08, 2022 Feb 08, 2022

Copy link to clipboard

Copied

LATEST

Merci beaucoup!

Votes

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