Skip to main content
dublove
Legend
April 18, 2025
Answered

Please, Help me modify this script, thanks.

  • April 18, 2025
  • 1 reply
  • 676 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 18, 2025

Fantastic, very impressive.
Thank you very much.