Checking for empty area before copy?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

ScottAndrew

Checking for empty area before copy?

Post by ScottAndrew »

I have a script that searches for slices using save to web trick and then does a selection then tries to copy doing the following:

app.activeDocument.selection.select(selectRect);

if (activeDocument.selection != null)
{
app.activeDocument.selection.copy();
app.activeDocument.paste();
app.activeDocument.activeLayer.name = layerName;
}

However if my the selection has no pixels I get an error in the console: "
General Photoshop error occurred.\n- Could not complete the command because the selected area is empty."

I have tried to wrap the code in a try-catch but doesnt seem to help..
Mike Hale

Checking for empty area before copy?

Post by Mike Hale »

activeDocument.selection will always return the object [selection] so your if test will alway test true. The selection object is broken in PS7, CS, and CS2 otherwise you could test selection.bounds to see if the selection is empty.

But I find a try/catch block works for me with either empty or no selection

Code: Select all try{
    app.activeDocument.selection.copy();
    alert('selection copied');
}catch(e){
   alert('error')
};;

If that doesn't work for you and you are running the script in ESTK check to see that 'Don't break on guarded exceptions' is checked under the debug menu
ScottAndrew

Checking for empty area before copy?

Post by ScottAndrew »

Thanks. you answered my questions..