Help for clearing a selection

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

Wi][iE

Help for clearing a selection

Post by Wi][iE »

Hi, everybody,

I'm kinda new to photoshop scripting, thou i have programming experience, and i've recently encountered a very nasty error while running my script.
Basicaly my script does some cropping and layer manipulation, but at a certain point i try to clear a selection with
Code: Select allapp.activeDocument.selection.clear();
and i get: "the command 'delete' is not currently available"

well... thinking i could go around i tryed
Code: Select allapp.activeDocument.selection.cut();
and even (from script listener)
Code: Select allvar id41 = charIDToTypeID( "Dlt " );
executeAction( id41, undefined, DialogModes.NO );
and all pop up the same error

the code for the function is (follow the debugFile.write for info):
Code: Select allfunction parseLayers(group, ch)
{
   debugFile.write("Parsing the layers...\n");
   for(idx=0;idx<group.length;idx++)
   {
      group[idx].visible=false;
   }
   //browse through all the art layers
   for(layerIdx=0;layerIdx<group.length;layerIdx++)
   {
      group[layerIdx].visible=true;
      if(layerIdx!=0)group[layerIdx-1].visible=false;
      debugFile.write("selecting "+ch+"... ");
      AD.selection.load(AD.channels[ch], SelectionType.REPLACE, 0, false);
      debugFile.write("inverting...");
      AD.selection.invert();
      debugFile.write("clearing..."); //DOESN'T WORK :((
      app.activeDocument.selection.clear();
      //AD.selection.cut();
      //var id41 = charIDToTypeID( "Dlt " );
      //executeAction( id41, undefined, DialogModes.NO );
      debugFile.write("inverting again...");
      AD.selection.invert();
      debugFile.write("done\n");
      saveElement(folder, group[layerIdx]);
   }
   debugFile.write("...done parsing layers\n");
}
...and if i comment the line for clearing, my script runs smoothly...

Can anyone pls tell me any way i can clear that selection, or bypass this error...

thanks in advance
Patrick

Help for clearing a selection

Post by Patrick »

You may be getting the error because the layer you have the selection on is locked, or if the selection is empty (I think that will cause this error too but not 100% sure).

You could get around this buy using a try/catch block in your function. Where you want to clear, do:

Code: Select alltry {
app.activeDocument.selection.clear();
} catch(e) {
alert("Unable to clear!");
}
 

Patrick
xbytor

Help for clearing a selection

Post by xbytor »

Code: Select allor if the selection is empty (I think that will cause this error too but not 100% sure).

For me, that's what the problem almost always is.

-X
Wi][iE

Help for clearing a selection

Post by Wi][iE »

thought so too, but after the script pops the error and i stop it, the selection i need is perfectly ok... and it is there, i'm 100% certain it is, since i used that code to do the cropping, in a previous version of the script...
i'll try the try{}catch(){} but it doesn't help me much since i have documents with 10 saved selections and 20+layers... and 200 clicks on ok does not sound good.
hmmm.. could it be that it does not have time to make the selection is there any command that can delay the script a bit?
Patrick

Help for clearing a selection

Post by Patrick »

Wi][iE wrote:i'll try the try{}catch(){} but it doesn't help me much since i have documents with 10 saved selections and 20+layers... and 200 clicks on ok does not sound good.

Just remove the line

Code: Select allalert("Unable to clear!");

and it will do nothing if it hits the error and keep plugging along.

Patrick
xbytor

Help for clearing a selection

Post by xbytor »

could it be that it does not have time to make the selection Question is there any command that can delay the script a bit?

After the script stops at the point you try to clear the selection, try to clear it manually. It could be that you have a layer selected that is transparent (e.g. new) or you have selected a part of layer that is transparent.

-X
Wi][iE

Help for clearing a selection

Post by Wi][iE »

dud, you're a genius (well, experience has it's part anyway) - never crossed my mind i never actually selected the proper layer... fixed the script with:
Code: Select alldebugFile.write("clearing...");
      AD.activeLayer = group[layerIdx]; // <- za magic line :D
      AD.selection.clear();

now i just have to find a that history count because i'm doing a bit too many operations and can't undo

thanks again
xbytor

Help for clearing a selection

Post by xbytor »

If you only need to skip back to a certain point, try setting a snapshot and reverting back when needed.

-X
Wi][iE

Help for clearing a selection

Post by Wi][iE »

Code: Select all      var tempHistoryState = AD.activeHistoryState;
      AD.selection.load(....);
      AD.selection.invert();
      AD.activeLayer = group[layerIdx];
      AD.selection.clear();
      AD.selection.invert();
      saveElement(folder, group[layerIdx]);
      AD.activeHistoryState = tempHistoryState;

this did fine

thanks again guys, now this script is truly efficient for me