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

Can't get access to layers of Smart object by Script

Community Beginner ,
Feb 17, 2025 Feb 17, 2025

Hi all. I have a small script that should open all folders and change all text layers. But I need to change text layers in smart objects too and this is the biggest problem. I don't know why but I can only get the name of the smart object and nothing else.

 

function replaceTextInLayers(layer) {
    if (layer.kind == LayerKind.TEXT) {
       
        var currentText = layer.textItem.contents;
        var newText = findTranslation(currentText);
        layer.textItem.contents = newText;
       


    }else if(layer.kind === LayerKind.SMARTOBJECT) {
        alert("Name of smart object: " + layer.name)
        layer.smartObject.editContents();
        var smartDoc = app.activeDocument;
        if (smartDoc != null) {
            alert("Layers count in smart objects: " + smartDoc.layers.length);
        } else {
            alert("Smart object is null");
        }
       
        smartDoc.close(SaveOptions.DONOTSAVECHANGES);
    }
     

    if (layer instanceof LayerSet) {
        for (var i = 0; i < layer.layers.length; i++) {
            replaceTextInLayers(layer.layers[i]);
        }
    }
}
TOPICS
Actions and scripting , Windows
188
Translate
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

Community Expert , Feb 17, 2025 Feb 17, 2025
quote
        layer.smartObject.editContents();

By valera_9213

 

Assuming that you have a variable defining layer as the app.activeDocument.activeLayer, where did you get the following from:

 

.smartObject

 

and 

 

.editContents();

 

Would this have come from a generative LLM AI tool (ChatGPT or similar)?

 

For the active smart object layer, you can:

 

app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));

 

This assumes that the file can be edited in Photoshop. Otherwise, the associated a

...
Translate
Community Expert , Feb 17, 2025 Feb 17, 2025
var theSO = openSmartObject (activeDocument.activeLayer);
////// open smart object //////
function openSmartObject (theLayer) {
try {
activeDocument.activeLayer = theLayer;
var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );
var desc2 = new ActionDescriptor();
executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );
return app.activeDocument
} catch (e) {return false}
};
Translate
Community Expert ,
Feb 17, 2025 Feb 17, 2025
quote
        layer.smartObject.editContents();

By valera_9213

 

Assuming that you have a variable defining layer as the app.activeDocument.activeLayer, where did you get the following from:

 

.smartObject

 

and 

 

.editContents();

 

Would this have come from a generative LLM AI tool (ChatGPT or similar)?

 

For the active smart object layer, you can:

 

app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));

 

This assumes that the file can be edited in Photoshop. Otherwise, the associated app will open instead, such as Illustrator if the smart object is an .ai file. Otherwise, more complex code would be required to assess the type of smart object and handle it accordingly.

 

So you would need to loop over all layers, check if the layer or a smart object layer and perform the required edits. A text layer only goes one level deep, however, a smart object can contain another smart object etc. So you would need more robust code to look at the edited smart object, detect layer groups and text layers, drill down into further smart objects, and repeat as needed. It depends on how bulletproof the script needs to be, which depends on your data or the expected data being processed.

Translate
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 Beginner ,
Feb 17, 2025 Feb 17, 2025

Hi Stephen. You correctly assumed that I take the data from AI chats because I have no other sources of information on how to open this or that object and what functions I have access to. At the moment, my main task is to open access to a smart object and get all the layers from there as their folders.

Translate
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 ,
Feb 17, 2025 Feb 17, 2025
LATEST
quote

You correctly assumed that I take the data from AI chats because I have no other sources of information on how to open this or that object and what functions I have access to.

Have you already found 

photoshop-javascript-ref-2020.pdfGitHubhttps://github.com › blob › master › Documentation › ph...

photoshop-scripting-guide-2020.pdfGitHubhttps://github.com › blob › master › Documentation › ph...

As far as Action Manager-code, which offers access to functionality that is beyond the DOM-code, is concerned one can use SciptingListener.plugin to record it and then try to adapt it as necessary. 

Translate
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 ,
Feb 17, 2025 Feb 17, 2025
var theSO = openSmartObject (activeDocument.activeLayer);
////// open smart object //////
function openSmartObject (theLayer) {
try {
activeDocument.activeLayer = theLayer;
var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );
var desc2 = new ActionDescriptor();
executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );
return app.activeDocument
} catch (e) {return false}
};
Translate
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