Skip to main content
Desbrina
Inspiring
April 9, 2022
Answered

Scripting repetitive tasks

  • April 9, 2022
  • 2 replies
  • 475 views

I'm trying to script several reptitive tasks as follows

 

  • Select all pixels on layer "BG Colour" and replace with current Foreground colour. Same as if you command+click on the layer. The layer has a 60% opacity if it makes a difference. 
  • Select all pixels on layer "Border Colour" and replace with current Foreground colour. Same as if you command+click on the layer.
  • Replace all text with specified value (Would be happy with it just bringing up the find/replace dialogue box)

 

I've worked out how to select the layer and select a specified point, but not all pixels & replace colour

 

var doc = app.documents[0];
app.activeDocument = doc;
var layers = app.activeDocument.layers;

// Text BG
var layerName = "Text BG";
var myLayer = layers[layerName];
app.activeDocument.activeLayer = myLayer;
makeSelection(224,439,102,7);

// Border Colour

// Selection Function
// x, y, width, height
// x & y position from top left corner of document
// width & height of selection
function makeSelection(x,y,sw,sh){
    app.activeDocument.selection.select([ [x,y], [x,y+sh], [x+sw,y+sh], [x+sw,y] ]);
}

 

This topic has been closed for replies.
Correct answer jefbr

You could use the scriptlistener plugin (if you don't know it... google it, it's really useful)

Get the output in the log files to get the content-aware code.

 

I'm not sure entirely sure the opacity makes a difference for content-aware fill, if you experiment with it and find out, do let us know! 😉

 

Because it's monday...

Here's a working -monday morning, not the cleanest code- implementation of content-aware fill only.

 

            /**Content Aware Fill from scriptlistener code
             * @9397041 {boolean} color Adapt colors to local environment?
             * @9397041 {boolean} rotate Orient image gradients?
             * @9397041 {boolean} scale Match pattern size?
             * @9397041 {boolean} mirror Flip pattern?
             * @9397041 {number} opacity Opacity
             * @9397041 {typeID} blendmode Layer blend mode as typeID... use scriptlistener to illicit more blend modes (look for "Nrml", "Dslv", "Drkn", Lghn, ...)
             */
            function cntAwrFill (color,rotate,scale,mirror,opacity,blendmode){

                var idFl = charIDToTypeID( "Fl  " );
                var idUsng = charIDToTypeID( "Usng" );
                var idFlCn = charIDToTypeID( "FlCn" );
                var idcontentAware = stringIDToTypeID( "contentAware" );
                var idOpct = charIDToTypeID( "Opct" );
                var idPrc = charIDToTypeID( "#Prc" ); 
                var idMd = charIDToTypeID( "Md  " );
                var idBlnM = charIDToTypeID( "BlnM" );
    
                var desc238 = new ActionDescriptor();
                desc238.putEnumerated( idUsng, idFlCn, idcontentAware );
                desc238.putBoolean( stringIDToTypeID( "contentAwareColorAdaptationFill" ), color ); //set
                desc238.putBoolean( stringIDToTypeID( "contentAwareRotateFill" ), rotate );
                desc238.putBoolean( stringIDToTypeID( "contentAwareScaleFill" ), scale );
                desc238.putBoolean( stringIDToTypeID( "contentAwareMirrorFill" ), mirror );
                desc238.putUnitDouble( idOpct, idPrc, opacity ); //Opacity value, can be anything between 0 & 100
                desc238.putEnumerated( idMd, idBlnM, blendmode ); 
                executeAction( idFl, desc238, DialogModes.NO );
    
            }

            cntAwrFill(true,true,true,false,100, charIDToTypeID( "Nrml" ));

 

 Best of luck to ya!

2 replies

jefbrCorrect answer
Inspiring
April 11, 2022

You could use the scriptlistener plugin (if you don't know it... google it, it's really useful)

Get the output in the log files to get the content-aware code.

 

I'm not sure entirely sure the opacity makes a difference for content-aware fill, if you experiment with it and find out, do let us know! 😉

 

Because it's monday...

Here's a working -monday morning, not the cleanest code- implementation of content-aware fill only.

 

            /**Content Aware Fill from scriptlistener code
             * @9397041 {boolean} color Adapt colors to local environment?
             * @9397041 {boolean} rotate Orient image gradients?
             * @9397041 {boolean} scale Match pattern size?
             * @9397041 {boolean} mirror Flip pattern?
             * @9397041 {number} opacity Opacity
             * @9397041 {typeID} blendmode Layer blend mode as typeID... use scriptlistener to illicit more blend modes (look for "Nrml", "Dslv", "Drkn", Lghn, ...)
             */
            function cntAwrFill (color,rotate,scale,mirror,opacity,blendmode){

                var idFl = charIDToTypeID( "Fl  " );
                var idUsng = charIDToTypeID( "Usng" );
                var idFlCn = charIDToTypeID( "FlCn" );
                var idcontentAware = stringIDToTypeID( "contentAware" );
                var idOpct = charIDToTypeID( "Opct" );
                var idPrc = charIDToTypeID( "#Prc" ); 
                var idMd = charIDToTypeID( "Md  " );
                var idBlnM = charIDToTypeID( "BlnM" );
    
                var desc238 = new ActionDescriptor();
                desc238.putEnumerated( idUsng, idFlCn, idcontentAware );
                desc238.putBoolean( stringIDToTypeID( "contentAwareColorAdaptationFill" ), color ); //set
                desc238.putBoolean( stringIDToTypeID( "contentAwareRotateFill" ), rotate );
                desc238.putBoolean( stringIDToTypeID( "contentAwareScaleFill" ), scale );
                desc238.putBoolean( stringIDToTypeID( "contentAwareMirrorFill" ), mirror );
                desc238.putUnitDouble( idOpct, idPrc, opacity ); //Opacity value, can be anything between 0 & 100
                desc238.putEnumerated( idMd, idBlnM, blendmode ); 
                executeAction( idFl, desc238, DialogModes.NO );
    
            }

            cntAwrFill(true,true,true,false,100, charIDToTypeID( "Nrml" ));

 

 Best of luck to ya!

Desbrina
DesbrinaAuthor
Inspiring
April 13, 2022

Thanks, while i didn't use this directly the script listener info did help me.

 

I ended up with

 

var layerName = "Text BG";
var myLayer = layers[layerName];
app.activeDocument.activeLayer = myLayer;
app.activeDocument.selection.deselect();
fillWithForegroundColour();

// Border Colour
layerName = "Border Colour";
myLayer = layers[layerName];
app.activeDocument.activeLayer = myLayer;
app.activeDocument.selection.deselect();
fillWithForegroundColour();

// Replace Text
// var idfindReplace = stringIDToTypeID( "findReplace" );
executeAction(stringIDToTypeID('findReplace'), undefined)

// Functions

function fillWithForegroundColour() {
	var idfill = stringIDToTypeID( "fill" );
	var desc235 = new ActionDescriptor();
	var idusing = stringIDToTypeID( "using" );
	var idfillContents = stringIDToTypeID( "fillContents" );
	var idforegroundColor = stringIDToTypeID( "foregroundColor" );
	desc235.putEnumerated( idusing, idfillContents, idforegroundColor );
	var idopacity = stringIDToTypeID( "opacity" );
	var idpercentUnit = stringIDToTypeID( "percentUnit" );
	desc235.putUnitDouble( idopacity, idpercentUnit, 100.000000 );
	var idmode = stringIDToTypeID( "mode" );
	var idblendMode = stringIDToTypeID( "blendMode" );
	var idnormal = stringIDToTypeID( "normal" );
	desc235.putEnumerated( idmode, idblendMode, idnormal );
	executeAction( idfill, desc235, DialogModes.NO );
}
Earth Oliver
Legend
April 9, 2022

If you're just selecting pixels on a named transparent layer, can't you simply record an action for that?

Desbrina
DesbrinaAuthor
Inspiring
April 9, 2022

I've tried that but for 1&2 but when i run the action nothing happens