Scripting repetitive tasks

Explorer ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

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] ]);
}

 

TOPICS
Actions and scripting

Views

77

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 10, 2022 Apr 10, 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 * @Param...

Likes

Translate

Translate
Guide ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2022 Apr 09, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 10, 2022 Apr 10, 2022

Copy link to clipboard

Copied

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
             * @Param {boolean} color Adapt colors to local environment?
             * @Param {boolean} rotate Orient image gradients?
             * @Param {boolean} scale Match pattern size?
             * @Param {boolean} mirror Flip pattern?
             * @Param {number} opacity Opacity
             * @Param {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!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

Copy link to clipboard

Copied

LATEST

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 );
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines