Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Is it possible to store the reference to a layer in extension?

Explorer ,
Aug 06, 2021 Aug 06, 2021

Copy link to clipboard

Copied

I'm trying to make such an extension: it has two buttons. One to "bookmark" a layer, the other to set the bookmarked layer as active layer. This way the user can quickly navigate through the layers without scrolling all the way.

 

I made a script that appends suffix '^' to the layer's name as a bookmark. It kinda worked, but the problem was, it's very very slow to iterate through all the layers and find the layer with said suffix. If I can store the reference to a layer, it would be instant, but as far as I know, Photoshop script is "stateless" and there is no way to pass a reference from a script to another.

 

Is there a way to "store" a reference in extension?

TOPICS
Actions and scripting

Views

70

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Adobe Community Professional , Aug 06, 2021 Aug 06, 2021
I have not worked with extensions, but as far as I know it is possible to pass variables between the script and the extension. Each layer has its own unique ID (within the document). It is enough to save it to variable and pass to extension. To find how to select a layer, knowing its ID, enter in the search bar 'select layer by id' If you can't find how to pass the variable, then just save and read it by the script (you can save it to a file, to the document itself as metadata, to an environment...

Likes

Translate

Translate
Adobe Community Professional , Aug 06, 2021 Aug 06, 2021
Store the Layer ID  of the layer you marked with the ^.  The problem I see is you most likely would want to be able to set more than one bookmark and also be able to remove bookmarks. If you are creating an extension set in the bookmark Buttons Part of the Bookmark layer Layer's name and layer Id  Where unset bookmarks will have an empty name and layer ID. Perhaps use a Alt+Click on a Set Bookmarked button remove the bookmark.  To Mark a Bookmarked Layer it may be easier to change the layer's co...

Likes

Translate

Translate
Adobe Community Professional ,
Aug 06, 2021 Aug 06, 2021

Copy link to clipboard

Copied

I have not worked with extensions, but as far as I know it is possible to pass variables between the script and the extension.

 

Each layer has its own unique ID (within the document). It is enough to save it to variable and pass to extension. To find how to select a layer, knowing its ID, enter in the search bar 'select layer by id'

 

If you can't find how to pass the variable, then just save and read it by the script (you can save it to a file, to the document itself as metadata, to an environment variable, to photoshop settings file etc.)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 06, 2021 Aug 06, 2021

Copy link to clipboard

Copied

Store the Layer ID  of the layer you marked with the ^.  The problem I see is you most likely would want to be able to set more than one bookmark and also be able to remove bookmarks. If you are creating an extension set in the bookmark Buttons Part of the Bookmark layer Layer's name and layer Id  Where unset bookmarks will have an empty name and layer ID. Perhaps use a Alt+Click on a Set Bookmarked button remove the bookmark.  To Mark a Bookmarked Layer it may be easier to change the layer's color code in the layers Palette rather then parsing the layer mane and adding the ^ suffix. Have Blue be a bookmarked layer color code.  You most likely need to store bookmarks information for each open document so the user can switch between documents.  If you use the documents Meta Data to store Bookmarks. Bookmarks can persist between edit sessions.

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2021 Aug 06, 2021

Copy link to clipboard

Copied

Thanks for your reply! My problem isn't how to make the bookmark persistent, however. My problem is that it's very slow to jump to the bookmarked layer. I have the following code to get all the layers:

 

var collectAllLayers = function(doc) {
    var allLayers = [];
    forEachLayer(docfunction(layer) {
        allLayers.push(layer);
    });
    return allLayers;
};

function forEachLayer(docfun) {
    for (var m = 0m < doc.layers.lengthm++){
        var theLayer = doc.layers[m];
        fun(theLayer);
        if (theLayer.typename === "LayerSet"){
            forEachLayer(theLayerfun);
        }
    }
 
It takes ~10 seconds for a document with 100 layers. Is it possible to select a layer by its ID without iterating through all layers?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 06, 2021 Aug 06, 2021

Copy link to clipboard

Copied

Ok I found it!

function selectByID(id)
{
  var desc1 = new ActionDescriptor();
  var ref1 = new ActionReference();
  ref1.putIdentifier(charIDToTypeID('Lyr '), id);
  desc1.putReference(charIDToTypeID('null'), ref1);
  executeAction(charIDToTypeID('slct'), desc1DialogModes.NO);
};

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 07, 2021 Aug 07, 2021

Copy link to clipboard

Copied

LATEST

As part of my personal learning, I collected different methods here:

Select a single layer.jsx

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines