Skip to main content
dublove
Legend
April 18, 2025
Answered

Please, Help me modify this script, thanks.

  • April 18, 2025
  • 1 reply
  • 668 views

This script is used for ps to automatically zoom to 100% after opening an image.
Now I want to add a function: after zooming in 100%, pop up the image size setting dialog box.

I'm going to change the width of the image。
Thank you very much.

setZoom (100);

function setZoom( zoom ) {
   cTID = function(s) { return app.charIDToTypeID(s); };
   var docRes = activeDocument.resolution;
   activeDocument.resizeImage( undefined, undefined, 72/(zoom/100), ResampleMethod.NONE );
   var desc = new ActionDescriptor();
   var ref = new ActionReference();
   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );
   desc.putReference( cTID( "null" ), ref );
   executeAction( cTID( "slct" ), desc, DialogModes.NO );
   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );
}
Correct answer Stephen Marsh

Hi Stephen Marsh

I don't know much about it. Please enlighten me.

Thank you very much.


quote

Hi Stephen Marsh

I don't know much about it. Please enlighten me.

Thank you very much.


By @dublove

 

Sure, the previous code created 2 history steps:

 

1) Image Size (72 ppi)

2) Image Size (original ppi value)

 

So if one clicked on the first history step, then the document resolution would no longer be correct.

 

Multiple history steps can be combined into a single entry in the history panel by calling the setZoom(100) function and parameter using suspendHistory()

 

// Call the zoom function from within suspendHistory
activeDocument.suspendHistory("Set Zoom to 100%", "setZoom(100)");

// Open the image size dialog
sTID = function (s) { return app.stringIDToTypeID(s); };
try {
  executeAction(sTID('imageSize'), undefined, DialogModes.ALL);
} catch (e) { if (e.number != 8007) { alert("Line: " + e.line + "\n\n" + e, "Bug!", true); throw (e); } }  


function setZoom(zoom) {
  var docRes = activeDocument.resolution;
  try {
    cTID = function (s) { return app.charIDToTypeID(s); };
    activeDocument.resizeImage(undefined, undefined, 72 / (zoom / 100), ResampleMethod.NONE);
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(cTID("Mn  "), cTID("MnIt"), cTID('PrnS'));
    desc.putReference(cTID("null"), ref);
    executeAction(cTID("slct"), desc, DialogModes.NO);
    activeDocument.resizeImage(undefined, undefined, docRes, ResampleMethod.NONE);
  } catch (error) {
    if (docRes !== undefined) {
      activeDocument.resolution = docRes;
    }
    alert("Line: " + (error.line || 'unknown') + "\n\n" + error.message, "Bug!", true);
    throw error; // Optional to abort the script
  }
}

 

I have also added error checking in the setZoom() function to restore the original document resolution if an unexpected error occurs.

1 reply

Inspiring
April 18, 2025

See if this works for you

 

setZoom (100);

function setZoom( zoom ) {
   cTID = function(s) { return app.charIDToTypeID(s); };
   var docRes = activeDocument.resolution;
   activeDocument.resizeImage( undefined, undefined, 72/(zoom/100), ResampleMethod.NONE );
   var desc = new ActionDescriptor();
   var ref = new ActionReference();
   ref.putEnumerated( cTID( "Mn  " ), cTID( "MnIt" ), cTID( 'PrnS' ) );
   desc.putReference( cTID( "null" ), ref );
   executeAction( cTID( "slct" ), desc, DialogModes.NO );
   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );
}

sTID = function(s) { return app.stringIDToTypeID(s); };
  try {
    executeAction(sTID('imageSize'), undefined, DialogModes.ALL);
  }catch (e) { if (e.number!=8007) { alert("Line: "+e.line+"\n\n"+e,"Bug!",true); throw(e); } }  
dublove
dubloveAuthor
Legend
April 20, 2025

Hi @Ciccillotto 

Isn't there something unreasonably wrong with this script?
It changes my resolution.
If the image was 350dpi before it was typed, it shifted to 72dpi when it was opened.

It shouldn't be changing my resolution for that to happen.
It keeps the original resolution and displays it at 100%.

Stephen Marsh
Community Expert
Community Expert
April 20, 2025

Maybe my settings were wrong, now it's normal again, scared me earlier ......



@dublove wrote:

Maybe my settings were wrong, now it's normal again, scared me earlier ......


 

The script sets a variable to the original resolution (PPI, not DPI) and then makes a temporary change to the resolution (without resampling) as a clever display sizing hack, before restoring the original resolution value.

 

So if the script was terminated before completion, or if one went back through the history steps it's possible that the file could be in a state where the resolution is incorrect.

 

One could attempt to reduce such possibilities by adding a try/catch block or suspendHisory to the code.