# Cannot enable/disable layers. Nudging layer moves selected layer not layer variable

**URL:** <https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809>\
**Category:** UXP Plugin API\
**Tags:** javascript\
**Created:** [April 14, 2021, 7:37pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809 "2021-04-14T19:37:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![4t0m1c](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/4t0m1c/32/1330_2.png) [@4t0m1c](https://forums.creativeclouddeveloper.com/u/4t0m1c)\
**Post date:** [April 14, 2021, 7:37pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/1 "2021-04-14T19:37:53Z")

</div>

Hi, so this started out as being a simple quest to enable and disable layers based on matching the layer name to a string. According to the API though, there is no way to enable or disable a layer -\_-.

So I’ve created a JS function to check for a matching layer name then nudge it off screen. Console.logs indicate that the layer names and if statements are all correct but the only layer that actually gets nudged is whichever layer is currently selected in the document instead of the variable to which the nudge is applied to.

As an example, I have 4 layers - ‘apple’, ‘banana’, ‘grape’ and ‘orange’. If the matching string is ‘apple’, then whichever layer is selected (including groups) ends up being nudged 3 times.

Any advice? Is this a bug?

This is the function that performs the modifications:

```
function ProcessAssets() {
  const hairVariation = document.getElementById("hairVariation").getAttribute("value") - 1;
  const hairVariations = document.getElementsByClassName("hairVariations");

  if (hairVariation < hairVariations.length && hairVariation >= 0) {
    console.log("Selected variation: " + hairVariations[hairVariation].getAttribute("value"));

    const app = require('photoshop').app;
    const doc = app.activeDocument;
    const docLayers = doc.layers;

    for (let index = 0; index < hairVariations.length; index++) {
      let element = hairVariations[index].getAttribute("value");
      let enable = index == hairVariation;

      for (let i = 0; i < docLayers.length; i++) {
        let layer = docLayers[i];
        if (layer.name == element) {
          console.log(layer.name + " == " + element);
          if (enable) {
            if (nudged.includes(layer)) {
              layer.nudge(-20000, 0);
              nudged.pop(layer);
              console.log("Found target variation. Shifting back.");
            }
          } else {
            if (!nudged.includes(layer)) {
              layer.nudge(20000, 0);
              nudged.push(layer);
              console.log("Found unwanted variation. Shifting away.");
            }
          }
        }
      }

      if (enable) {
        console.log(element + " enabled");
      } else {
        console.log(element + " disabled");
      }
    }

  } else {
    console.log("Outside of range");
  }
}
```

---

<div class="post-metadata">

**Author:** ![Pierre\_G](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pierre_g/32/971_2.png) [@Pierre\_G](https://forums.creativeclouddeveloper.com/u/Pierre_G)\
**Post date:** [April 14, 2021, 7:55pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/2 "2021-04-14T19:55:07Z")

</div>

Hi, maybe you could share a couple of lines of code ? It might be helpful.

---

<div class="post-metadata">

**Author:** ![4t0m1c](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/4t0m1c/32/1330_2.png) [@4t0m1c](https://forums.creativeclouddeveloper.com/u/4t0m1c)\
**Post date:** [April 14, 2021, 8:12pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/3 "2021-04-14T20:12:34Z")

</div>

Sure, I have added it the original post. 🙂

---

<div class="post-metadata">

**Author:** ![Pierre\_G](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pierre_g/32/971_2.png) [@Pierre\_G](https://forums.creativeclouddeveloper.com/u/Pierre_G)\
**Post date:** [April 14, 2021, 8:17pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/4 "2021-04-14T20:17:24Z")

</div>

What are you trying to acheive ? Unselect a given layer ?

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [April 14, 2021, 8:20pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/5 "2021-04-14T20:20:13Z")

</div>

Some operations in Photoshop can only be processed on the currently active layer, for example all transform events (to which nudge belongs). If you want to transform a specific layer, you’ll have to select it first (by index, id or name).

However, you can easily toggle layer visibility with batchPlay. Also, this works on any layer without selecting it beforehand:

```
{
  _obj: "hide",
  _target: {
    _ref: "layer",
    _name: "Layer 2",
  },
}
```

---

<div class="post-metadata">

**Author:** ![4t0m1c](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/4t0m1c/32/1330_2.png) [@4t0m1c](https://forums.creativeclouddeveloper.com/u/4t0m1c)\
**Post date:** [April 14, 2021, 8:32pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/6 "2021-04-14T20:32:26Z")

</div>

Aha ok, thank you, this seems to be the right direction. Could you perhaps give an example of how one would call this from a js function?

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [April 14, 2021, 8:42pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/7 "2021-04-14T20:42:15Z")

</div>

The BatchPlay syntax is explained in the docs [here](https://www.adobe.io/photoshop/uxp/ps_reference/media/advanced/batchplay/) and there are many examples in the forum, too - that should help to figure it out 🙂

---

<div class="post-metadata">

**Author:** ![4t0m1c](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/4t0m1c/32/1330_2.png) [@4t0m1c](https://forums.creativeclouddeveloper.com/u/4t0m1c)\
**Post date:** [April 15, 2021, 11:19am UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/8 "2021-04-15T11:19:47Z")

</div>

Thanks @simonhenke for your insight. It took a lot more googling to figure it out but you got me on the right track. The API documentation is seriously lacking for UXP and batchPlay. Looking at a ton of other posts I managed to piece together this function that works.

Using the layer.\_id was necessary to account for layers with the same name.

```
function RunBatch(_show, _id) {
  const batchPlay = require("photoshop").action.batchPlay;

  const result = batchPlay(
    [{
      _obj: _show ? "show" : "hide",
      _target: {
        _ref: "layer",
        _id: _id,
      },
      "layerID": [_id],
      "makeVisible": _show,
    }], {
      "synchronousExecution": false,
      "modalBehavior": "fail"
    }
  );
}
```

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [April 15, 2021, 12:26pm UTC](https://forums.creativeclouddeveloper.com/t/cannot-enable-disable-layers-nudging-layer-moves-selected-layer-not-layer-variable/2809/9 "2021-04-15T12:26:12Z")

</div>

Yes, using layer ID is best practice when it comes to targeting layers, to prevent conflicts. One exception is when you need to target a layer inside of a batchPlay call, that you created in just that call.

→ batchplay  
 → read id  
 → batchplay

would result in two history steps, which is often unwanted. In these cases its fine to assign a unique name and target the layer by that.

Few more notes to your function:

> [@4t0m1c](#):
>
> `"layerID": [_id],`

This property doesn’t do anything. It sometimes gets added to the descriptor after Photoshop has processed it and has more information on the event. (For example in the make layer event, it will show the newly assigned ID). Using the \_id in the target object is sufficient.

> [@4t0m1c](#):
>
> ` "makeVisible": _show,`

Same for this one. As far as I know, makeVisible is only a parameter used in the select (layer) event. The event code/name of `show` or `hide` already takes care of that.

> [@4t0m1c](#):
>
> ```auto
> {
> "synchronousExecution": false,
> "modalBehavior": "fail"
> }
> 
> ```

Both of these are default values, so you can leave them out, but of course you can also keep them.  
Also, if you don’t do anything with the result (and only run some operation), you don’t have to assign it to a variable.

So this would be a minimized version of the function:

```
const batchPlay = require("photoshop").action.batchPlay;
function showLayer(_show, _id) {
  batchPlay(
    [{
      _obj: _show ? "show" : "hide",
      _target: {
        _ref: "layer",
        _id
      }
    }], {}
  );
}

```
