Skip to main content
Known Participant
April 5, 2023
Answered

How to center and zoom Photoshop document window based on active layer?

  • April 5, 2023
  • 1 reply
  • 563 views

Hi,

 

I am trying to make the script center and zoom (to specific %) the document window based on the active layer that its being processed.

 

In other words, i would like to achieve a similar user experience as in when you Alt + Click on a layer thumbnail and the content of that layer is then centered and zoomed to fill your whole document window.

 

The only difference is that when you perform the alt + click, it zooms in to around 12.5% and i would like it to be 25% zoomin, but my current script modifies multiple layers and asks me to do something for each layer so i want it to zoom in and center my view depending on which layer is being active at the moment. This latter functionality i got it, i just havent been able to achieve the center and zoom.

 

Right now i got this function but it doesnt seem to work properly when i call it.

 

If you can provide how should the funciton look like, i will appreciate it. Thanks!

 

function zoomInOnActiveLayer(doc, zoomFactor) {
    var originalRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    var centerX = doc.activeLayer.bounds[0].value + (doc.activeLayer.bounds[2].value - doc.activeLayer.bounds[0].value) / 2;
    var centerY = doc.activeLayer.bounds[1].value + (doc.activeLayer.bounds[3].value - doc.activeLayer.bounds[1].value) / 2;

    if (app.activeDocument.activeWindow) {
        app.activeDocument.activeWindow.scrollCenterOnAnchor(new UnitValue(centerX, "px"), new UnitValue(centerY, "px"));
        app.activeDocument.activeWindow.zoom = zoomFactor;
    }

    app.preferences.rulerUnits = originalRulerUnits;
}
This topic has been closed for replies.
Correct answer jazz-y

Scripting the "zoom to layers bounds" feature...

 

#target photoshop;
zoomToLayer(app.activeDocument.activeLayer.id, 0.5)

function zoomToLayer(layerID, zoom) {
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID,
        lrBounds = getProperty('layer', 'bounds', layerID),
        x = lrBounds.getUnitDoubleValue(s2t('left')) + lrBounds.getUnitDoubleValue(s2t('width')) / 2,
        y = lrBounds.getUnitDoubleValue(s2t('top')) + lrBounds.getUnitDoubleValue(s2t('height')) / 2,
        w = lrBounds.getUnitDoubleValue(s2t('width')),
        h = lrBounds.getUnitDoubleValue(s2t('height'));
    var activeView = getProperty('document', 'viewInfo').getObjectValue(s2t('activeView')).getObjectValue(s2t('globalBounds'));
    docW = activeView.getDouble(s2t('right')) - activeView.getDouble(s2t('left')),
        docH = activeView.getDouble(s2t('bottom')) - activeView.getDouble(s2t('top')),
        k = Math.min(docW / w, docH / h) * (zoom ? zoom : 1);
    (d = new ActionDescriptor()).putUnitDouble(s2t('zoom'), s2t('percentUnit'), k);
    setProperty('document', 'zoom', d);
    (d = new ActionDescriptor()).putUnitDouble(s2t('horizontal'), s2t('distanceUnit'), x * k);
    d.putUnitDouble(s2t('vertical'), s2t('distanceUnit'), y * k);
    setProperty('document', 'center', d);
    function getProperty(object, property, id) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t(property));
        id ? r.putIdentifier(s2t(object), id) : r.putEnumerated(s2t(object), s2t('ordinal'), s2t('targetEnum'));
        return getValue(executeActionGet(r), p)
        function getValue(d, id) {
            switch (d.getType(id)) {
                case DescValueType.OBJECTTYPE: return d.getObjectValue(id);
                case DescValueType.LISTTYPE: return d.getList(id);
                case DescValueType.REFERENCETYPE: return d.getReference(id);
                case DescValueType.BOOLEANTYPE: return d.getBoolean(id);
                case DescValueType.STRINGTYPE: return d.getString(id);
                case DescValueType.INTEGERTYPE: return d.getInteger(id);
                case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(id);
                case DescValueType.DOUBLETYPE: return d.getDouble(id);
                case DescValueType.ALIASTYPE: return d.getPath(id);
                case DescValueType.CLASSTYPE: return d.getClass(id);
                case DescValueType.UNITDOUBLE: return { value: d.getUnitDoubleValue(id), type: t2s(d.getUnitDoubleType(id)) };
                case DescValueType.ENUMERATEDTYPE: return { value: t2s(d.getEnumerationValue(id)), type: t2s(d.getEnumerationType(id)) };
            }
        }
    }
    function setProperty(target, property, desc) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t(property));
        r.putEnumerated(s2t(target), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor).putReference(s2t('null'), r);
        d.putObject(s2t('to'), p, desc);
        executeAction(s2t('set'), d, DialogModes.NO);
    }
}

1 reply

jazz-yCorrect answer
Legend
April 5, 2023

Scripting the "zoom to layers bounds" feature...

 

#target photoshop;
zoomToLayer(app.activeDocument.activeLayer.id, 0.5)

function zoomToLayer(layerID, zoom) {
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID,
        lrBounds = getProperty('layer', 'bounds', layerID),
        x = lrBounds.getUnitDoubleValue(s2t('left')) + lrBounds.getUnitDoubleValue(s2t('width')) / 2,
        y = lrBounds.getUnitDoubleValue(s2t('top')) + lrBounds.getUnitDoubleValue(s2t('height')) / 2,
        w = lrBounds.getUnitDoubleValue(s2t('width')),
        h = lrBounds.getUnitDoubleValue(s2t('height'));
    var activeView = getProperty('document', 'viewInfo').getObjectValue(s2t('activeView')).getObjectValue(s2t('globalBounds'));
    docW = activeView.getDouble(s2t('right')) - activeView.getDouble(s2t('left')),
        docH = activeView.getDouble(s2t('bottom')) - activeView.getDouble(s2t('top')),
        k = Math.min(docW / w, docH / h) * (zoom ? zoom : 1);
    (d = new ActionDescriptor()).putUnitDouble(s2t('zoom'), s2t('percentUnit'), k);
    setProperty('document', 'zoom', d);
    (d = new ActionDescriptor()).putUnitDouble(s2t('horizontal'), s2t('distanceUnit'), x * k);
    d.putUnitDouble(s2t('vertical'), s2t('distanceUnit'), y * k);
    setProperty('document', 'center', d);
    function getProperty(object, property, id) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t(property));
        id ? r.putIdentifier(s2t(object), id) : r.putEnumerated(s2t(object), s2t('ordinal'), s2t('targetEnum'));
        return getValue(executeActionGet(r), p)
        function getValue(d, id) {
            switch (d.getType(id)) {
                case DescValueType.OBJECTTYPE: return d.getObjectValue(id);
                case DescValueType.LISTTYPE: return d.getList(id);
                case DescValueType.REFERENCETYPE: return d.getReference(id);
                case DescValueType.BOOLEANTYPE: return d.getBoolean(id);
                case DescValueType.STRINGTYPE: return d.getString(id);
                case DescValueType.INTEGERTYPE: return d.getInteger(id);
                case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(id);
                case DescValueType.DOUBLETYPE: return d.getDouble(id);
                case DescValueType.ALIASTYPE: return d.getPath(id);
                case DescValueType.CLASSTYPE: return d.getClass(id);
                case DescValueType.UNITDOUBLE: return { value: d.getUnitDoubleValue(id), type: t2s(d.getUnitDoubleType(id)) };
                case DescValueType.ENUMERATEDTYPE: return { value: t2s(d.getEnumerationValue(id)), type: t2s(d.getEnumerationType(id)) };
            }
        }
    }
    function setProperty(target, property, desc) {
        (r = new ActionReference()).putProperty(s2t('property'), p = s2t(property));
        r.putEnumerated(s2t(target), s2t('ordinal'), s2t('targetEnum'));
        (d = new ActionDescriptor).putReference(s2t('null'), r);
        d.putObject(s2t('to'), p, desc);
        executeAction(s2t('set'), d, DialogModes.NO);
    }
}