Get the number of active selections

Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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?

 

 

TOPICS
Actions and scripting

Views

2.8K

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

Adobe Community Professional , Jun 05, 2021 Jun 05, 2021
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.activeHistory...

Likes

Translate

Translate
LEGEND ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

image.png

You can Add other areas to an Active selection

 

image.png

However if area overlap they  combine 

image.png

JJMack

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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. 

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
LEGEND ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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.

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

What is the question? The thread got a bit confusing.

The snippet worked without the refresh function and the function keys worked on a Mac system.

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

I posted there a code, but with no BridgeTalk yet. Then you said you have problem in using it. Fortunately r-bin said one script can be bound with different keys. That was solution for you.

 

Now I'm curious if the other solution, so that I posted meantime of that day works for you on Mac too. It's an original code that I changed, so removed refresh() to add BridgeTalk() instead.

 

To find it, in a linked post, click a linked post in which you'll immediatelly see described code.

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

Got it, I reply in the original post. The BriodgeTalk snipe works in mac but not smoothly.

Also, can the snippet support different keyboard combinations like shift+comand+option+f1?

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

 

function key(v) {return eval('ScriptUI.environment.keyboardState.' + (v ? v + 'Key' : 'keyName'))} key('shift') && key('ctrl') && key('alt') && key()=='F1'

 

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

 

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)

 

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
LEGEND ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

I understand selections won't intersect each other.

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

Thank you, the code alerts the number of selections in the document.

The if statement below bypasses the alert and alerts the number of selections in the document.

How can Include a conditional statement?

 if the number of selections is more the 1 stop else run action.
 
 
function selectionNum() {
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);
 
}
 
if (selectionNum() > 1) {
alert("The document has multiple selection");
} else {
// do action
}

 

 

 

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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')

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
Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

This works! thank you all for your dedication and help!

 

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
}

if (slctns() > 1) {
alert("doc has multiple slections")
} else {
alert('run action');
}

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
LEGEND ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

Yes the code does seem to show how many none continuous area are selected. When I ran it on the luminosity selection I briefly  saw the blue paths used the get the number.  17,929

image.png

JJMack

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
Adobe Community Professional ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

I did not dive so deep, just after original post I imagined basicly three separate selections without 'holes', square, circle and triangle, and wrote script for such simple circumstance.

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
LEGEND ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

"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..

Capture.jpg

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

Capture.jpg

 

JJMack

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
Adobe Community Professional ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

There was not said if selection is inside other selection.

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
LEGEND ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

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

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
Adobe Community Professional ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

I'm understanding he means distinct, not touching each other selections.

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
Adobe Community Professional ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

quote

Their statement "A photoshop document has multiple active selections on the canvas."  Is nonsense.

By @JJMack

 

I agree. Rave.

I wonder how the author defines what the inactive selections are.

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
Participant ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

In the context of this post, multiple active selctions refer to more than one nonintersecting selection which is made with the patch or marquee tool. 

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