Skip to main content
grenzionky
Participant
September 13, 2017
Answered

How to edit text in smartobject layer

  • September 13, 2017
  • 3 replies
  • 4679 views

I'm working on a script to get text inputted through the script to change text in a text layer within a smartobject (mockup).

I tried everything I could think of, I even scoured the forums here and found nothing...

I know how to edit a regular text layer, that's easy, but a text layer within a smartobject seems impossible...

My question is: how do I edit text in a smartobject through a script?

This topic has been closed for replies.
Correct answer Tomas Sinkunas

Hey appologies for that, I left that my accident. Here's a clean code now.

(function () {

    // Find smart Layers in the project

    var smartObjects = findLayers(app.activeDocument, true, {

        typename: "ArtLayer",

        kind: LayerKind.SMARTOBJECT,

    });

    // Let's pick first Smart Object from the array

    if (smartObjects.length === 0)

        return alert("Didn't find any Smart Layer");

    var mySmartLayer = smartObjects[0];

    // Make it active;

    touchLayer(mySmartLayer);

    // Open Smart Object

    openSmartObject();

    // Find Text Layers in the project

    var textLayers = findLayers(app.activeDocument, true, {

        typename: "ArtLayer",

        kind: LayerKind.TEXT

    });

    // Pick first text layer from the array

    if (textLayers.length === 0)

        return alert("Didn't find any Text Layer");

    var myTextLayer = textLayers[0];

    // Make it active

    touchLayer(myTextLayer);

    // Chante text layer content

    myTextLayer.textItem.contents = "Indeed!";

    // Save and close document

    app.activeDocument.close(SaveOptions.SAVECHANGES);

    //////////////

    function findLayers(searchFolder, recursion, userData, items) {

        items = items || [];

        var folderItem;

        for (var i = 0, il = searchFolder.layers.length; i < il; i++) {

            folderItem = searchFolder.layers;

            if (propertiesMatch(folderItem, userData)) {

                items.push(folderItem);

            }

            if (recursion === true && folderItem.typename === "LayerSet") {

                findLayers(folderItem, recursion, userData, items);

            }

        }

        return items;

    }

    function propertiesMatch(projectItem, userData) {

        if (typeof userData === "undefined") return true;

        for (var propertyName in userData) {

            if (!userData.hasOwnProperty(propertyName)) continue;

            if (!projectItem.hasOwnProperty(propertyName)) return false;

            if (projectItem[propertyName].toString() !== userData[propertyName].toString()) {

                return false;

            }

        }

        return true;

    }

    function touchLayer(layer) {

        app.activeDocument.activeLayer = layer;

        var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putIdentifier(app.charIDToTypeID('Lyr '), layer.id);

        desc.putReference(app.charIDToTypeID('null'), ref);

        executeAction(app.charIDToTypeID('slct'), desc, DialogModes.NO);

        return layer;

    }

    function openSmartObject() {

        var descriptor = new ActionDescriptor();

        executeAction(stringIDToTypeID("openSmartObject"), descriptor, DialogModes.NO);

    }

})();

3 replies

Tomas Sinkunas
Legend
September 14, 2017

There you go buddy.

(function () {

    // Find smart Layers in the project

    var smartObjects = findLayers(app.activeDocument, true, {

        typename: "ArtLayer",

        kind: LayerKind.SMARTOBJECT,

    });

    // Let's pick first Smart Object from the array

    if (smartObjects.length === 0)

        return alert("Didn't find any Smart Layer");

    var mySmartLayer = smartObjects[0];

    // Make it active;

    touchLayer(mySmartLayer);

    // Open Smart Object

    openSmartObject();

    // Find Text Layers in the project

    var textLayers = findLayers(app.activeDocument, true, {

        typename: "ArtLayer",

        kind: LayerKind.TEXT

    });

    // Pick first text layer from the array

    if (textLayers.length === 0)

        return alert("Didn't find any Text Layer");

    var myTextLayer = textLayers[0];

    // Make it active

    touchLayer(myTextLayer);

    // Chante text layer content

    myTextLayer.textItem.contents = "Indeed!";

    // Save and close document

    app.activeDocument.close(SaveOptions.SAVECHANGES);

    //////////////

    function findLayers(searchFolder, recursion, userData, items) {

        items = items || [];

        var folderItem;

        for (var i = 0, il = searchFolder.layers.length; i < il; i++) {

            folderItem = searchFolder.layers;

            if (propertiesMatch(folderItem, userData)) {

                items.push(folderItem);

            }

            if (recursion === true && folderItem.typename === "LayerSet") {

                findLayers(folderItem, recursion, userData, items);

            }

        }

        return items;

    }

    function propertiesMatch(projectItem, userData) {

        if (typeof userData === "undefined") return true;

        for (var propertyName in userData) {

            if (!userData.hasOwnProperty(propertyName)) continue;

            if (!projectItem.hasOwnProperty(propertyName)) return false;

            if (projectItem[propertyName].toString() !== userData[propertyName].toString()) {

                return false;

            }

        }

        return true;

    }

    function touchLayer(layer) {

        app.activeDocument.activeLayer = layer;

        var desc = new ActionDescriptor();

        var ref = new ActionReference();

        ref.putIdentifier(app.charIDToTypeID('Lyr '), layer.id);

        desc.putReference(app.charIDToTypeID('null'), ref);

        executeAction(app.charIDToTypeID('slct'), desc, DialogModes.NO);

        return layer;

    }

    function openSmartObject() {

        var descriptor = new ActionDescriptor();

        executeAction(s2t("openSmartObject"), descriptor, DialogModes.NO);

    }

})();

grenzionky
Participant
September 15, 2017

Thank you so much for the reply!

On line 99 I'm getting an error: Error 24: s2t is not a function.

Any ideas on how to fix that?

JJMack
Community Expert
Community Expert
September 15, 2017

My guess would be the active layer was not a smart object layer so open smart object is not an available function.  You need to select a smart oblect layer before you can open the object.

Thomas code creates an array of smaty object layers and touches the first one to select it.   If no smart object layer exists an alert about that should be displayed.

  1.     // Let's pick first Smart Object from the array 
  2.     if (smartObjects.length === 0
  3.         return alert("Didn't find any Smart Layer"); 
  4.     var mySmartLayer = smartObjects[0];

it there are smart obkect layers

  1.     // Make it active; 
  2.     touchLayer(mySmartLayer); 


sets the active layer to the first one in the array mySmartLayer = smartObjects[0];

next it is opened

  1.     // Open Smart Object 
  2.     openSmartObject(); 

JJMack
grenzionky
Participant
September 14, 2017

I realized that. The question was how would I do that.

I need, through the script, open the smartobject, edit the text, then close the smartobject.

Can someone please provide a code snippet (or a link to the documentation to a code snippet), that I can use to do that? Please.

JJMack
Community Expert
Community Expert
September 14, 2017

You would do it the same way that you would do it manually using Photoshop UI.  You just need to script that process.

You will most likely need to use action manager code to open the active layers object. You need to know the layer and know that the layer object is a Photoshop object,   When you open the layer object. Photoshop will create a temp work file in temp space  from the object and will open this work file. That will add a new document open in Photoshop and it will become the Active document. Photoshop will switch from your mockup document to the work document.  Your script is still in control. You would then get the layers in the work document. Find the text layer you want changed and then change its text. Your script would then use file save to save the work document and then file close to get it out of  Photoshop.  The close will cause Photoshop to switch back the your mockup document as the current active document.  When you save the work document Photoshop would also see that the document has been changed and would also updated the mockup document layers object open in Photoshop with the changed document.  So when Photoshop switches back to the mockup document the layer object will have been changed if you saved changes to the work file.  The script works the same way you would do the process using Photoshop UI.

If you do not know Action Manager Code learn about Adobe Scriptlistener Plug-in. Install it and have it recors action manager code for opening as smart object.  Use the code to code a function to open smart object and  use it in your script.

JJMack
JJMack
Community Expert
Community Expert
September 13, 2017

You need to edit the object and have Photoshop update the smart object in the layer. Also if you open the smart object in your it may open in ACR or AI  all object are not opened by Photoshop thet open in the application the are associated with.  Object can be RAW file AI files etc. If the object is opened by an application other that Photoshop  Your script will not be in control of processing the object opened buy the other application or plug-in.  If the object is a Photoshop object I think you should be able to open it and work on it.  If iy has Text its not a RAW file however it could be and .AI or .SVG or .pdf or .eps and if they are opened by Photoshop they will open as a raster layer no text or vectors.   Layered Photoshop object and layered Photoshop file will have layer.

Smart Object layers in Mockup have their place but the also impose some limitation. All replacement object must have the same size and resolution as the object stored in the mockup. When you replace a smart object layer content the layers associated transform is not replaced or updated,  The transform will not work correctly if the replacement object has a different size.  All replacement muse be create the correct size.

JJMack
grenzionky
Participant
September 13, 2017

When I open up the smartobject in photoshop, it opens up a seperate .psb file, and within that psb file there is a text layer that I need to edit through a script.

JJMack
Community Expert
Community Expert
September 13, 2017

You will need to edit the psb document when it opens find and replace the text than save and close the psb work document.  When the PSB is committed  Photoshop will update the smart object layer. While editing the PSB do not chanege its size and resolution. The PSB is a work file in you temp space that Photoshop creates from the object and opens it for you to work on.

In other words you main script need to open the smart object layer object.  Like when you double click on it in the layers palette

JJMack