• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

257

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

Guide , 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

...

Votes

Translate

Translate
Community Expert , 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

...

Votes

Translate

Translate
Adobe
Guide ,
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.)

Votes

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
Community Expert ,
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

Votes

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?

Votes

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);
};

Votes

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
Community Expert ,
Aug 07, 2021 Aug 07, 2021

Copy link to clipboard

Copied

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

Select a single layer.jsx

Votes

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
Community Expert ,
Mar 04, 2022 Mar 04, 2022

Copy link to clipboard

Copied

LATEST

A similar topic cross-referenced as a standard script rather than as an extension, just in case it helps somebody who stumbles over this topic in the future:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/is-there-a-way-to-return-to-a-previou...

 

Votes

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