Skip to main content
Participating Frequently
January 4, 2023
Answered

Please help, script for select Elliptical Marquee Tool wont work

  • January 4, 2023
  • 4 replies
  • 581 views

Hi there! - Please help me.
I'm trying to automate average blur circles using the Elliptical Marquee Tool across the canvas within a grid and I keep getting the Rectangular Marquee Tool instead. Also the average blur isnt working, it's giving me the colour thats I've eyedropped previously. Please see below script.
@JJMack maybe you might know?

// Function to create a guide
function createGuide(position, direction) {
var id10 = charIDToTypeID( "Mk " );
var desc3 = new ActionDescriptor();
var id11 = charIDToTypeID( "Nw " );
var desc4 = new ActionDescriptor();
var id12 = charIDToTypeID( "Pstn" );
desc4.putInteger( id12, position );
var id13 = charIDToTypeID( "Ornt" );
desc4.putEnumerated( id13, id13, charIDToTypeID(direction) );
var id14 = charIDToTypeID( "Gd " );
desc3.putObject( id11, id14, desc4 );
executeAction( id10, desc3, DialogModes.NO );
}

// Set up variables
var numColumns = 56;
var numRows = 39;
var gridSize = 250;

// Create the grid of guides
for (var i = 0; i < numColumns; i++) {
var x = i * gridSize;
createGuide(x, "Vrtc");
}
for (var j = 0; j < numRows; j++) {
var y = j * gridSize;
createGuide(y, "Hrzn");
}

// Create the Elliptical Marquee tool for each grid and find the blur average
for (var i = 0; i < numColumns; i++) {
for (var j = 0; j < numRows; j++) {
// Select the grid
var x = i * gridSize;
var y = j * gridSize;
activeDocument.selection.select([[x, y], [x + gridSize, y], [x + gridSize, y + gridSize], [x, y + gridSize]], SelectionType.REPLACE);

// Create a new layer for the blur average
var newLayer = activeDocument.artLayers.add();
newLayer.name = "Blur Average " + (i + 1) + ", " + (j + 1);

// Fill the selection with the blur average
activeDocument.selection.fill(app.foregroundColor);
activeDocument.selection.deselect();
}
}

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

@defaultcjgyuglr6yh5 – Sadly JJMack has passed on.

 

Here is code for a "select all" version using the elliptical marquee which you could adapt:

 

https://gist.github.com/MarshySwamp/451f60096079084b818d693d8cf09547

 

ellipticalSelectAll(0, 0, app.activeDocument.height.value, app.activeDocument.width.value, true);

function ellipticalSelectAll(top, left, bottom, right, antiAlias) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor2.putUnitDouble( s2t( "top" ), s2t( "pixelsUnit" ), top );
	descriptor2.putUnitDouble( s2t( "left" ), s2t( "pixelsUnit" ), left );
	descriptor2.putUnitDouble( s2t( "bottom" ), s2t( "pixelsUnit" ), bottom );
	descriptor2.putUnitDouble( s2t( "right" ), s2t( "pixelsUnit" ), right );
	descriptor.putObject( s2t( "to" ), s2t( "ellipse" ), descriptor2 );
	descriptor.putBoolean( s2t( "antiAlias" ), antiAlias );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

 

4 replies

c.pfaffenbichler
Community Expert
Community Expert
January 7, 2023

I am not completely sure what you want to achieve but you can try this. 

// create elliptical averaged layers in x columns and y rows;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
app.togglePalettes();
// the number of colums;
var theColumns = 10;
// the number of rows;
var theRows = 20;
// determine values;
var myDocument = app.activeDocument;
var theWidth = myDocument.width;
var theHeight = myDocument.height;
var columnWidth = theWidth/theColumns;
var rowHeight = theHeight/theRows;
// create merged layers;
myDocument.artLayers.add();
var desc23 = new ActionDescriptor();
desc23.putBoolean( stringIDToTypeID( "duplicate" ), true );
executeAction( stringIDToTypeID( "mergeVisible" ), desc23, DialogModes.NO );
//var thisLayer = myDocument.activeLayer;
var thisLayerId = getLayerId();
// create the averaged layers;
for (var m = 0; m < theColumns; m++) {
    for (var n = 0; n < theRows; n++) {
        ellipticalSelection([m*columnWidth, n*rowHeight, m*columnWidth+columnWidth, n*rowHeight+rowHeight]);
// average;
        executeAction( charIDToTypeID( "Avrg" ), undefined, DialogModes.NO );
// layer via copy;
        executeAction( charIDToTypeID( "CpTL" ), undefined, DialogModes.NO );
// select merged layer;
        selectLayerByID(thisLayerId, false);
    }
};
// delete merged layer;
myDocument.activeLayer.remove();
// reset;
app.preferences.rulerUnits = originalRulerUnits;
app.togglePalettes();
};
////// stuff //////
function ellipticalSelection (theBounds) {
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
// =======================================================
var idset = stringIDToTypeID( "set" );
    var desc20 = new ActionDescriptor();
        var ref6 = new ActionReference();
        ref6.putProperty( stringIDToTypeID( "channel" ), stringIDToTypeID( "selection" ) );
    desc20.putReference( stringIDToTypeID( "null" ), ref6 );
        var desc21 = new ActionDescriptor();
        desc21.putUnitDouble( stringIDToTypeID( "top" ), idpixelsUnit, theBounds[1] );
        desc21.putUnitDouble( stringIDToTypeID( "left" ), idpixelsUnit, theBounds[0] );
        desc21.putUnitDouble( stringIDToTypeID( "bottom" ), idpixelsUnit, theBounds[3] );
        desc21.putUnitDouble( stringIDToTypeID( "right" ), idpixelsUnit, theBounds[2] );
    desc20.putObject( stringIDToTypeID( "to" ), stringIDToTypeID( "ellipse" ), desc21 );
// anti-aliasing or not;
    desc20.putBoolean( stringIDToTypeID( "antiAlias" ), false );
executeAction( idset, desc20, DialogModes.NO );

};
////// get active layer’s identifier //////
function getLayerId () {
    var ref = new ActionReference();
    ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("layerID"));
    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
    return executeActionGet(ref).getInteger(stringIDToTypeID("layerID"));
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){ 
    add = undefined ? add = false:add 
    var ref = new ActionReference();
        ref.putIdentifier(charIDToTypeID("Lyr "), id);
        var desc = new ActionDescriptor();
        desc.putReference(charIDToTypeID("null"), ref );
            if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
            desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
        try{
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
    }catch(e){/*alert(e.message)*/}
};
Participating Frequently
January 8, 2023

Hey c.pfaffenbichler,
Thanks for the above, I ended up solving this myself but much appreciate your help. 
Regards

c.pfaffenbichler
Community Expert
Community Expert
January 6, 2023

Could you post an example of an image and the result you want to get? 

c.pfaffenbichler
Community Expert
Community Expert
January 4, 2023

Also the average blur isnt working, it's giving me the colour thats I've eyedropped previously.

Could you please post screenshots from before and after applying the Smart Filter with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

Participating Frequently
January 5, 2023

Appologies for the late response. Sure please see the attached. 

Weirdly my amended code worked for the above response and i went to use again and nothing happened..

Let me know if this helps 🙂

 

 

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
January 4, 2023

@defaultcjgyuglr6yh5 – Sadly JJMack has passed on.

 

Here is code for a "select all" version using the elliptical marquee which you could adapt:

 

https://gist.github.com/MarshySwamp/451f60096079084b818d693d8cf09547

 

ellipticalSelectAll(0, 0, app.activeDocument.height.value, app.activeDocument.width.value, true);

function ellipticalSelectAll(top, left, bottom, right, antiAlias) {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor2.putUnitDouble( s2t( "top" ), s2t( "pixelsUnit" ), top );
	descriptor2.putUnitDouble( s2t( "left" ), s2t( "pixelsUnit" ), left );
	descriptor2.putUnitDouble( s2t( "bottom" ), s2t( "pixelsUnit" ), bottom );
	descriptor2.putUnitDouble( s2t( "right" ), s2t( "pixelsUnit" ), right );
	descriptor.putObject( s2t( "to" ), s2t( "ellipse" ), descriptor2 );
	descriptor.putBoolean( s2t( "antiAlias" ), antiAlias );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

 

Participating Frequently
January 5, 2023

Ohh no! Thats horrible news, thank you for sharing 😞
Thanks for that! I've amended and it looks to be drawing correctly now.. now to fix more issues! aha

Stephen Marsh
Community Expert
Community Expert
January 5, 2023

If my code is the correct answer, please mark it so.