Skip to main content
c.pfaffenbichler
Community Expert
Community Expert
December 2, 2008
Question

verify if a selection exists

  • December 2, 2008
  • 4 replies
  • 1140 views
Sorry to bother You all with such a question, but how can I verify if an active selection exists in a document?
Actually I seem to be able to verify it by way of a whack try-catch-construction set to some selection-property (which returns undefined if no Selection exists), but obviously a more simple way should exist. 

I want to do so in order to set up a condition to delete the Layer Mask if no Selection is active on creating an Adjustment Layer (a basic setting seemingly lost in CS4).

Thanks for any suggestions.
This topic has been closed for replies.

4 replies

c.pfaffenbichler
Community Expert
Community Expert
December 3, 2008
Thanks to You all.
I readily admit to the bad-form, Im more of a cobbler-together of Script-elements (for many of which I have to thank xbytor anyway).

As usual in the Scripting-fora good and prompt advice.
Known Participant
December 2, 2008
Christoph

I didn't attempt to test your code but you may be correct that your solution is smaller and will/should work properly on the face of it.

Likely the reason X used the form shown in the first example is that earlier versions of Photoshop had problems when requesting selection bounds and it seems I recall some versions where it didn't work at all.

However if you are planning on only using CS3, CS4 your version should work or at least, has the appearance that it will.

Regards

George Smith
c.pfaffenbichler
Community Expert
Community Expert
December 2, 2008
Thanks!
A nifty construction, but it seems to indicate that the fact of a Selection being active is indeed not an easily accessed property.
I wonder if

var myDocument = app.activeDocument;
try {var forgetIt = myDocument.selection.bounds}
catch (a) {alert (has no selection)};

might not be shorter after all?

Edit: Thanks especially for the promptness of the answer!
Known Participant
December 2, 2008
> A nifty construction, but it seems to indicate that the fact of a Selection being active is indeed not an easily accessed property.

This is true.

> I wonder if
>
> var myDocument = app.activeDocument;
> try {var forgetIt = myDocument.selection.bounds}
> catch (a) {alert (has no selection)};
>
> might not be shorter after all?
>

Marginally shorter, but bad form. A general rule for good programming is that
you do not use try/catch for stuff like this. There are times when you
absolutely have to because there is no other choice or times when it saves you
20 or 30 lines of code. But, in this case, the hasSelection function is small
enough that there is no real benefit to doing the try/catch.



George_E_Smith@adobeforums.com wrote:
> Likely the reason X used the form shown in the first example is that earlier
versions of Photoshop had problems when requesting selection bounds and it seems
I recall some versions where it didn't work at all.

Selection.bounds didn't work in CS2 and accessing the property would always
raise an exception. I'm not really sure how I implemented earlier versions but
using the history state stack trick has simplified a lot of code.

-X
Paul Riggott
Inspiring
December 2, 2008
Here is one of X's functions...

alert(hasSelection());

function hasSelection(doc) {
if(doc == undefined) doc = activeDocument;
var res = false;
var as = doc.activeHistoryState;
doc.selection.deselect();
if (as != doc.activeHistoryState) {
res = true;
doc.activeHistoryState = as;
}
return res;
}