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

How to edit text in smartobject layer

New Here ,
Sep 13, 2017 Sep 13, 2017

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting

Views

3.7K

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 1 Correct answer

Advocate , Sep 15, 2017 Sep 15, 2017

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

 

...

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 13, 2017 Sep 13, 2017

Copy link to clipboard

Copied

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

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
New Here ,
Sep 13, 2017 Sep 13, 2017

Copy link to clipboard

Copied

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.

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 ,
Sep 13, 2017 Sep 13, 2017

Copy link to clipboard

Copied

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

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
New Here ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

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.

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 ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

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

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
Advocate ,
Sep 14, 2017 Sep 14, 2017

Copy link to clipboard

Copied

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

    }

})();

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
New Here ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

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?

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 ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

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

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
Advocate ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

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

    }

})();

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 ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

  1. function openSmartObject() {  
  2.         var descriptor = new ActionDescriptor();  
  3.         executeAction(stringIDToTypeID("openSmartObject"), descriptor, DialogModes.NO)

Thomas is working on a tool to convert Action manager code to be more readable.   As I wrote to open a smart object you would most likely have to use Action Manager code.  Adobe Photoshop DOM does not cover all Phototshop features.

here is what the action manager code may look like straight for Adobe Scriptlistenet plugin.

// =======================================================

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

    var desc92 = new ActionDescriptor();

executeAction( idplacedLayerEditContents, desc92, DialogModes.NO );

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
New Here ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

IT WORKED!!!!!!!!!!!!!!!!!!!!!!!!!!

Thank you guys soooo much!!!!

The action manager code for openSmartObject() from JJMack and the rest of the code from Tomas Sinkunas worked together perfectly!

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
Advocate ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

JJMack's Action Manager code to open smart object is actually the same as my function openSmartObject(), except in my case I cleaned it up with Clean SL. In case you wonder what Clean SL is - rendertom / Clean SL / source / — Bitbucket ​

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 ,
Sep 15, 2017 Sep 15, 2017

Copy link to clipboard

Copied

Thomas knows JavaScript and Photoshop scripting well.  I only hack at scripting Photoshop.  I can not type, can not spell and do not know JavaScript.  However I have been involved with computer for more than 50 years I can still hack some code.....

Thomas's Clean SL is a great tool to learn about Action manager code.

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
Community Beginner ,
Jan 09, 2019 Jan 09, 2019

Copy link to clipboard

Copied

LATEST

Thank you, very useful code

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