Skip to main content
Known Participant
March 26, 2025
Answered

Need Help in Scripting

  • March 26, 2025
  • 3 replies
  • 3886 views

Hello @m1b ,

I need some help with a script. Could you please assist me?

Is there any scripting possibility to adjust this dialog box in Photoshop?

Correct answer Stephen Marsh

For PDF files larger than 200 inches, the Photoshop interface will default to 200 and you will have to manually override this with the desired width, such as 272 in @ 72 ppi. Scripting will have the same limitation with the current script code. I'll see if I can find another workable alternative solution, as my first idea also hits the limit of 200, although it can be made larger if the required size is known.

3 replies

Stephen Marsh
Community Expert
March 27, 2025

@punit_9859 

 

This v2 script uses a different method to get the PDF page size, it works with your sample PDF but fails with some other PDF files that lack the required information. If you get an error that the page size can't be determined, use the previous v1 script.

 

I hope to rework this, but it will have to do for now:

 

Back to the drawing board. Reflecting on the challenges and your feedback, I think that it is best and safest to process files 200 inches or larger manually. I have replaced the previous 2.0 code with the new v2.1 code as it wasn't performing as expected.

 

/*
Rasterize to Variable Resolution Based on PDF Page Size in Inches.jsx
Stephen Marsh
v2.1, 26th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-help-in-scripting/td-p/15232063
*/

#target photoshop

// Open a dialog to select the PDF file
var thePDF = File.openDialog("Select a PDF file to rasterize");

if (thePDF !== null) {

    // Get the file dimensions
    var pdfOpenOptions = new PDFOpenOptions();
    pdfOpenOptions.antiAlias = true;
    pdfOpenOptions.mode = OpenDocumentMode.GRAYSCALE; // Set to grayscale for speed
    pdfOpenOptions.resolution = 2; // Low res for speed
    pdfOpenOptions.page = 1; // First page only
    pdfOpenOptions.cropPage = CropToType.MEDIABOX; // MEDIABOX | CROPBOX | BLEEDBOX | TRIMBOX | ARTBOX
    app.open(thePDF, pdfOpenOptions);

    // Get the doc width and height in inches
    var docWidthInches = (app.activeDocument.width.as("inches")).toFixed(2);
    var docHeightInches = (app.activeDocument.height.as("inches")).toFixed(2);

    // Close the doc without saving
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

    // Determine the greater value between width and height
    var maxDimensionInches = Math.max(docWidthInches, docHeightInches);

    // Set the resolution based on the greater value
    var resolution;
    if (maxDimensionInches <= 12) {
        resolution = 600;
    } else if (maxDimensionInches >= 12.01 && maxDimensionInches <= 24) {
        resolution = 300;
    } else if (maxDimensionInches >= 24.01 && maxDimensionInches <= 120) {
        resolution = 125;
    } else if (maxDimensionInches <= 199.99) {
        resolution = 72;
    } else {
        alert("Process this oversize PDF manually.");
        throw new Error("Process this oversize PDF manually.");
    }

    // Re-open the PDF as a new doc in RGB mode with the determined resolution
    pdfOpenOptions.mode = OpenDocumentMode.RGB;
    pdfOpenOptions.resolution = resolution;
    app.open(thePDF, pdfOpenOptions);

    // Flatten the image to remove transparency and layers
    app.activeDocument.flatten();

    /*
    // New layer from Background
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("layer"), s2t("background"));
    descriptor.putReference(s2t("null"), reference);
    descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
    */

} else {
    alert("Script cancelled!");
}

 

Known Participant
March 28, 2025

@Stephen Marsh ,

I ran the script but encountered some issues. When using the script, the file does not open at its accurate size in Photoshop. For reference, I will attach a screenshot showing the original file size and the size after opening via the script.

Additionally, the script always opens the file in RGB mode. If possible, I would like the script to open the file in its original color mode— for example, if the file is in CMYK, it should open in CMYK, and if it is in RGB, it should open in RGB.

Stephen Marsh
Community Expert
March 28, 2025

Are all of the files from the same source, Adobe Illustrator?

 

Why do you need to rasterize?

 

Have you tried rasterizing the PDF in either Illustrator or Acrobat Pro?

Stephen Marsh
Community Expert
March 27, 2025

@punit_9859 – Try this script and let me know how you go:

 

/*
Rasterize to Variable Resolution Based on PDF Page Size in Inches.jsx
Stephen Marsh
v1.0, 26th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-help-in-scripting/td-p/15232063
*/

#target photoshop

// Open a dialog to select the PDF file
var thePDF = File.openDialog("Select a PDF file to rasterize");

if (thePDF !== null) {

    // Get the file dimensions
    var pdfOpenOptions = new PDFOpenOptions();
    pdfOpenOptions.antiAlias = true;
    pdfOpenOptions.mode = OpenDocumentMode.GRAYSCALE; // Set to grayscale for speed
    pdfOpenOptions.resolution = 2; // Low res for speed
    pdfOpenOptions.page = 1; // First page only
    pdfOpenOptions.cropPage = CropToType.MEDIABOX; // MEDIABOX | CROPBOX | BLEEDBOX | TRIMBOX | ARTBOX
    app.open(thePDF, pdfOpenOptions);

    // Get the doc width and height in inches
    var docWidthInches = (app.activeDocument.width.as("inches")).toFixed(2);
    var docHeightInches = (app.activeDocument.height.as("inches")).toFixed(2);

    // Close the doc without saving
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

    // Determine the greater value between width and height
    var maxDimensionInches = Math.max(docWidthInches, docHeightInches);

    // Set the resolution based on the greater value
    var resolution;
    if (maxDimensionInches < 12) {
        resolution = 600;
    } else if (maxDimensionInches >= 12.01 && maxDimensionInches <= 24) {
        resolution = 300;
    } else if (maxDimensionInches >= 24.01 && maxDimensionInches <= 120) {
        resolution = 125;
    } else {
        resolution = 72;
    }

    // Re-open the PDF as a new doc in RGB mode with the determined resolution
    pdfOpenOptions.mode = OpenDocumentMode.RGB;
    pdfOpenOptions.resolution = resolution;
    app.open(thePDF, pdfOpenOptions);

    // Flatten the image to remove transparency and layers
    app.activeDocument.flatten();

    /*
    // New layer from Background
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putProperty(s2t("layer"), s2t("background"));
    descriptor.putReference(s2t("null"), reference);
    descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
    */

} else {
    alert("Script cancelled!");
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Known Participant
March 27, 2025

@Stephen Marsh ,

The script works perfectly for files under 200 inches, but for files above 200 inches, it automatically limits the maximum size to 200 inches.

For reference, I have attached a file with dimensions 272 inches × 152 inches.

Stephen Marsh
Community Expert
March 27, 2025

Scripts can't change how Photoshop works, what it's limitations are.

 

I have an alternative method which might work for these large dimensions. I'll come back to you when I have time.

Stephen Marsh
Community Expert
March 26, 2025

@punit_9859 

 

A script can't adjust a compiled program GUI.

 

It's much easier to script without a graphical user interface, just using basic system dialogs to select files or folders etc.

 

If you want a different GUI, you must recreate the entire interface and associated functions and logic in either legacy ExtendScript or UXP.

 

Perhaps if you posted in more detail, step-by-step what you are trying to do... Or markup a screenshot showing what you want to change.

 

Known Participant
March 26, 2025

Thank you for your response.
Yes Sure, @Stephen Marsh 

Basically, I want to create a script that sets a specific DPI based on the image size:

  • 1 inch to 12 inches600 DPI

  • 13 inches to 24 inches300 DPI

  • 25 inches to 120 inches125 DPI

  • More than 120 inches72 DPI

Would it be possible to achieve this through a script?

 

Stephen Marsh
Community Expert
March 26, 2025
quote

Thank you for your response.
Yes Sure, @Stephen Marsh 

Basically, I want to create a script that sets a specific DPI based on the image size:

  • 1 inch to 12 inches600 DPI

  • 13 inches to 24 inches300 DPI

  • 25 inches to 120 inches125 DPI

  • More than 120 inches72 DPI

Would it be possible to achieve this through a script?

 


By @punit_9859

 

* What is the factor that determines the size in inches for the resolution required (i.e. 1 inch to 12 inches)? Is this width, height, or whichever of the two is larger?

 

* Are these Photoshop PDF files (raster) or are they Generic PDF files, which could contain vectors and rasters?

 

* I presume that you wish to retain the physical print size of the PDF at the required resolution. So if the original image was 8 inches wide, then it should be 8 inches wide @ 600ppi (not dpi) when rasterized. Is that correct?

 

* Are you only rasterizing the first page, or will there be a fixed or variable quantity of multiple pages being rasterized?

 

There are challenges with your request due to limitations with PDFOpenOptions or Action Manager code... The biggest issue is programmatically getting the width or height of the PDF before rasterizing. PDF uses PostScript Points as the internal unit of measure, which is 1/72 inch, so conversions aren't difficult. Within Photoshop, one may have to first rasterize the PDF to get the size, then close and then reopen again with the known size. Or you could try first reading the PDF as binary data and using a regex to find the native size... But what size, page 1, another page, which PDF page box etc?