Skip to main content
Participating Frequently
July 18, 2023
Answered

Applying the last Liquify filter to a layer using scripting?

  • July 18, 2023
  • 1 reply
  • 651 views

I was wondering if anyone has been able to use Javascript to script out applying the last Liquify Filter to a different layer? So far I have this, but it doesn't seem to be working. As you can see from the code, I've tried multiple ways, none of which throw an error, but all of which don't show the liquify result 😞

#target photoshop

app.bringToFront();
var doc = app.activeDocument;

function cTID(s) {
    return app.charIDToTypeID(s);
}

function sTID(s) {
    return app.stringIDToTypeID(s);
}

var colorGroup = doc.layerSets.getByName("color"); // assuming the group name is "color"

function applyLiquify() {
    var dialogMode = DialogModes.NO;
    var idLqFy = charIDToTypeID( "LqFy" );
    try {
        app.executeAction(idLqFy, undefined, dialogMode);
        return true;
    } catch (e) {
        alert("applyLiquify" + e + " " + e.line);
    }
    return false;
}


function repeatlastfilter() {
    try {
        app.runMenuItem(cTID("LstF"), false);
        return true;
    } catch (e) {
        alert("repeatlastfilter:" + e + " " + e.line);
    }
    return false;
}

function lastFilter(){
    var dialogMode = DialogModes.NO;
    var d = new ActionDescriptor();
    var r = new ActionReference();
    r.putEnumerated( app.charIDToTypeID('Mn  '), app.charIDToTypeID('MnIt'), app.charIDToTypeID('LstF') );
    d.putReference( app.charIDToTypeID('null'), r );
    try{executeAction( app.charIDToTypeID('slct'), d, dialogMode );}catch(e){alert(e+" "+e.line);return false;};
    return true;
}

function applyLiquifyToLayers(layers) {
    for (var i = 0; i < layers.length; i++) {
        var layer = layers[i];
        if (layer.name === "filename") {
            continue;
        }

        // check if this layer is a group; if so apply to sub layers
        // if (layer.typename === "LayerSet") {
        //     applyLiquifyToLayers(layer.layers);
        // }
        doc.activeLayer = layer;
        var success = selectLayerOrLayerMask();
        alert("selectLayerOrLayerMask success:" + success);
        // success = lastFilter();
        // alert("lastFilter success:" + success);
        success = applyLiquify();
        alert("applyLiquify success:" + success);
        // success = repeatlastfilter();
        // alert("repeatlastfilter success:" + success);
        break;
    }
    return true;
}

function selectLayerOrLayerMask() {
    var success = false;
    try {
        success = Select_Layer_Mask();
    } catch (err) {
    }
    if (success === false) {
        alert("Selecting layer mask failed. Selecting original layer.");
        Select_Original_Layer();
    }
    return success;
}


// SELECT ORIGINAL LAYER
function Select_Original_Layer() {
    try {
        var dialogMode = DialogModes.NO;
        var selectOriginalLayer_Desc1 = new ActionDescriptor();
        var selectOriginalLayer_ref1 = new ActionReference();
        selectOriginalLayer_ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), sTID("RGB"));
        selectOriginalLayer_Desc1.putReference(cTID('null'), selectOriginalLayer_ref1);
        selectOriginalLayer_Desc1.putBoolean(cTID('MkVs'), false);
        app.executeAction(cTID('slct'), selectOriginalLayer_Desc1, dialogMode);
        return true;
    } catch (e) {
        return false;
    }
}


// SELECT LAYER MASK
function Select_Layer_Mask() {
    try {
        var dialogMode = DialogModes.NO;
        var togglelayermask_desc1 = new ActionDescriptor();
        var togglelayermask_ref1 = new ActionReference();
        togglelayermask_ref1.putEnumerated(cTID('Chnl'), cTID('Chnl'), cTID('Msk '))
        togglelayermask_desc1.putReference(cTID('null'), togglelayermask_ref1)
        togglelayermask_desc1.putBoolean(cTID('MkVs'), true)
        app.executeAction(cTID('slct'), togglelayermask_desc1, dialogMode)
    } catch (e) {
        return false;
    }

    return true;
}


applyLiquifyToLayers(colorGroup.layers);

 

This topic has been closed for replies.
Correct answer r-bin
On new Photoshops "Last Filter" does not work with "Liquify". On CS6, for example, it still works.
Try instead of calling "Last Filter" to use the code to apply the last used mesh.
 

 

 

var d = new ActionDescriptor();
d.putString(charIDToTypeID("LqMD"), app.preferencesFolder.fsName+"\\"+"Liquify Last Mesh.psp");
executeAction(charIDToTypeID("LqFy"), d, DialogModes.NO);

 

 

P.S. This code is for windows. For Mac, there is no way to check.

 

 

 

1 reply

Participating Frequently
July 18, 2023

I have also tried using different actions that apply the last filter, but even running the action manually fails. The only way I have been able to run the last liquify command is by clicking in the GUI or using the keyboard shortcut manually, which doesn't seem right... This has to be automatable, right?

r-binCorrect answer
Legend
July 18, 2023
On new Photoshops "Last Filter" does not work with "Liquify". On CS6, for example, it still works.
Try instead of calling "Last Filter" to use the code to apply the last used mesh.
 

 

 

var d = new ActionDescriptor();
d.putString(charIDToTypeID("LqMD"), app.preferencesFolder.fsName+"\\"+"Liquify Last Mesh.psp");
executeAction(charIDToTypeID("LqFy"), d, DialogModes.NO);

 

 

P.S. This code is for windows. For Mac, there is no way to check.

 

 

 
Participating Frequently
July 18, 2023

Holy Guacamole, you're a genius.