Skip to main content
New Participant
October 31, 2019
Answered

ExtendScript Photoshop Edit Contents Smart Layer

  • October 31, 2019
  • 6 replies
  • 17589 views

I have a psd file with an embedded psd file. Using photoshop normally, I double on the layer icon to edit the contents (or choose "edit contents" from the dropdown). I can not seem to find out how to open/edit it with extendscript/javascript. It is a legend that we use on our plans and would like to add content via scripting but need to find out how to edit the contents first. Note: this is not a linked file externally, it is embedded.

Thanks in advance.

This topic has been closed for replies.
Correct answer Stephen Marsh

One way:

 

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

 

https://community.adobe.com/t5/Photoshop/Script-to-Export-Contents-of-all-Smart-Objects-in-a-Photoshop/m-p/10644877

6 replies

JJMack
Community Expert
Community Expert
January 5, 2021

yes you fixed it

JJMack
pixxxelschubser
Community Expert
Community Expert
January 5, 2021



@schroef  wrote:
"… Sorry my message didnt post a couple times, then i got some kind of ban and needed to wait a hour. Kinda stupid dumb it does a page refresh and not post the message. Twice did i made a long new post, both times lost 

:(

 This forum still isnt working really well i guess. …"


test

schroef
Inspiring
January 6, 2021

@pixxxelschubser 

thanks for trying, i just posted my experience above and added some comments about what happened.

Sadly i didnt screengrab every error i got. Only the "wrong/bad HTML" code error

Sil.C
Community Manager
Community Manager
January 5, 2021

test

schroef
Inspiring
January 6, 2021

This issue happened when i was posting message quite fast within the hour. Thats also why i got the "wait 3600 second" warning/error.

 

I also see a lot of errors when i post code which have comments in them from either community links or other website. It then shows an error about "faulty html" code or so. I did make a screengrab of that error i get a lot when pasting script as Javascript. Sometimes when i checked the pasted script, i noticed parts where missing. But also i noticed sometimes parts where added. I looked like it grabbed text of the forum link i pasted in the script as a comment.



What i did see a lot was that when i pressed post, the page would reload and nothing showed. No error not hint of bad embed text nothing, my complete message was gone. This happened quite a lot of times. Then eventually after witing a day i still could reply to a post made by JJMack. I could only reply to the main question on top.

Could it be that threads have some kind of limit of responses? Because i was posting quite a lot script code each time, perhaps there something like a "max character count", i dont know. You would need to ask the developers of the forum.

The fact that you think its posting and it doesnt is nasty. You cant go back and you basically lost your complete message.

 

 Hope this helps to understand what i was experiencing and perhaps can help to backtrack the issue.

 

PS there was actually another weird error i got. Its an error where it also stated that "invalid HTML" is used and that its being highlighted, yet nothing is highlighted then?!

schroef
Inspiring
January 5, 2021
 
Sorry my message didnt post a couple times, then i got some kind of ban and needed to wait a hour. Kinda stupid dumb it does a page refresh and not post the message. Twice did i made a long new post, both times lost 😞 This forum still isnt working really well i guess.


Anyways here's the link. https://we.tl/t-amPqhIM4pC
I had it ready forgot about, but than it also got delete twice because of the prior post issue

I finally got it working again. I now open the smartobject between the 2 dialogs. Somehow the GUI does update then properly. I than use command to switch documents. I also delete the prior delete file to keep smaller when saving. I thought this part of the code would make the script run longer. But it actually faster then. Sometimes its about 1 sec faster running it with 6 designs.

It was quite weird actually, because at one point i already had it working. Then for extra check i saved the main template closed photoshop opened it again and then suddenly it would run. So this time i did save the script file into a new version from the point it was working and then started fine tuning. 

I notice that when functions are below in the script this can cause issues. Now i make sure the functions are earlier in the lines. Im not sure im 100% cirrect on this, but it seems this does influence the script sometimes.

Im also suspending the history, i believe this also speeds things up since they dont neet to be written to the log and the GUI.

Below version works, ive tested it multiple times now while opening and closing the main file and restarting Phososhop a couple times. It works just a bit nicer now then the other version i had yesterday, there i opened the smartobject from the start manually.
 
after i waited a day now it still wont allow me to post a comment here?!? Im trying this with a linked version of the script. I also had to reply to the first comment. I cant reply to your latest comment, it will refresh and nothing is posted. Therefor i tagged you using your name, that way im sure you are notified
JJMack
Community Expert
Community Expert
January 5, 2021

The problem is strange and does not always fail the same way.  The Problem seems to be related to some smart object layer and the way the are crated.  For example the only template I had where the smart object did open right away with 

 

 

 

 

		runMenuItem(stringIDToTypeID('placedLayerEditContents'));

 

 

 

 

was one I download from the web.  When I replicated the Template on my Windows machine it did open right away.  However, if I ran the same batch job sever time in a row occasionally the object would fail to open right away in the middle of a batch but  the code I added to wait for it to open or to add a open would  solve the problem most of the time. Waiting always seem to work however occasionally other problems would happen if I added the open command. Perhaps I open  and update the object  then the opened again open again and missed up my script because it was the processing the wrong document.

 

Now the Template you posted only one smart object layer needs to be updated  but the template has several smart object layers on  the top of the stack.  My mockup scripts are design to only update the top of the  layers stack smart object layers.  It is easy to convert  Mockup templates to be compatible with my scripts design.  So I modified your template.  I simply grouped all the layer above the Background into a group named Mockup. I the used Ctrl+J to dupe the smart object layer that needs to be updated and move the copy out of the mockup group to the top of the layer stack and I removed the masks changed opacity and fill to 0 and turn off its visibility. 

 

Right away the Smart Object fail to open but my script had not problem populating the template.   I then replicated  your template.  I saved the smart object work file as a PSD File.   I them placed the PSD file into the replicated template above the smart object layer that needs the replacement. and did a Ctrl+J to dupe it and moved the dupe to the top of the stack set fill and opacity  to 0 and turn off its visibility. and deleted the old top of the stack smart object layer..  I then  moved the vector layer mask off of the smart object layer  to the placed psd smart object layer and the deleted the smart object layer that needed to be replaced.  I clipped the Smart object layer to the Phone case layer at the sized the object with a transform to cover the phone case and Saved the Template.   On my machine this template object open right away. Running many batches occasionally it  may fails to open.    This is a strange problem.  I have not test in PS 2020 just 2021.

 

Here is a link to a zip file the has my text templates and scripts Mockups.zip 

JJMack
JJMack
Community Expert
Community Expert
January 5, 2021

I have now tested with CS6,  CC 2014, CC 2018, CC 2019, PS 2020 and PS 2021.  All seen to fail to open the Smart Object Layer's  object that your Mac created right away like the one I had downloaded.  Where in PS 2021 version 22.1 if I  wait using a wait till message the object will open.   In prior Photoshop the object never opens I have to add the open for the temp work file that was created by Photoshop for placeLeyerEditContents  but did not open.  

 

So the scripts in my toolkit work in Photoshop version prior to PS 2021 on windows machines.  I'm not positive they will on Mac for I'm not sure of where Photoshop created the temp work file on a mac.  This is what I use for Windows it may be the same for  Mac. depends on Forder.temp beint the place the file is created

 

 

var workFile = new File(Folder.temp + "/" +  name);  // May work for both Windows and Mac

 

 

 

PS 22.1 can occasionally have problem if I add the Open Command. However,  adding the open performs faster than waiting for the object to eventually open So I will not be changing my scripts for waiting requires human interaction which is not appropriate for a batch process.

 

I look at you script its seem to work on my machine but has issues with Place scaling images  and it  leave the object open.   I particularly do not like templates that have smart object  layer where the object is layered and to populate  the template you have to edits late within the object. Templates like that are too hard to use and  requite a custom script for automation.  It is far easier to create templates that simply need the smart object replaced.  So I simplified your Template or the one you purchased or downloaded.   I rasterized some layers  and moved the tool bar layers into the template from the object and added a camera layer.  This template can be batch processed with my script and if you use its edit option replacement images can be any size. The phone screen will be filled with image content.  If replacement images  are Phone size my scripts default isuse  Replace content not edit here the template. samsung-s20-mockup-xxx.psd 

 

http://www.mouseprints.net/old/dpr/samsung-s20-mockup-xxx.psd 

JJMack
JJMack
Community Expert
Community Expert
November 1, 2019

A PSD Object will open in Photoshop and your script will still be in cintrol of Photoshop and can edit the opened object. However, you need to be careful opening Smart Object Layers contents using A Photoshop script.  There are many ways smart object layers are created. All smart Object Layers's Object are not Photoshop Objects.  If you open them in a Photoshop script they will open in other applications or a Photoshop ACR Plug-in. The Photoshop script that opened the object  will lose control till the Application or pug-in execution finishes The script can not edit such objects.   R-bin posted code you can use to test the type object a smart object contains.  To determine if the object will open in Photoshop or not. I use that code in my Batch Photoshop Mockups scripts when the user want toe edit object rather than replace smart object layer object using replace content.   My scripts can not handle editing Mockup templates smart object layers that have none Photoshop object. Only replace content is supported  for none when Smart Object layer have none Photoshop Objects.

 

 

 

 


function objectIsPsObject(SOlayer) {
	//Thanks to r-bin
	var ext = smartobject_file_ext(SOlayer);  
    var rc = true;
	switch (ext)  
		{  
		case "nef":  
		case "cr2":  
		case "crw":  
		case "raf":  
		case "orf":  
		case "mrw":  
		case "dcr":  
		case "mos":  
		case "raw":  
		case "pef":  
		case "srf":  
		case "dng":  
		case "x3f":  
		case "erf":  
		case "sr2":  
		case "kdc":  
		case "mfw":  
		case "mef":  
		case "arw":  
		case "nrw":  
		case "rw2":  
		case "rwl":  
		case "iiq":  
		case "3fr":  
		case "fff":  
		case "srw":  
		case "ai":
		case "svg":
		case "pdf":
		case "esp":
			rc = false.	
			break;  
		case "error":  
			rc = false;
			break;         
  		default:  
			rc = true;
			break;  
    }  
	return rc;
}

 

 

 

 r-bin code 

JJMack
Stephen Marsh
Community Expert
Community Expert
November 1, 2019

JJMack, the code as posted does not work...

 

When I add:

 

objectIsPsObject();

 

An error is returned from your posted code:

 

Runtime Error: Error Code# 24: smartobject_file_ext is not a function on the following code entry:
 
var ext = smartobject_file_ext(SOlayer);
JJMack
Community Expert
Community Expert
November 3, 2019

The function is complete.   A function is not a complete script. And your one line has problems it will work only if the active layer is a smart object layer and the object is supported by photoshop.  It will fail in all other cases. Here no active layer and also with an  active layer that is a smar object layer with an object that photoshop does not support. In the case vector smart object. That object opened in Chrome on my machine. It an SVG file create by Adobe AI.

 

 

The code does not open open a smart object layer's object.  The code test a smart object layer to see if you use open smart object layer object to open the object into Photoshop.   If you pass your smart object layer that has a PSB object it would return true you can.   Your line of code  edits Photoshop active document active layers smart object. 

 

 

 

 

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

 

 

 

 

There are problems with that if not user properly. If the active layers is not a smart object layers the lines will through a script error. If the active layers is a smart object layers and the object is not a Photoshop supported object it will not fail. However, the script will lose control  and not be able to edit the object for it will not open in Photoshop. An other Application like AI or a Plug-In like ACR will be what open the object not Photoshop. If you know active layer is a smart object layer and the layer's object is a Photoshop object your code will work.  I your not sure of what kind of object the smart object is you need r-bin code or your script will fail its Murphy's law.

Your one line should be a if statement. With or without the else.

 

 

 

 

if (objectIsPsObject(activeDocument.activeLayer)) app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
else alert(activeDocument.activeLayer.name + " Object can not be edited using Photoshop its unchanged.");

 

 

 

I do not know JavaScript or Photoshop Scripting. I do have a programming background so I hack  at code.  You are learning you need to read more code to get a better understanding of what is what. It should not be hard for you read that function to see all it does is return True or False. 

 


We all make mistakes and I did forget to paste the inner function smartobject_file_ext. So you were right when you posted the code I posted does not work.  So here is a  script your one line script to open Photoshop active layers smart object layer object in  Photoshop for editing. The code to edit the object is not included. However. That code should end with a Close that Saves the edits. So the opened work file will  closed out of Photoshop so the active document will become the original current document  Its Active Smart object layer's  object will be updated and new pixels rendered for the layers content.  As the Script stands there will be at least two open documents in Photoshop when it ends. The original document you ran the script from and the opened temp work document. Should] the object be a Photoshop object.

 

 

if (!documents.length) alert('There are no documents open.', 'No Document');
else {
   if (app.activeDocument.activeLayer.kind!=LayerKind.SMARTOBJECT) alert("ActiveLayer not Smart Object Layer");
   else {
      if (!objectIsPsObject(activeDocument.activeLayer)) alert(activeDocument.activeLayer.name + " Object can not be edited using Photoshop its unchanged.");
      else app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
      // Add code here to edit the smart object temp work file

     // Close the work file with save to clost the temp wok file out of Phtoshop and have Photoshop updater the orifinals smart object layers objeyy
   }
}

function objectIsPsObject(SOlayer) {
	//Thanks to r-bin
	var ext = smartobject_file_ext(SOlayer);  
    var rc = true;
	switch (ext)  
		{  
		case "nef":  
		case "cr2":  
		case "crw":  
		case "raf":  
		case "orf":  
		case "mrw":  
		case "dcr":  
		case "mos":  
		case "raw":  
		case "pef":  
		case "srf":  
		case "dng":  
		case "x3f":  
		case "erf":  
		case "sr2":  
		case "kdc":  
		case "mfw":  
		case "mef":  
		case "arw":  
		case "nrw":  
		case "rw2":  
		case "rwl":  
		case "iiq":  
		case "3fr":  
		case "fff":  
		case "srw":  
		case "ai":
		case "svg":
		case "pdf":
		case "esp":
			rc = false.	
			break;  
		case "error":  
			rc = false;
			break;         
  		default:  
			rc = true;
			break;  
    }  
	return rc;
}

function smartobject_file_ext(layer) {  
    try {         
        var r = new ActionReference();     
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));  
        r.putIdentifier(stringIDToTypeID("layer"), layer.id);  
        var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));         
         
        var n = name.lastIndexOf(".");  
        if (n < 0) return "";  
     
        return name.substr(n+1).toLowerCase();  
        }  
    catch (e) { return "error"; }  
}   

 

JJMack
Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 31, 2019

One way:

 

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

 

https://community.adobe.com/t5/Photoshop/Script-to-Export-Contents-of-all-Smart-Objects-in-a-Photoshop/m-p/10644877

New Participant
November 1, 2019

Thanks Stephen & JJmack, answer was right in front of me in the code but couldn't see it through the forest.