• 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 Replace Image on Layer with PhotoShop Script?

Community Beginner ,
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

2.8K

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 Expert ,
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? 

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

 

 

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

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, 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.

 

 

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? 

 

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 ,
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;
};

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, 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

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 ,
Sep 18, 2021 Sep 18, 2021

Copy link to clipboard

Copied

LATEST

Thanks so much all of you guys. I got it to worked. I actually had to first make the active document a smart object. Then check if the active layer is smart object. Then replace the layer using the replaceSO function like below

 

doc.activeLayer = myGroup.layers[3].layers[2];

var theLayer = doc.activeLayer;

// Check if layer is SmartObject;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {

var pics = "C:\\pics\\bg.jpeg");

replaceSO (pics);
}

 

function replaceSO(file){
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc5.putPath( idnull, new File( file ) );
executeAction( idplacedLayerReplaceContents, desc5, 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 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

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
LEGEND ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Paste a link to a blog where you found the 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
Community Beginner ,
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;
};

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

Copy link to clipboard

Copied

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

 

 
 

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