Highlighted

Batch replace Smart Objets of same dimension and resolution

Community Beginner ,
Dec 17, 2018

Copy link to clipboard

Copied

c.pfaffenbichler​ can you please give me a hand here?

I'm using your script to replace a SO and all the images have the exactly same size and resolution.

Problem: The SO on the result images are scale down, still in correct position and perspective, but not in the original size of the reference.

Thank you!

// replace smart object’s content and save jpgs;

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var thePath = myDocument.path;

var theLayer = myDocument.activeLayer;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// jpg options;

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 9;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

// check if layer is smart object;

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

else {

// select files;

if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.psb;*.tif", true)}

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

if (theFiles) {

// work through the array;

for (var m = 0; m < theFiles.length; m++) {

// replace smart object;

theLayer = replaceContents (theFiles, theLayer);

var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

//save jpg;

myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);

}

}

}

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

if (theFile.name.match(/\.(psd|tif|psb)$/i) != null || theFile.constructor.name == "Folder") {

return true

};

};

////// replace contents //////

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

};

Thanks for the files.

It seems the SO has 300ppi and the replacement images 1200ppi, which would explain a change in size.

TOPICS
Actions and scripting

Views

535

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

Batch replace Smart Objets of same dimension and resolution

Community Beginner ,
Dec 17, 2018

Copy link to clipboard

Copied

c.pfaffenbichler​ can you please give me a hand here?

I'm using your script to replace a SO and all the images have the exactly same size and resolution.

Problem: The SO on the result images are scale down, still in correct position and perspective, but not in the original size of the reference.

Thank you!

// replace smart object’s content and save jpgs;

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var thePath = myDocument.path;

var theLayer = myDocument.activeLayer;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// jpg options;

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 9;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

// check if layer is smart object;

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

else {

// select files;

if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.psb;*.tif", true)}

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

if (theFiles) {

// work through the array;

for (var m = 0; m < theFiles.length; m++) {

// replace smart object;

theLayer = replaceContents (theFiles, theLayer);

var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

//save jpg;

myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);

}

}

}

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

if (theFile.name.match(/\.(psd|tif|psb)$/i) != null || theFile.constructor.name == "Folder") {

return true

};

};

////// replace contents //////

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

};

Thanks for the files.

It seems the SO has 300ppi and the replacement images 1200ppi, which would explain a change in size.

TOPICS
Actions and scripting

Views

536

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
Dec 17, 2018 0
LEGEND ,
Dec 17, 2018

Copy link to clipboard

Copied

So far I could not reproduce the issue, can you provide files for testing (the containing image with just the SO and a few of the substitution files that result in incorrect results)?

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...
Dec 17, 2018 0
Community Beginner ,
Dec 17, 2018

Copy link to clipboard

Copied

Images for testing

Actually I'm using this script:

// replace smart object’s content and save psd;

// 2011, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var thePath = myDocument.path;

var theLayer = myDocument.activeLayer;

// psd options;

psdOpts = new PhotoshopSaveOptions();

psdOpts.embedColorProfile = true;

psdOpts.alphaChannels = true;

psdOpts.layers = true;

psdOpts.spotColors = true;

// check if layer is smart object;

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

else {

// select files;

if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.psd;*.tif", true)}

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

if (theFiles) {

// work through the array;

          for (var m = 0; m < theFiles.length; m++) {

// replace smart object;

                    theLayer = replaceContents (theFiles, theLayer);

                    var theNewName = theFiles.name.match(/(.*)\.[^\.]+$/)[1];

//save jpg;

                    myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".psd")),psdOpts,true);

                    }

          }

}

};

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

     if (theFile.name.match(/\.(psd|tif)$/i) != null || theFile.constructor.name == "Folder") {

          return true

          };

     };

////// replace contents //////

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
Reply
Loading...
Dec 17, 2018 0
LEGEND ,
Dec 17, 2018

Copy link to clipboard

Copied

Thanks for the files.

It seems the SO has 300ppi and the replacement images 1200ppi, which would explain a change in size.

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...
Dec 17, 2018 0
betovzky LATEST
Community Beginner ,
Dec 17, 2018

Copy link to clipboard

Copied

You are completely right! Thank you very much!!

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...
Dec 17, 2018 1