Performance of custom Zoom In script

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: xbytor, DavideBarranca, Paul MR

igoroshko
Posts: 3
Joined: Fri Oct 06, 2017 8:32 pm

Performance of custom Zoom In script

Postby igoroshko » Fri Oct 06, 2017 8:46 pm

Hello,

I need custom script values, so found a script that can do it:

Code: Select all

main();
function main(){
/****************
Use either
zoomIn();
zoomOut();
**************/
if(!documents.length) return;
var zoomLevels=[12.5, 25, 50, 100, 150, 200, 300, 400];
zoomIn();

function zoomIn(){
var zoomLevel = getZoomLevel();
for(var z in zoomLevels){
    if(Number(zoomLevels[z]) > Number(zoomLevel)){
        setZoomLevel(zoomLevels[z]);
        break;
        }
    }
};

function getZoomLevel(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var desc = executeActionGet(ref);
   return Number(desc.getDouble(stringIDToTypeID('zoom'))*100).toFixed(1);
};

function setZoomLevel( zoom ) {
    if(zoom < 1 ) zoom =1;
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var getScrRes = executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;
   var docRes = activeDocument.resolution;
   activeDocument.resizeImage( undefined, undefined, getScrRes/(zoom/100), ResampleMethod.NONE );
   var desc = new ActionDescriptor();
   ref = null;
   ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID( "Mn  " ), charIDToTypeID( "MnIt" ), charIDToTypeID( 'PrnS' ) );
   desc.putReference( charIDToTypeID( "null" ), ref );
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );
};
};


I added it to the scripts folder, and assigned shortcut. But one problem still remains - it is quite slow, takes about 2 secons to change the zoom, while standard shortcut 'Ctrl' + '+' is much faster.

Is there any way to improve script execution speed?



User avatar
txuku
Posts: 54
Joined: Thu Jan 01, 1970 12:00 am
Contact:

Re: Performance of custom Zoom In script

Postby txuku » Sat Oct 07, 2017 9:04 am

Bonjour

Um..............

Ctrl+ and Ctrl- with Ctrl0
seem to me the fastest ! :)

igoroshko
Posts: 3
Joined: Fri Oct 06, 2017 8:32 pm

Re: Performance of custom Zoom In script

Postby igoroshko » Sat Oct 07, 2017 1:32 pm

But I need custom zoom levels. Ctrl+ jumps from 100% to 200% which is completely unuseful on my 4k screen when working on web designs.

User avatar
txuku
Posts: 54
Joined: Thu Jan 01, 1970 12:00 am
Contact:

Re: Performance of custom Zoom In script

Postby txuku » Sat Oct 07, 2017 2:43 pm

Ah bon ! :)

You have the xbytor function setZoom( ) that should suit you :

Code: Select all

function setZoom( zoom ) {// as percent
   cTID = function(s) { return app.charIDToTypeID(s); }; // from xbytor
   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 );
}
setZoom( 65 );

User avatar
Jaroslav Bereza
Posts: 36
Joined: Tue Dec 27, 2016 7:22 pm
Contact:

Re: Performance of custom Zoom In script

Postby Jaroslav Bereza » Tue Oct 10, 2017 9:53 pm

I think this not zoom, but some kind of hack :-D
Looks like you are changing resolution without any visible change.
Magic Scripts for Photoshop

xbytor
Posts: 20
Joined: Thu Jan 01, 1970 12:00 am

Re: Performance of custom Zoom In script

Postby xbytor » Thu Oct 12, 2017 11:44 pm

Jaroslav Bereza wrote:I think this not zoom, but some kind of hack :-D
Looks like you are changing resolution without any visible change.


It was Mike Hale's hack. "PrnS" is "PrintSize". Not really sure what it does.

User avatar
txuku
Posts: 54
Joined: Thu Jan 01, 1970 12:00 am
Contact:

Re: Performance of custom Zoom In script

Postby txuku » Fri Oct 13, 2017 7:02 am

Bonjour

Glad to see you pass xbytor ! :)

User avatar
Jaroslav Bereza
Posts: 36
Joined: Tue Dec 27, 2016 7:22 pm
Contact:

Re: Performance of custom Zoom In script

Postby Jaroslav Bereza » Fri Oct 13, 2017 8:22 pm

It was Mike Hale's hack. "PrnS" is "PrintSize". Not really sure what it does.

I know how it works.

You can set print resolution in PS unit settings.
Here: Image

You can set here how many DPI has your monitor. So you can trigger print size and A4 on monitor will have same size as A4 paper in realworld. Because it will set your zoom to specific number.

Image

This code will save you 1500ms (at least on my machine).
I added property to reference for document and application. So you are asking for specific property instead huge amount of data.
You probably also could disable zoom animation... you need to wait while animation is showing zoom.
And I am not sure why we need "activeDocument.resizeImage" this could save 100ms

Code: Select all

main();
function main(){
/****************
Use either
zoomIn();
zoomOut();
**************/
if(!documents.length) return;
var zoomLevels=[12.5, 25, 50, 100, 150, 200, 300, 400];
zoomIn();

function zoomIn(){
var zoomLevel = getZoomLevel();
for(var z in zoomLevels){
    if(Number(zoomLevels[z]) > Number(zoomLevel)){
        setZoomLevel(zoomLevels[z]);
        break;
        }
    }
};

function getZoomLevel(){
   var ref = new ActionReference();
   ref.putProperty( stringIDToTypeID('property'), stringIDToTypeID('zoom'));
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var desc = executeActionGet(ref);
   return Number(desc.getDouble(stringIDToTypeID('zoom'))*100).toFixed(1);
};

function setZoomLevel( zoom ) {
    if(zoom < 1 ) zoom =1;
   var ref = new ActionReference();
   ref.putProperty( stringIDToTypeID('property'), stringIDToTypeID('unitsPrefs'));
   ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var getScrRes = executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;
   var docRes = activeDocument.resolution;
   activeDocument.resizeImage( undefined, undefined, getScrRes/(zoom/100), ResampleMethod.NONE );
   var desc = new ActionDescriptor();
   ref = null;
   ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID( "Mn  " ), charIDToTypeID( "MnIt" ), charIDToTypeID( 'PrnS' ) );
   desc.putReference( charIDToTypeID( "null" ), ref );
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
   activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );
};
};
Magic Scripts for Photoshop

User avatar
Jaroslav Bereza
Posts: 36
Joined: Tue Dec 27, 2016 7:22 pm
Contact:

Re: Performance of custom Zoom In script

Postby Jaroslav Bereza » Fri Oct 13, 2017 8:36 pm

So I figured out what is going on...
1) script is changing only document resolution value but not width and height in pixels and from this reason "print size" changes zoom.
2) so we need read value before and set it back after changing
I would prefer not changing document resolution but screen resolution in settings.
I will check performance.

This can change print resolution settings.

Code: Select all

var idsetd = charIDToTypeID( "setd" );
    var desc57 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idPrpr = charIDToTypeID( "Prpr" );
        var idUntP = charIDToTypeID( "UntP" );
        ref7.putProperty( idPrpr, idUntP );
        var idcapp = charIDToTypeID( "capp" );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref7.putEnumerated( idcapp, idOrdn, idTrgt );
    desc57.putReference( idnull, ref7 );
    var idT = charIDToTypeID( "T   " );
        var desc58 = new ActionDescriptor();
        var idnewDocPresetPrintResolution = stringIDToTypeID( "newDocPresetPrintResolution" );
        var idRlt = charIDToTypeID( "#Rlt" );
        desc58.putUnitDouble( idnewDocPresetPrintResolution, idRlt, 21597.191895 );
        var idnewDocPresetScreenResolution = stringIDToTypeID( "newDocPresetScreenResolution" );
        var idRlt = charIDToTypeID( "#Rlt" );
        desc58.putUnitDouble( idnewDocPresetScreenResolution, idRlt, 5184.000000 );
    var idUntP = charIDToTypeID( "UntP" );
    desc57.putObject( idT, idUntP, desc58 );
executeAction( idsetd, desc57, DialogModes.NO );
Magic Scripts for Photoshop

User avatar
Jaroslav Bereza
Posts: 36
Joined: Tue Dec 27, 2016 7:22 pm
Contact:

Re: Performance of custom Zoom In script

Postby Jaroslav Bereza » Sat Oct 14, 2017 10:58 am

This is my final version. Anyway It will work wrong if you have screenSize as "pixels/cm" and you can't read this setting.

Code: Select all

main();
function main(){
   /****************
   Use either
   zoomIn();
   zoomOut();
   **************/
   if(!documents.length) return;
   var zoomLevels=[12.5, 25, 50, 100, 150, 200, 300, 400, 800, 1600, 3200];
   zoomIn();

   function zoomIn(){
      var zoomLevel = getZoomLevel();
      for(var z in zoomLevels){
         if(Number(zoomLevels[z]) > Number(zoomLevel)){
            setZoomLevel(zoomLevels[z]);
            break;
         }
      }
   };

   function getZoomLevel(){
      var ref = new ActionReference();
      ref.putProperty( stringIDToTypeID('property'), stringIDToTypeID('zoom'));
      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var desc = executeActionGet(ref);
      return Number(desc.getDouble(stringIDToTypeID('zoom'))*100).toFixed(1);
   };

   function setDocResolution(dpi){
      var desc = new ActionDescriptor();
      desc.putUnitDouble( charIDToTypeID( "Rslt" ), charIDToTypeID( "#Rsl" ), dpi );
      executeAction( charIDToTypeID( "ImgS" ), desc, DialogModes.NO );
   }

   function setZoomLevel( zoom ) {
      if(zoom < 1 ) zoom = 1;
      var ref = new ActionReference();
      ref.putProperty( stringIDToTypeID('property'), stringIDToTypeID('unitsPrefs'));
      ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      var getScrRes = executeActionGet(ref).getObjectValue(stringIDToTypeID('unitsPrefs')).getUnitDoubleValue(stringIDToTypeID('newDocPresetScreenResolution'))/72;
      var docRes = activeDocument.resolution;
      setDocResolution(getScrRes/(zoom/100))
      
      runMenuItem(stringIDToTypeID( 'printSize' ))
      setDocResolution(docRes);
   };
};


Magic Scripts for Photoshop


Return to “Photoshop Scripting - General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest