Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


How To Replace Image on Layer with PhotoShop Script?

New Here ,
Sep 13, 2021 Sep 13, 2021

Copy link to clipboard

Copied

Hello, im trying to replace an layer image using photoshop script, can anyone point me in the right direction. Below is my code snippet:

 

var doc = app.activeDocument;

var imageGroup = doc.layerSets.getByName('Front');

var oldImage = imageGroup.layers[3].layers[2];

 

var importOptions = new ImportOptions(new File("C:/../newImage.png"));

TOPICS
Actions and scripting, Windows

Views

94

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Is the Layer in question a Smart Object? 

 


var importOptions = new ImportOptions(new File("C:/../newImage.png"));


Where did you get »ImportOptions« and what is it intended to be? 

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
community guidelines
New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

No this is is layer with a image attach to it. Im not sure what function (new ImportOptions) does, I copied this from a blog.  Im also getting the error below, could this error mean that Im using a old version of Photoshop. I currently have Photoshop CS6.

The error::::::

 8800; General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

- The command "Replace Contents" is not currently available.

Line: 27

->  executeAction( idplacedLayerReplaceContents, desc3, DialogModes.NO );

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied


No this is is layer with a image attach to it.

What is that supposed to describe? 

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible? 

 

»Replace Contens« refers to Smart Objects, so you may want to create a file in which the Layer-whose-contents-are-to-be-replaced is a Smart Object right away. 

 

Only posting fragments of the code you are using makes a meaningful discussion dificult. 

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Replace Content is applicable only to smarts object layers object.  Replacement objects must be the same size as the object as the object embedded or linked to in the layered template file. That is the same Aspect Ratio, Canvas size  and Print resolution.  For All Smart Object layers have an Object Transform which scale positions and warps the object for its use in the mockup.  This transform is not Change or replaced when you use Replace Content. Smart Objects may be shared with other smart object layers which may have different Transform like in a Picture Package mockup.   There is also a potential  problem using Image File the have transparent boards like PNG files can have.   Copy/Paste Duplicate  Layer, Place may trim the layers to the late subject bounds  any fully transparent boarder may well be striped.  I handle the by filling top left pixel and bottom left canvas two pixels with a 1% opacity 50% gray pixel. Smart Object Layers often have unlinked Layer mask in templates so even if you try to replace object with larger objects not only would the transform be wrong for the object larger content would also be masked off.    Copy/paste, drag drop, duplicate layer and Place all add new later  Replace content is the only way I know that replaces and existing  and Place may have been user to create the Smart object layer.

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Paste a link to a blog where you found the code.

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
community guidelines
New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

I received the code from github here replace_smartobject_image.js 

 

I made the layer a smart object, so that error no longer shows. But now there's a dialog that opens asking me to choose the new file/smart object. Is there a way I can dynamically remove the dialog, so the the code dynamically chooses the file/smart object. I think the issue now is executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO); . Its popping up a dialog that I don't want. I want the code to select the file. Below is my code snippet

doc.activeLayer =  myGroup.layers[3].layers[1];
var theLayer = doc.activeLayer;
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;

// Check if layer is SmartObject;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
	alert("selected layer is not a smart object")
} else {
	// Select Files;
	var PathFile = "C:\..\bg.jpg";
	var myFile = File(PathFile);
	if (myFile) {
		// Replace SmartObject
		theLayer = replaceContents(myFile, theLayer);
	}
}

function replaceContents(newFile, theSO) {
	app.activeDocument.activeLayer = theSO;
	var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
	var desc3 = new ActionDescriptor();
	var idnull = charIDToTypeID("null");
	desc3.putPath(idnull, new File(newFile));
	var idPgNm = charIDToTypeID("PgNm");
	desc3.putInteger(idPgNm, 1);
	executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
	return app.activeDocument.activeLayer;
};

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
community guidelines
Adobe Community Professional ,
Sep 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

LATEST

Please post meaningful screenshots already, not just snippets. 

Is PathFile actually meaningful in your code? 

 

Does changing the replace-function make a difference? 

 

function replaceContents (newFile, theSO) {
app.activeDocument.activeLayer = theSO;
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc244 = new ActionDescriptor();
desc244.putPath( charIDToTypeID( "null" ), new File( newFile ) );
executeAction( idplacedLayerReplaceContents, desc244, DialogModes.NO );
return app.activeDocument.activeLayer
};

 

 
 

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
community guidelines
New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Actually I intended the function (new ImportOptions(new File("C:/../newImage.png")), to replace the old image (var oldImage ...).

 

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied


@seanace3200 wrote:

Actually I intended the function (new ImportOptions(new File("C:/../newImage.png")), to replace the old image (var oldImage ...).


What is »new ImportOptions« supposed to be? Where did you get it from? 

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
community guidelines
New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Sorry that function was a typo. Im not using that function anymore. I just converted the layer into a Smart Object.

Im now getting a error that says "Object expected"

 

Below is the updated code/function

 

var PathFile = "C:\\..\\newImage.png";

var oldImage = ssnGroup.layers[3].layers[1]; // image shared object
var newImage = File(PathFile);

replaceContents( newImage,oldImage);

 

function replaceContents(newFile, theSO) {
app.activeDocument.activeLayer = theSO;
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer;
};

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
community guidelines
Adobe Community Professional ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

You need to show the full script  the message states  that

var oldImage = ssnGroup.layers[3].layers[1]; // image shared object 

 

did not set oldImage to your document  smart object Layer's  layer object

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
community guidelines