How to select layer with only ID?

I have a function to select a layer inserted in the function as the argument, however, this takes the layer name and ID to select the layer, the problem is that if I have two or more layers with the same name, the function always select the same layer, even if I insert another layer inside that has the same name:

async function selectLayer(l) {
        await batchPlay(
            [
                {
                    _obj: "select",
                    "_target": [
                        {
                            "_ref": "layer",
                            "_name": l.name
                        }
                    ],
                    makeVisible: false,
                    layerID: [l.id],
                    _options: {
                        dialogOptions: "dontDisplay"
                    }
                }
            ], {});
    }

How could I select a layer by only the ID? Is it possible? As the Id is unique to the layer inside the document I think this would be a better solution for my problem.

Thanks!

It totally is possible. Actually in my humble opinion you should avoid selecting by name for the reason that you just stated: names are not necessarily unique. You can always get a layer’s ID from the layer’s properties. You then specify that id as _id instead of _name, i.e:

...
"_target":[
    { _ref: "layer", _id: l.id}
],
...

As a side node, in my plugins I typically also specify the layer’s document for all functions that use batchPlay and operate on a layer. In my experience this makes it more robust when dealing with multiple open documents in the same session:

...
"_target":[
    { _ref: "layer", _id: l.id},
    { _ref: 'document', _id: l.document.id }
],
...```

Some actions support additional document reference and some do not. If they don’t you have to still activate the document explicitly.

Thank you very much man, that does the work, so easy thank you!!

It is great to now these kind of additional stuff, thank you Jarda!