Skip to main content
Inspiring
June 5, 2021
Answered

Get the number of active selections

  • June 5, 2021
  • 5 replies
  • 5290 views

A photoshop document has multiple active selections on the canvas.

A square selection, around selection, and irregular selection for example.

Is it possible to alert the number of active selections at a given time?

 

 

This topic has been closed for replies.
Correct answer Kukurykus
function slctns() {
	return sTT = stringIDToTypeID,
	aHS = (aD = activeDocument).activeHistoryState,
	(ref1 = new ActionReference()).putClass(sTT('path')),
	(dsc = new ActionDescriptor()).putReference(sTT('null'), ref1),
	(ref2 = new ActionReference()).putProperty(sTT('selectionClass'), sTT('selection')),
	dsc.putReference(sTT('from'), ref2), dsc.putUnitDouble(sTT('tolerance'), sTT('pixelsUnit'), .5),
	executeAction(sTT('make'), dsc), lngth = aD.pathItems[0].subPathItems.length, aD.activeHistoryState = aHS, lngth
}

!(slctns() - 1) && alert('Your Code')

5 replies

Legend
June 5, 2021
var file = new File(Folder.temp.fsName + "/" + "tmp");

var d = new ActionDescriptor();
executeAction(stringIDToTypeID("deleteMeasurements"), d, DialogModes.NO);

var d = new ActionDescriptor();
d.putEnumerated(stringIDToTypeID("source"), stringIDToTypeID("measurementSource"), stringIDToTypeID("selection"));
var list = new ActionList();
list.putEnumerated(stringIDToTypeID("measurementDataPoint"), stringIDToTypeID("Count"));
d.putList(stringIDToTypeID("dataPoints"), list);
var d = executeAction(stringIDToTypeID("recordMeasurements"), d, DialogModes.NO);

var d = new ActionDescriptor();
d.putPath(stringIDToTypeID("file"), file);
executeAction(stringIDToTypeID("exportMeasurements"), d, DialogModes.NO);

var d = new ActionDescriptor();
executeAction(stringIDToTypeID("deleteMeasurements"), d, DialogModes.NO);

file.open("r");

var s = file.read();

file.close();
file.remove();

alert(s);

JJMack
Community Expert
Community Expert
June 5, 2021

"A photoshop document has multiple active selections on the canvas.

A square selection, around selection, and irregular selection for example.

Is it possible to alert the number of active selections at a given time?"

 

The active selection is a single selection. The Selected pixels.  You can have Photoshop convert a selection to a Path.  The Path will contain 1 or more Path Items.  In your case the rectangle Path item will match the rectangle selected area well.  The irregular path item will come close  to matching the irregular selection area removed from the rectangle selected area.  If the selection was selecting the green and red  pixels there would be single rectangular selected area  and   single path item a rectangle path item,

 

I have no idea how well Photoshop will handle Selection opacity converting to a path.  My gut tells me very poorly.  Vectors graphics is not suited for that task..

If you know nothing about the active selection you do not want to concert it toe a path.  Here is a luminosity selection converted to a path

 

JJMack
Kukurykus
Legend
June 5, 2021

There was not said if selection is inside other selection.

JJMack
Community Expert
Community Expert
June 6, 2021

Their statement "A photoshop document has multiple active selections on the canvas."  Is nonsense. It not correct. There is or there is not an  Active selection.  It is not possible to have multiple Active selection. A selection is the selected pixels in a layer or merges layer  it what is use by Copy, Copy Merge. Cut,   Copy + Paste  (Ctrl+J).   You can have multiple Saved selection.  You can load a save selection as the active selection and you can  modify the Active selection by adding and removing selected pixels.  I can for example make the active section by using  four saves selections.   It is still the document active selection.  There is or there is not an Active selection for a document.  An  Active selection  does not have to be contiguous set of selected pixels. An Active selection could be for example be four rectangle area of the document background layer.

JJMack
Kukurykus
Legend
June 5, 2021

 

sTT = stringIDToTypeID
aHS = (aD = activeDocument).activeHistoryState;
(ref1 = new ActionReference()).putClass(sTT('path'));
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref1);
(ref2 = new ActionReference()).putProperty(sTT('selectionClass'), sTT('selection'))
dsc.putReference(sTT('from'), ref2), dsc.putUnitDouble(sTT('tolerance'), sTT('pixelsUnit'), .5)
executeAction(sTT('make'), dsc), lngth = aD.pathItems[0].subPathItems.length, aD.activeHistoryState = aHS, alert(lngth)

 

JJMack
Community Expert
Community Expert
June 5, 2021

The question was about Active selections.  You code seems to be for the current Path which can contain Path items for subpaths that can overlap and can be combined in various ways.   Vector graphics.  Photoshop can create very good selection from a Path.  You can have Photoshop create a path from as selection but that path will not normally match the selection well in most cases. Simple shape like rectangles will be good  complex selection will not become good matchinf paths.

JJMack
Kukurykus
Legend
June 5, 2021

I understand selections won't intersect each other.

Kukurykus
Legend
June 5, 2021

I know how to do it, but maybe first answer to my simple question: Jun 03, 2021

Inspiring
June 5, 2021

In reference to June 3, 2021. The last snippet worked on a Mac. The first snippet did not work on my system.

Kukurykus
Legend
June 5, 2021

I'm not saying about codes in 'Random blend mode' thread but the last code with BridgeTalk method in 'Keyboard shortcut to execute specific function from script', where I linked you to.

JJMack
Community Expert
Community Expert
June 5, 2021

That is easy there is one or there is no active selection true or fales.   You can save a colection of selection as alpha channels.  There a limit of 53.

////// check for selection //////
function hasSelection(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(ref);
var hasSelection = docDesc.hasKey(stringIDToTypeID("selection"));
return hasSelection;
};

alert("hasSelection = " + hasSelection() );

You can Add other areas to an Active selection

 

However if area overlap they  combine 

JJMack
Inspiring
June 5, 2021

That is true, a document can have one or more selections active.

Sometimes I run into a problem when I am not aware the document has a selection. Then I add a new selection and the adjustment affects both selections.  I was thinking of a way to check if the document has more than one selection to avoid the problem. Another solution is to reset the selection pod to New Selection when a selection tool is active. 

JJMack
Community Expert
Community Expert
June 5, 2021

No there can be 53 saved selections.  You may consider Layer mask and Vector Layer mask and Frame masks also as saved selections.  However, The active selection is a singular. The active selection may  contain areas that have been saves as individual selections.   There is only one active selection all its selected area will be deselected with Ctrl+D  there is no deselect area 1 no deselect area 2.   It a True False there  is or there is not as active selection.  You can edit the Active selection add and subtract area.   There can only be one Active selection.  The JavaScript function I posted will return the True/False state.  So your script can check if the is one or not. If there one your script cans save it as an alpha channel. Deselect  make the selection you want active use it then deselect load the Alpha channel you save to restore the original active selection then delete the saved alpha channel.   A selection tool does not have to be the current tool for there to be an active selection.

JJMack