Setting active layers and applying filters

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

Moderators: xbytor, Kukurykus, Tom

horshack
Posts: 2
Joined: Fri Feb 28, 2020 5:57 pm

Setting active layers and applying filters

Post by horshack »

I wrote a script to apply a high-pass filter to every layer at the root layer hierarchy:

Code: Select all

function applyHighPassFilterAllLayers(radius) {
    var doc = activeDocument;
    var layers = activeDocument.layers;
    for (var layerIndex=0; layerIndex < layers.length; layerIndex++) {
        var layer = layers[layerIndex];
        doc.activeLayer = layer;
        layer.applyHighPass(radius);
    }
}
The above script works if any layer is active in the UI prior to running the script (ie, a single layer is highlighted/selected in the layers panel). However, if no layer is active in the UI then the script fails with PS reporting "The command High Pass it not currently available". This seems to occur because PS doesn't think any layer is active even though my code is setting the active layer inside the loop. If I examine activeDocument.activeLayer prior to entering my loop I see it the first layer is the active layer even though it's not visibly active in the UI, yet it still fails. With trial and error I can get the script to work in this scenario if I set a different layer as active prior to the loop. For example:

Code: Select all

function applyHighPassFilterAllLayers(radius) {
    var doc = activeDocument;
    var layers = activeDocument.layers;
    doc.activeLayer = layers[1]; // temporary workaround - set second layer as active prior to entering layer loop below
    for (var layerIndex=0; layerIndex < layers.length; layerIndex++) {
        var layer = layers[layerIndex];
        doc.activeLayer = layer;
        layer.applyHighPass(radius);
    }
}
Any ideas on how I can get this to work without the temporary workaround I put in?

User avatar
Kukurykus
Posts: 240
Joined: Mon Jul 25, 2016 12:36 pm

Re: Setting active layers and applying filters

Post by Kukurykus »

Use 'select top layer' workaround instead of your current workaround:

Code: Select all

activeDocument.artLayers.add().remove()

horshack
Posts: 2
Joined: Fri Feb 28, 2020 5:57 pm

Re: Setting active layers and applying filters

Post by horshack »

Kukurykus wrote:
Sat Feb 29, 2020 1:08 am
Use 'select top layer' workaround instead of your current workaround:

Code: Select all

activeDocument.artLayers.add().remove()
Thanks. Do you know the background as to why either workaround method is necessary? Is it just a bug in Adobe's scripting logic where the internal representation of the layer being active doesn't match the UI presentation?

User avatar
Kukurykus
Posts: 240
Joined: Mon Jul 25, 2016 12:36 pm

Re: Setting active layers and applying filters

Post by Kukurykus »

Yes that's the known bug, Adobe doesn't fix from long.