Copy link to clipboard
Copied
Hi everyone,
How to test if a selection is empty?
If I try to access the bounds, the object does not exist, if I test the selection object it seems fine.
Thanks
Christian
There may not be a way to do this without the try/catch block.
Copy link to clipboard
Copied
You can use a try/catch block with copy to see if it errors"
#target photoshop
var doc = activeDocument;
try{
doc.selection.copy()
//code here if there is something in the selection
}
catch(e){
//code here for if the selection is empty
}
Copy link to clipboard
Copied
Thank you, but I would have liked a test without the try/catch, which works, I confirm.
cb
Copy link to clipboard
Copied
There may not be a way to do this without the try/catch block.
Copy link to clipboard
Copied
This function is from xbytor.
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;
};
Copy link to clipboard
Copied
If the object that may or may not exists it one its own layer you could test that layer bounds to see if it empty, I wrote script and tested the layers boundry of an empty layer not a selection bounds for a selection on an enpty will have a bounds but the lauyer's bound seems to be all zeros.

Get ready! An upgraded Adobe Community experience is coming in January.
Learn more