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

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

    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

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.

AFAIK layer IDs are persistent. Document ID is not

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.

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