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

Please, Help me modify this script, thanks.

Guide ,
Apr 18, 2025 Apr 18, 2025

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 );
}
TOPICS
Actions and scripting , Windows
222
Translate
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

Enthusiast , Apr 18, 2025 Apr 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, DialogMod
...
Translate
Community Expert , Apr 20, 2025 Apr 20, 2025
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 
...
Translate
Adobe
Enthusiast ,
Apr 18, 2025 Apr 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); } }  
Translate
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
Guide ,
Apr 18, 2025 Apr 18, 2025

Fantastic, very impressive.
Thank you very much.

Translate
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
Guide ,
Apr 20, 2025 Apr 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%.

Translate
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
Enthusiast ,
Apr 20, 2025 Apr 20, 2025

on my mac m1 computer does it work correctly and maintain the dpi of the original file?

Translate
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
Guide ,
Apr 20, 2025 Apr 20, 2025

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

Translate
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 ,
Apr 20, 2025 Apr 20, 2025

@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.

Translate
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
Guide ,
Apr 20, 2025 Apr 20, 2025

Hi Stephen Marsh

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

Thank you very much.

Translate
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 ,
Apr 20, 2025 Apr 20, 2025
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.

Translate
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
Guide ,
Apr 20, 2025 Apr 20, 2025
LATEST

Great, the script works perfectly.
Thank you very much.

Translate
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