Skip to main content
dublove
Legend
May 5, 2026
Answered

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

  • May 5, 2026
  • 22 replies
  • 1113 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);

 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    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);
    }

     

    22 replies

    Participating Frequently
    September 2, 2026

    very nice bro keep it up

     

    Participating Frequently
    September 2, 2026

    love to read it

    Participant
    August 27, 2026

    This is a really useful explanation of the difference between a fixed 46% zoom and 46% of the current “Fit on Screen” zoom. The FACTOR = 0.46 approach makes the script much more flexible, especially when the screen size changes.

    I also work on web content and maintain a U website, so I appreciate examples like this that show how small technical adjustments can make a better user experience.

    For the original Photoshop question, I think the calculated-factor approach is the better solution because it first determines the current Fit on Screen value and then applies the requested percentage.

    ( URL removed by moderator)

    Code Metrix
    Participant
    May 29, 2026

    You can also make the zoom handling a bit smoother by reducing unnecessary redraws during the process. In some Photoshop scripts, temporary UI redraw suspension helps minimize flickering when switching between “Fit on Screen” and the calculated zoom level.

    Another thing that may help is using a direct zoom calculation after retrieving the current fit value instead of triggering multiple view actions repeatedly.

    We had a similar discussion while optimizing Photoshop workflow scripts for smoother UI handling and custom scaling behavior on large displays:

    The current approach shared by @Stephen Marsh is still one of the better Extend Script-based solutions for dynamic fit-percentage zooming.

    (URL removed by moderator)

    jesse_5311
    Participating Frequently
    May 23, 2026

    Yes, it should be possible. The current script is using the built-in “Fit on Screen” command, so you would probably need to replace it with a custom zoom action or set the zoom percentage directly through scripting to reach exactly 46%.

    Taseer123
    Participant
    May 18, 2026

    Yes, but “Fit on Screen” itself can’t be changed to a fixed percentage — it always auto-calculates based on screen size.

    If you want 46% zoom, you need to set the view zoom manually instead of using FitOnScreen.

    Try this:

    app.activeDocument.activeView.zoom = 0.46;

    Note:

    • FitOnScreen = dynamic (changes based on window size)

    • activeView.zoom = fixed percentage (what you want)

    So just replace your current “FitOnScreen” action with a zoom value.

    Hope that helps 👍

    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?