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

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