# How to store a selected layer in a variable or an object that could be used later to execute actions on the layer

**URL:** <https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931>\
**Category:** Photoshop\
**Tags:** uxp\
**Created:** [February 26, 2023, 4:23pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931 "2023-02-26T16:23:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jam/32/2615_2.png) [@jam](https://forums.creativeclouddeveloper.com/u/jam)\
**Post date:** [February 26, 2023, 4:23pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931/1 "2023-02-26T16:23:52Z")

</div>

Hi, I’m new to UXP and have been looking for a way to store a layer or a layer’s id with a specific parameter/attribute (related to the actions that need to be executed. For example a ‘resize’ or ‘move’) somewhere that would be persistent and linked to the specific document to use later to execute an action on the layer.  
this should work on the same layer in a duplicated document.  
Something like assigning a variable to a layer in photoshop’s Data Sets.  
My other question: Is there a way to access the variables that we set on layers (image \> variables \> define…) in photoshop’s datasets?  
something like this below

```auto
    const app = require("photoshop").app;
    const appCore = require('photoshop').core
    const doc = app.activeDocument

    const setVariable = async () => {
        if (doc.activeLayers.length >= -1){
            doc.activeLayers.forEach(async (layer) => {
                window.localStorage.setItem(layer.id)
            })
        }else{
            await appCore.showAlert({ message: 'Please select layers to assign a variable to'})
        }
    }

```

However local storage is not linked to a document.

Thanks

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [February 26, 2023, 5:41pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931/2 "2023-02-26T17:41:03Z")

</div>

The only way you’re going to be able to attach persistent data to a file is by writing it to the file’s metadata.  
Just stringify it and either write to an existing metadata field or create a custom one.  
~~I’m pretty sure that document and layer ids are not persistent though and will change every time the document is closed/re-opened (or possibly for each Photoshop session) so this might be a moot exercise without doing something like renaming the layer and storing that as your reference id instead.~~

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [February 26, 2023, 5:53pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931/3 "2023-02-26T17:53:59Z")

</div>

AFAIK layer IDs are persistent. Document ID is not

---

<div class="post-metadata">

**Author:** ![jam](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jam/32/2615_2.png) [@jam](https://forums.creativeclouddeveloper.com/u/jam)\
**Post date:** [February 26, 2023, 6:37pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931/4 "2023-02-26T18:37:40Z")

</div>

Thank you Timothy for your reply. That sounds like the perfect solution, I will look up how to write an entry to the file’s metadata. would you be able to provide a quick example?

As Karmalakas said, I tested the layer IDs and they’re persistent, doc IDs change but if data is stored in the file’s metadata, then I assume it will be present in the duplicated document.

---

<div class="post-metadata">

**Author:** ![Timothy\_Bennett](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/timothy_bennett/32/1251_2.png) [@Timothy\_Bennett](https://forums.creativeclouddeveloper.com/u/Timothy_Bennett)\
**Post date:** [February 26, 2023, 7:04pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-store-a-selected-layer-in-a-variable-or-an-object-that-could-be-used-later-to-execute-actions-on-the-layer/5931/5 "2023-02-26T19:04:57Z")

</div>

I’ve not personally messed with metadata since CEP, but here’s a link to some pretty detailed forums posts on the subject:

> [@Adding custom properties or metadata to Layers?](http://forums.creativeclouddeveloper.com/t/adding-custom-properties-or-metadata-to-layers/5892/8):
>
> Take a look on the following threads: [Photoshop UXP plugin + XMPMetadata](http://forums.creativeclouddeveloper.com/t/photoshop-uxp-plugin-xmpmetadata/2166)[Document XMP metadata](http://forums.creativeclouddeveloper.com/t/document-xmp-metadata/3486)
