Photoshop script. Edit SmartObject

Community Beginner ,
Nov 23, 2020

Copy link to clipboard

Copied

Hi everyone!

There is a mockup .psd graphic file.

Inside the file is smart object.

How can I access and edit this smart object? (change layer, delete layer).

I try to access mockup as a normal layer, but it doesn't work.

 

  var mockup    = open(File('mockup.psd'));
  app.activeDocument = mockup

  var layerRef = app.activeDocument.artLayers.getByName("Layer_name");
  layerRef.remove(); //just to delete the Layer for example

image.png

I would be glad if there are examples about how is possible edit a smart object.

Thanks.

Adobe Community Professional
Correct answer by Kukurykus | Adobe Community Professional

 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

 

TOPICS
Actions and scripting, Mac, Make It, SDK, Windows

Views

150

Likes

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

Photoshop script. Edit SmartObject

Community Beginner ,
Nov 23, 2020

Copy link to clipboard

Copied

Hi everyone!

There is a mockup .psd graphic file.

Inside the file is smart object.

How can I access and edit this smart object? (change layer, delete layer).

I try to access mockup as a normal layer, but it doesn't work.

 

  var mockup    = open(File('mockup.psd'));
  app.activeDocument = mockup

  var layerRef = app.activeDocument.artLayers.getByName("Layer_name");
  layerRef.remove(); //just to delete the Layer for example

image.png

I would be glad if there are examples about how is possible edit a smart object.

Thanks.

Adobe Community Professional
Correct answer by Kukurykus | Adobe Community Professional

 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

 

TOPICS
Actions and scripting, Mac, Make It, SDK, Windows

Views

151

Likes

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
Nov 23, 2020 0
Adobe Community Professional ,
Nov 23, 2020

Copy link to clipboard

Copied

 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

 

Likes

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
Reply
Loading...
Nov 23, 2020 1
Most Valuable Participant ,
Nov 23, 2020

Copy link to clipboard

Copied

Also all Smart object layer objects are not Photoshop object. If you open a Smart Object Layer object that is a vector smart object the temp work file that Photoshop created will open in  AI and Camera RAW work files will open in ACR not Photoshop. Your Photoshop script will not be able to control the edit process.  In my Photoshop Mockup populating scripts, I test what kind of object a smart object  layer contain. If they are not a photoshop object my scripts will inform the user their template layer is not compatible with my script edit option and the smart object layer will not be repopulated.  If a mockup template has a smart object layer the needs to populated has a none Photoshop object  that mockup cans only be populated using my script default method of operation the uses Layer>Smart Objects>Replace Content.  All replacement object need to has the same Aspect Ratio, Size and Print resolution as the objects in the template for proper population. Changing the size of an object does not work when it comes to templates. Template may have several transform and wrappings recorded into the template for objects for other smart object layers can share a single object.  Even if there is only a single smart object layer there is a recorded object transform to generate the layers pixels from the hardened object pixels and that transform cans include warping of the object for the mockup template's use of the of the object and the Layer may also be  mask. Changing an object size populating a template should not be done.

JJMack

Likes

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
Reply
Loading...
Nov 23, 2020 1
Community Beginner ,
Nov 24, 2020

Copy link to clipboard

Copied

Thank you! It works!

 

Could I ask you one more question? When I make ExportAs:

 

app.runMenuItem(stringIDToTypeID("exportDocumentAsDialog"));

 

How can I confirm and click the "export" button after the dialog box?

 

P.S can't listing this action because doesn't work and I don't know why.

Likes

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
Reply
Loading...
Nov 24, 2020 0
Most Valuable Participant ,
Nov 24, 2020

Copy link to clipboard

Copied

I do not think that is possible with a Photoshop Script. You should use Save As instead of an interactive export.  You may be able to use some system add one to interact with an Applications UI. With Photoshop  scripting you can not  get or set the Photoshop cursor position. So if there is some system add on you can use it would need to be able position photoshop cursor in the export dialog and click.

 

Also to update a smart object you need to use save for the work document.  So the Temp  Work file is updated  only then will Photoshop update the smart object layer's object with the updated content.

JJMack

Likes

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
Reply
Loading...
Nov 24, 2020 0
Adobe Community Professional ,
Nov 24, 2020

Copy link to clipboard

Copied

Only Save/Save As and Export/Save for Web (Legacy)... can be recorded into an action or used in a script, you can't use the "new" Export As or Quick Export As ### options.

Likes

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
Reply
Loading...
Nov 24, 2020 1
Adobe Community Professional ,
Nov 24, 2020

Copy link to clipboard

Copied

Not scriptable. The only thing you can do is to adapt .js files that are in:

 

'C:\Program Files\Adobe\Adobe Photoshop 2021\Required\Plug-ins\Generator'

 

It'll be hard for you to change them though, that generator would work the way you wish.

 

As to my previous answer if it works, mark is as correct solution 😉 or you want me to do it 😉

Likes

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
Reply
Loading...
Nov 24, 2020 1
Most Valuable Participant ,
Nov 27, 2020

Copy link to clipboard

Copied

Someone post a PSD that has a Smart object layer  that if had a script 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

and that was all the smart object would  would open in Photoshop as a two layer PSB the top layer a vector smart object layer over a Shape Layer.   Using a double click in Photoshop also opened the object psb.

 

However if the script had other code after the 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

The smart object did not open the document that has the object was still active and my Mockup script proceeded to destroy the mockup template.

 

So I used the scriptlistener the see what was recorder when I open  the smart object in Photoshop.  To my surprise after

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

there were other steps after that one and  one was an open for the Object work file  created by Photoshop by in temp space.

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

 

So I was able to update my Mockup script toe handle this Slar Object the do not open  with 

runMenuItem(stringIDToTypeID('placedLayerEditContents'))

However, I a windows user and the code I added will only work one windows.  I'm posting the code here the see if some Mac user can help fix the code to also work on a Mac.

 

openSmartObject (app.activeDocument.activeLayer);
////// open smart object //////
function openSmartObject (theLayer) {
	current = app.activeDocument;
	if (theLayer.kind == "LayerKind.SMARTOBJECT") runMenuItem(stringIDToTypeID('placedLayerEditContents'));
	if ( current == app.activeDocument) {
		//alert("Did mot open all the way");
		try {         
			var r = new ActionReference();     
			r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));  
			r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);  
			var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));         
			}  
		catch (e) { throw theLayer + " Smart Object Did not Open"; }  
		if ($.os.search(/windows/i) != -1) 	var workFile = new File( "~/AppData/Local/Temp/" +  name);  // Windows user ID system temp space
		else var workFile = new File( "~/AppData/Local/Temp/" +  name);  // should be in Mac users temp space I would think
		if (workFile.exists) app.open(File(workFile));
		if ( current == app.activeDocument) {	
			throw theLayer + " Smart Object Did not Open";
			}
	}
	return app.activeDocument
};

 

JJMack

Likes

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
Reply
Loading...
Nov 27, 2020 0
Adobe Community Professional ,
Nov 27, 2020

Copy link to clipboard

Copied

I tested on CC2019 on Mac (Mojave) and it worked OK for me JJMack!

 

  • Raster layer converted to SO, all good!
  • Placed embedded converted to SO, all good!
  • Place linked converted to SO, all good!

 

That being said, the path on a Mac is not so easy, it is randomly generated to some extent:

 

Screen Shot 2020-11-28 at 10.12.50.png

Likes

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
Reply
Loading...
Nov 27, 2020 0
JJMack LATEST
Most Valuable Participant ,
Nov 28, 2020

Copy link to clipboard

Copied

I forgot about the read only Folder object Photoshop scripting has  Folder.temp so  that should be correct then for both platforms  on windows it will be  "~ /AppData/Local/Temp/" where

" ~ " is "/c/users/usermane/" so  (Folder.temp + "/" + workfile) should work for me on both platforms.  so I changed my  open smart object code to use that when "placedLayerEditContents" Fails to open the objects workfile.

JJMack

Likes

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
Reply
Loading...
Nov 28, 2020 1