Skip to main content
Inspiring
May 2, 2023
Question

About WorkDok.selection.clear()...

  • May 2, 2023
  • 7 replies
  • 3425 views

Hello everyone.

 

I was expecting part of the new layer to be cleared (WorkDok.selection.clear();), but it doesn't happen ???

test.psd 

 

var WorkDok = app.activeDocument
WorkDok.artLayers[activeDocument.artLayers.length - 1].copy();
WorkDok.artLayers.add();
WorkDok.paste();
WorkDok.pathItems[0].makeSelection();
WorkDok.selection.invert();
WorkDok.selection.clear();

 

 

 

7 replies

Participant
May 14, 2025

Hey! It looks like you're trying to clear a selection, but the issue might be related to the order of operations. Try making sure that the layer you're working with is fully active before using selection.clear(). You could also try adding a WorkDok.selection.deselect() right before clearing the selection to ensure it’s properly deselected first. Also, check that the selection path is correctly set before calling clear(). Let me know if this helps

Legend
May 3, 2023

The selection.clear() function simply calls the Edit->Clear menu command (Similar to pressing the Delete button). When the path is active, it deletes the path. With active Note, deletes Note. Otherwise, it removes the pixels in the active layer.

 

To deselect a path, use the following code:

 

var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("path"));
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("deselect"), d, DialogModes.NO);

 

Legend
May 3, 2023

How to download your file?

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2023

Please save the Path and deselect it before running the Script. 

AnyONAuthor
Inspiring
May 3, 2023

save the Path and deselect it before running the Script...

Resalt >

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2023
quote

save the Path and deselect it before running the Script...

Resalt >

So it works as intended. (Maybe hide Layer 1) 

The clear applies to a Path or a Selection. 

Participant
May 3, 2023

selection.clear() is used to clear the pixels within a selected area of a layer. If you are using this method and it's not clearing the selected area, there could be a few reasons why this is happening.

  1. The selection is not active: Make sure that you have an active selection before using the selection.clear() method. You can create a selection using any of the selection tools (Marquee, Lasso, Magic Wand, etc.). If there is no active selection, the method will clear the entire layer.

  2. The layer is locked or the pixels are locked: Check to see if the layer or pixels are locked. If they are, you will not be able to clear the selected area.

  3. The layer is a background layer: If the layer you are working on is a background layer, you will not be able to clear the pixels within it. You will need to convert the background layer to a regular layer before you can make changes to the pixels within it.

  4. The layer is not visible: If the layer you are working on is not visible, you will not be able to clear the selected area. Make sure that the layer is visible by toggling the visibility icon in the Layers panel.

 

Hope this helps, Good Luck!

Participant
May 11, 2023

Also save the path before deselecting your selection area.

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2023

An aside: Why do you copy/paste instead of duplicating the Layer? 

AnyONAuthor
Inspiring
May 3, 2023

 I also tried various editor states, and noticed this feature.
If in the Paths panel, the Work Path layer is selected (tinted with white), then no cleanup occurs. But if you remove the selection from the Work Path, then everything is fine

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2023

Again: 

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Paths, Options Bar, …) visible? 

Screenshots that illustrate the actual start- and end-point of the Script in this case. 

 

Try saving the Work Path as a proper Path by assigning it a name. 

c.pfaffenbichler
Community Expert
Community Expert
May 3, 2023

Seems to work as expected. 

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Paths, Options Bar, …) visible?