Skip to main content
Known Participant
September 17, 2024
Answered

How do I Automatically apply Filter with a script?

  • September 17, 2024
  • 3 replies
  • 692 views

I have a process in which I apply a filter to the specific active layer, I do it many times and I find that making a script would save me a lot of time. I think I could do it with an action but I'm doing other process together so finding out how to do it with a script would be much better for me.

How would I write a code that applies a filter to the selected active layer? Any filter? And how could I auto add the level of the filter on the script if it has any?

For example: If I wanna add a mosaic filter to the active layer and I want it to always have a cell size of 100, how could I incorporate that into a script?

This topic has been closed for replies.
Correct answer Stephen Marsh

@HeyoThere 

 

Using legacy ExtendScript code, you can use DOM code for supported filters. Such as:

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/ArtLayer.html

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/ArtLayer/applyAddNoise.html#artlayer-applyaddnoise

 

app.activeDocument.activeLayer.applyAddNoise(10, NoiseDistribution.GAUSSIAN, true);

 

Or using legacy AM code recorded via the ScriptingListener plugin:

 

var idaddNoise = stringIDToTypeID( "addNoise" );
    var desc187 = new ActionDescriptor();
    var iddistort = stringIDToTypeID( "distort" );
    var iddistort = stringIDToTypeID( "distort" );
    var iduniformDistribution = stringIDToTypeID( "uniformDistribution" );
    desc187.putEnumerated( iddistort, iddistort, iduniformDistribution );
    var idnoise = stringIDToTypeID( "noise" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc187.putUnitDouble( idnoise, idpercentUnit, 10.000000 );
    var idmonochromatic = stringIDToTypeID( "monochromatic" );
    desc187.putBoolean( idmonochromatic, true );
    var idFlRs = charIDToTypeID( "FlRs" );
    desc187.putInteger( idFlRs, 204982 );
executeAction( idaddNoise, desc187, DialogModes.NO );

 

 Some filters are not available in the DOM and can only be run via AM code:

 

var idmosaic = stringIDToTypeID( "mosaic" );
    var desc207 = new ActionDescriptor();
    var idcellSize = stringIDToTypeID( "cellSize" );
    var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
    desc207.putUnitDouble( idcellSize, idpixelsUnit, 100.000000 );
executeAction( idmosaic, desc207, DialogModes.NO );

 

Or refactored into a function call and function:

 

mosaic(100);

function mosaic(cellSize) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	descriptor.putUnitDouble( s2t( "cellSize" ), s2t( "pixelsUnit" ), cellSize );
	executeAction( s2t( "mosaic" ), descriptor, DialogModes.NO );
}

 

For the new UXP, you would either use UXP DOM code or BatchPlay recorded code:

 

https://developer.adobe.com/photoshop/uxp/2022/

 

async function actionCommands() {
    let commands = [
        // Mosaic
        {
            "_obj": "mosaic",
            "cellSize": {
                "_unit": "pixelsUnit",
                "_value": 100.0
            }
        }
    ];
    return await require("photoshop").action.batchPlay(commands, {});
}

console.log(
    await require("photoshop").core.executeAsModal(actionCommands, {"commandName": "Action Commands"})
);

 

3 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 17, 2024

@HeyoThere 

 

Using legacy ExtendScript code, you can use DOM code for supported filters. Such as:

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/ArtLayer.html

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/ArtLayer/applyAddNoise.html#artlayer-applyaddnoise

 

app.activeDocument.activeLayer.applyAddNoise(10, NoiseDistribution.GAUSSIAN, true);

 

Or using legacy AM code recorded via the ScriptingListener plugin:

 

var idaddNoise = stringIDToTypeID( "addNoise" );
    var desc187 = new ActionDescriptor();
    var iddistort = stringIDToTypeID( "distort" );
    var iddistort = stringIDToTypeID( "distort" );
    var iduniformDistribution = stringIDToTypeID( "uniformDistribution" );
    desc187.putEnumerated( iddistort, iddistort, iduniformDistribution );
    var idnoise = stringIDToTypeID( "noise" );
    var idpercentUnit = stringIDToTypeID( "percentUnit" );
    desc187.putUnitDouble( idnoise, idpercentUnit, 10.000000 );
    var idmonochromatic = stringIDToTypeID( "monochromatic" );
    desc187.putBoolean( idmonochromatic, true );
    var idFlRs = charIDToTypeID( "FlRs" );
    desc187.putInteger( idFlRs, 204982 );
executeAction( idaddNoise, desc187, DialogModes.NO );

 

 Some filters are not available in the DOM and can only be run via AM code:

 

var idmosaic = stringIDToTypeID( "mosaic" );
    var desc207 = new ActionDescriptor();
    var idcellSize = stringIDToTypeID( "cellSize" );
    var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
    desc207.putUnitDouble( idcellSize, idpixelsUnit, 100.000000 );
executeAction( idmosaic, desc207, DialogModes.NO );

 

Or refactored into a function call and function:

 

mosaic(100);

function mosaic(cellSize) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	descriptor.putUnitDouble( s2t( "cellSize" ), s2t( "pixelsUnit" ), cellSize );
	executeAction( s2t( "mosaic" ), descriptor, DialogModes.NO );
}

 

For the new UXP, you would either use UXP DOM code or BatchPlay recorded code:

 

https://developer.adobe.com/photoshop/uxp/2022/

 

async function actionCommands() {
    let commands = [
        // Mosaic
        {
            "_obj": "mosaic",
            "cellSize": {
                "_unit": "pixelsUnit",
                "_value": 100.0
            }
        }
    ];
    return await require("photoshop").action.batchPlay(commands, {});
}

console.log(
    await require("photoshop").core.executeAsModal(actionCommands, {"commandName": "Action Commands"})
);

 

HeyoThereAuthor
Known Participant
September 17, 2024

Thank you!! This is going to help me a lot!

Stephen Marsh
Community Expert
Community Expert
September 17, 2024

@HeyoThere -- You're welcome.