Skip to main content
Inspiring
July 13, 2022
Answered

Trying to test if a image/document is greater than 2 x the height

  • July 13, 2022
  • 3 replies
  • 1109 views

I'm creating a script that exports pdf's of selected pages or spreads to the desktop opens them in photoshop and checks if its portrait or landscape scales them based on that and exports jpgs. See code below:

 if (app.activeDocument.width.value > app.activeDocument.height.value) {
            //Landscape
            app.activeDocument.resizeImage(
                new UnitValue(2000, "px"),
                currentHeight,
                currentResolution,
                ResampleMethod.BILINEAR
            );
        } else {
            //Portrait
            app.activeDocument.resizeImage(
                new UnitValue(1325, "px"),
                currentHeight,
                currentResolution,
                ResampleMethod.BILINEAR
            );
        }

 This works well until I run into a book that is in a landscape format. So I was thinking instead I could check if the width > 2x height. I think that should work to test if its a landscape book. Like the code below but it doesn't work.

 if (app.activeDocument.width.value > (app.activeDocument.height.value * 2)) {
        //Landscape
         app.activeDocument.resizeImage(
        new UnitValue(2650, "px"),
        currentHeight,
        currentResolution,
        ResampleMethod.BILINEAR
    );
} else{
        //Portrait
        app.activeDocument.resizeImage(
        new UnitValue(1325, "px"),
        currentHeight,
        currentResolution,
        ResampleMethod.BILINEAR
    );
        } 

 

Any ideas of how to make this work or make it better?

This topic has been closed for replies.
Correct answer rob day

Hi @davidn5918184 , You should be able to get it from the page object. This would test the document’s active page:

 

//the page to check
var ap =  app.activeWindow.activePage;
var b = ap.bounds;
var pw = b[3]-b[1]
var ph = b[2]-b[0]

if (ph>pw) {
	$.writeln("Page is Portrait")
} else if (ph == pw){
    $.writeln("Page is Square")
}else{
    $.writeln("Page is Landscape")
}

3 replies

m1b
Community Expert
Community Expert
July 13, 2022

Hi @davidn5918184, as a side question, did you know you can export pages as jpegs directly from Indesign? It would make for a simpler workflow (and script). See Document.exportFile, and App.JPEGExportPreference. You can set the final pixel dimensions using page size and export resolution (with no need for extra scaling step). As for calculating the scaling, I usually think in terms of fitting the page into an N pixel square, eg. a landscape page might be 5000px wide and a portrait page would be 5000px high. Of course, there may be other reasons for the way you are approaching this.

- Mark

Inspiring
July 13, 2022

I do, it is that I need tiffs. I've sent in a feature request which would be so nice but it's not there yet.

 

m1b
Community Expert
Community Expert
July 13, 2022

Yep, just thought it worth checking. If this is a photoshop scripting question, might be best to ask over there?

Or if it's just a basic code issue, what is failing with your code? At a quick glance I don't see what's wrong with your first code (except for missing the square document case, as @rob day mentions). How does it fail? The second code example doesn't make sense to me unless you have a special meaning of "landscape" that it has width twice the height. It feels to me like you aren't specifying your desired outcome well. What feature do you require in the exported TIF file? eg. a certain resolution suitable for printing? Your pixel values seem arbitrary given that the indesign page sizes seem to be variable.

- Mark

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
July 13, 2022

Hi @davidn5918184 , You should be able to get it from the page object. This would test the document’s active page:

 

//the page to check
var ap =  app.activeWindow.activePage;
var b = ap.bounds;
var pw = b[3]-b[1]
var ph = b[2]-b[0]

if (ph>pw) {
	$.writeln("Page is Portrait")
} else if (ph == pw){
    $.writeln("Page is Square")
}else{
    $.writeln("Page is Landscape")
}
Inspiring
July 13, 2022

I can already see issues with it. The first part should set the the width to a single page width of 1325 like the portrait and only if it is a double page spread should it be 2650px. So maybe if its landscape, and not >2x the height?