Skip to main content
dublove
Legend
May 5, 2026
Answered

Can this script be modified to fit 46% of the screen?

  • May 5, 2026
  • 4 replies
  • 78 views

I found the code below on the forum; it makes the image automatically fit the screen when opened.
But I want it to fit 46% of the screen—is that possible?
Thank you.

        var id57 = charIDToTypeID(“slct”);
        var desc15 = new ActionDescriptor();
        var id58 = charIDToTypeID(“null”);
        var ref6 = new ActionReference();
        var id59 = charIDToTypeID(“Mn  ”);
        var id60 = charIDToTypeID(“MnIt”);
        var id61 = charIDToTypeID(“FtOn”);
        ref6.putEnumerated(id59, id60, id61);
        desc15.putReference(id58, ref6);
        executeAction(id57, desc15, DialogModes.NO);

 

    Correct answer Stephen Marsh

    @dublove 

     

    Try this script:

     

    /*
    Zoom to 46% of Fit On Screen Zoom
    Stephen Marsh
    https://community.adobe.com/questions-712/can-this-script-be-modified-to-fit-46-of-the-screen-1560318
    */

    #target photoshop

    // Fit on Screen
    var fitDesc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Mn "), charIDToTypeID("MnIt"), charIDToTypeID("FtOn"));
    fitDesc.putReference(charIDToTypeID("null"), ref);
    executeAction(charIDToTypeID("slct"), fitDesc, DialogModes.NO);

    // Read the the current zoom
    var docRef = new ActionReference();
    docRef.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var docDesc = executeActionGet(docRef);
    var fitZoom = docDesc.getDouble(stringIDToTypeID("zoom")) * 100;

    // Calculate the target zoom
    var FACTOR = 0.46;
    var targetZoom = fitZoom * FACTOR;

    // Apply the calculated zoom
    setZoomLevel(targetZoom);

    // Optional confirmation
    alert("Fit zoom: " + Math.round(fitZoom * 10) / 10 + "%\n" + "Target (" + (FACTOR * 100) + "% factor): " + Math.round(targetZoom * 10) / 10 + "%");


    ///// FUNCTIONS /////

    function setZoomLevel(zoomPercent) {
    if (zoomPercent < 1) zoomPercent = 1;

    var originalRes = activeDocument.resolution;

    // Get the screen resolution
    var screenRef = new ActionReference();
    screenRef.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var screenRes = executeActionGet(screenRef)
    .getObjectValue(stringIDToTypeID("unitsPrefs"))
    .getUnitDoubleValue(stringIDToTypeID("newDocPresetScreenResolution")) / 72;

    // Change the resolution temporarily
    activeDocument.resizeImage(undefined, undefined, screenRes / (zoomPercent / 100), ResampleMethod.NONE);

    // Zoom to print size
    var printDesc = new ActionDescriptor();
    var printRef = new ActionReference();
    printRef.putEnumerated(charIDToTypeID("Mn "), charIDToTypeID("MnIt"), charIDToTypeID("PrnS"));
    printDesc.putReference(charIDToTypeID("null"), printRef);
    executeAction(charIDToTypeID("slct"), printDesc, DialogModes.NO);

    // Restore the original resolution
    activeDocument.resizeImage(undefined, undefined, originalRes, ResampleMethod.NONE);
    }

     

    4 replies

    dublove
    dubloveAuthor
    Legend
    May 5, 2026

    Is there any change in zooming or flickering during the process.
    Can it be delivered directly to the specified location?

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

    Is there any change in zooming or flickering during the process.
    Can it be delivered directly to the specified location?

     

    The script zooms in two steps:

    1. Fit to screen
    2. Zooms to the calculated 46% of fit to screen

    I don’t know of a way to do otherwise, perhaps with UXP instead of ExtendScript, I don’t know.

    dublove
    dubloveAuthor
    Legend
    May 5, 2026

    Thank you very much; this works great.
    Especially the adjustable `var FACTOR = 0.80;`
    Thank you for selflessly sharing such a great program.

    Stephen Marsh
    Community Expert
    Stephen MarshCommunity ExpertCorrect answer
    Community Expert
    May 5, 2026

    @dublove 

     

    Try this script:

     

    /*
    Zoom to 46% of Fit On Screen Zoom
    Stephen Marsh
    https://community.adobe.com/questions-712/can-this-script-be-modified-to-fit-46-of-the-screen-1560318
    */

    #target photoshop

    // Fit on Screen
    var fitDesc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Mn "), charIDToTypeID("MnIt"), charIDToTypeID("FtOn"));
    fitDesc.putReference(charIDToTypeID("null"), ref);
    executeAction(charIDToTypeID("slct"), fitDesc, DialogModes.NO);

    // Read the the current zoom
    var docRef = new ActionReference();
    docRef.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var docDesc = executeActionGet(docRef);
    var fitZoom = docDesc.getDouble(stringIDToTypeID("zoom")) * 100;

    // Calculate the target zoom
    var FACTOR = 0.46;
    var targetZoom = fitZoom * FACTOR;

    // Apply the calculated zoom
    setZoomLevel(targetZoom);

    // Optional confirmation
    alert("Fit zoom: " + Math.round(fitZoom * 10) / 10 + "%\n" + "Target (" + (FACTOR * 100) + "% factor): " + Math.round(targetZoom * 10) / 10 + "%");


    ///// FUNCTIONS /////

    function setZoomLevel(zoomPercent) {
    if (zoomPercent < 1) zoomPercent = 1;

    var originalRes = activeDocument.resolution;

    // Get the screen resolution
    var screenRef = new ActionReference();
    screenRef.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var screenRes = executeActionGet(screenRef)
    .getObjectValue(stringIDToTypeID("unitsPrefs"))
    .getUnitDoubleValue(stringIDToTypeID("newDocPresetScreenResolution")) / 72;

    // Change the resolution temporarily
    activeDocument.resizeImage(undefined, undefined, screenRes / (zoomPercent / 100), ResampleMethod.NONE);

    // Zoom to print size
    var printDesc = new ActionDescriptor();
    var printRef = new ActionReference();
    printRef.putEnumerated(charIDToTypeID("Mn "), charIDToTypeID("MnIt"), charIDToTypeID("PrnS"));
    printDesc.putReference(charIDToTypeID("null"), printRef);
    executeAction(charIDToTypeID("slct"), printDesc, DialogModes.NO);

    // Restore the original resolution
    activeDocument.resizeImage(undefined, undefined, originalRes, ResampleMethod.NONE);
    }

     

    dublove
    dubloveAuthor
    Legend
    May 5, 2026

    HI ​@Stephen Marsh 

    Sorry, I made a mistake, which caused the script to fail.

    Running well.

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

    Sorry, I made a mistake, which caused the script to fail.

    Running well.

     

    @dublove - If you are happy, please mark the script as the correct reply/best answer etc.

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

    The following script gets the current zoom level:

     

    /*
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-current-zoom-level/m-p/3133950
    */

    // alert(zoomLevel());
    alert(Math.round(zoomLevel() * 10) / 10 + "%"); // round to 2 decimal places

    function zoomLevel(){
    if(!documents.length) return;
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var desc = executeActionGet(ref);
    return desc.getDouble(stringIDToTypeID('zoom'))*100; // to %
    }

     

    dublove
    dubloveAuthor
    Legend
    May 5, 2026

    Hi ​@Stephen Marsh 

    Sorry, I don't quite understand.
    It looks like the result is wrong. What do I need to change?

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

    Sorry, I don't quite understand.
    It looks like the result is wrong. What do I need to change?

     

    That code was an example of how to get the current zoom, so that you can apply the 46% scale factor to this variable. It wasn’t full working code, it was a hint and a stepping stone.

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

     @dublove - I already answered in the other topic, please don’t double-post.

     

    If you are after 46%:

     

     

    Try this script from ​@jazz-y (originally set to 0.5 or 50%):

     

    /*
    https://community.adobe.com/t5/photoshop-ecosystem-discussions/a-script-that-replaces-the-ctr-0-hotkeys/td-p/13490085
    zoom to 50% centered view
    by jazz-y
    */

    #target photoshop

    var s2t = stringIDToTypeID,
    zoom = 0.46; // 46% zoom

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('resolution'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var res = executeActionGet(r).getInteger(p);

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('width'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var docW = executeActionGet(r).getDouble(p) * res / 72;

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('height'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    var docH = executeActionGet(r).getDouble(p) * res / 72;

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('zoom'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    (d = new ActionDescriptor()).putReference(s2t('null'), r);
    (d1 = new ActionDescriptor()).putUnitDouble(s2t('zoom'), s2t('percentUnit'), zoom);
    d.putObject(s2t('to'), p, d1);
    executeAction(s2t('set'), d, DialogModes.NO);

    (r = new ActionReference).putProperty(s2t('property'), s2t('center'));
    r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
    (d = new ActionDescriptor).putReference(s2t('target'), r);
    (d1 = new ActionDescriptor()).putUnitDouble(s2t('horizontal'), s2t('distanceUnit'), docW * zoom / 2);
    d1.putUnitDouble(s2t('vertical'), s2t('distanceUnit'), docH * zoom / 2);
    d.putObject(s2t('to'), s2t('center'), d1);
    executeAction(s2t('set'), d, DialogModes.NO);

     

    dublove
    dubloveAuthor
    Legend
    May 5, 2026

    Hi ​@Stephen Marsh 

    Not this one.
    This is still 46% of 100%.
    I am referring to 46% of "Zoom by screen"

    You can manually test the effect:
    Select the magnifier, right-click on the screen, and then click "Zoom by screen size". For example, a zoom value (assuming 62%) is obtained at this time.
    Adjust the zoom ratio in the lower left corner to half of what it was previously (i.e., 62%*46%).

    Stephen Marsh
    Community Expert
    Community Expert
    May 5, 2026

    That might be a translation issue.

     

    Do you mean this one - fit on screen?