Highlighted

Script: Replace two Images/SO in Mockup and save as JPEG

Community Beginner ,
Nov 13, 2020

Copy link to clipboard

Copied

Hello there,

i have tried severall scripts posted on here, as well as the Photo Collage Toolkit.

I have a mockup which shows a book spread with a left and right page. There are two SOs placed on eigther sides and set on multiply.

 

1. Select source images. (best would be, to choose file 1 and 2, then 3 and 4, so i do not have to rename them 1left.jpeg and 1right.jpeg.)

2. The SO should be filled with the left and right pages.

3. Document should be saved as JPG.

 

I have found this script by laryn on github which works perfectly, but only replaces one SO.

 

// Replace SmartObject’s Content and Save as PSD
// 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;
    // Check if layer is SmartObject;
    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;*.jpg", true)
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].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|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject 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
};

 

How can i add the action to change the other SO aswell and choose the right files from the folder? Or can you recommend any other script?

 

Kindest regards,

Wallace

TOPICS
Actions and scripting, Mac

Views

79

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

Script: Replace two Images/SO in Mockup and save as JPEG

Community Beginner ,
Nov 13, 2020

Copy link to clipboard

Copied

Hello there,

i have tried severall scripts posted on here, as well as the Photo Collage Toolkit.

I have a mockup which shows a book spread with a left and right page. There are two SOs placed on eigther sides and set on multiply.

 

1. Select source images. (best would be, to choose file 1 and 2, then 3 and 4, so i do not have to rename them 1left.jpeg and 1right.jpeg.)

2. The SO should be filled with the left and right pages.

3. Document should be saved as JPG.

 

I have found this script by laryn on github which works perfectly, but only replaces one SO.

 

// Replace SmartObject’s Content and Save as PSD
// 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;
    // Check if layer is SmartObject;
    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;*.jpg", true)
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].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|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject 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
};

 

How can i add the action to change the other SO aswell and choose the right files from the folder? Or can you recommend any other script?

 

Kindest regards,

Wallace

TOPICS
Actions and scripting, Mac

Views

80

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 13, 2020 0
LEGEND ,
Nov 13, 2020

Copy link to clipboard

Copied

Please provide meaningful screenshots (including the pertinent Panels) to clarify the mock-up’s Layer structure an names. 

What are the naming conventions for the two placed »pages«? Maybe one selection might suffice if the corresponding files are unequivocally identifyable. 

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 13, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

Thanks for the reply.

This is the Mockup Structure, I know that SO1 and SO2 aren't relevant for this script, because it works with the selected SO. It's just a remain of another script of you that i have tried out. (Which is exactly what i am looking for, but it closes itself after running.)

 

layers.pngmockup.png

folder structure.pngoutput.png

 

 

 

I also have hundreds of images which need to be inserted into this mockup.

 

Regarding your second post: I think its the exact same script, just the comments are changed? Saving as a JPEG works perfectly fine, but only for the selected SO and i would like to replace both with one run of the script.

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 17, 2020 0
LEGEND ,
Nov 17, 2020

Copy link to clipboard

Copied

»Which is exactly what i am looking for, but it closes itself after running.«

Photoshop crashes or the image is closed? 

The last part is determined by the line 

myDocument.close();

and you could removed that. 

 

What exactly do you want to change about that Script? 

Having to manually choose each left- and right-side image manually would seem like a huge waste of time to me …

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 17, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

I wouldn't want to change anything. The script isn't saving anything, it just asks if i want to save the file or not, then closes whitout saving anything.

 

I open up the mockup with the two SOs, "SO1" and "SO2". I run the scipt, select the files called "1left.jpeg, 1right.jpeg,...", promt to whether to save or not, closed, nothing changed or saverd in the directory.

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 17, 2020 0
LEGEND ,
Nov 17, 2020

Copy link to clipboard

Copied

»jpeg« is not on the list, see the line 

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

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 17, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

files were named .jpg, bad typo sorry.

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 17, 2020 0
LEGEND ,
Nov 17, 2020

Copy link to clipboard

Copied

Just did a test and the results seem as intended.

the selected replacement images 

Screenshot 2020-11-17 at 14.08.30.png

the original layered image and the resulting images.

Screenshot 2020-11-17 at 14.08.15.png

 

Can you provide the layered image and some replacement images for testing? 

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 17, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

Thats the result that i was expecting.

Here are the files: https://gofile.io/d/q9LCdx

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 17, 2020 0
LEGEND ,
Nov 17, 2020

Copy link to clipboard

Copied

Try the following Script, it got me these results. 

MockUp_Spread_1right.jpgMockUp_Spread_2right.jpg

// https://forums.adobe.com/message/9640596#9640596
// select umages that have "left" and "right" in their names to replace smart objects callled "SO1" and "SO2" and save jpg-files;
// 2017, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
// select files;
if ($.os.search(/windows/i) != -1) {var theDesigns = File.openDialog ("please select files", "*.psd;*.psb;*.tif;*.jpg", true)}
else {var theDesigns = File.openDialog ("please select files", getFiles, true)};
if (theDesigns.length > 0) {
// get left and right;
var theLefts = new Array;
var theRights = new Array;
for (var d = 0; d < theDesigns.length; d++) {
if (File(theDesigns[d]).name.indexOf("right") != -1) {
	if (File(File(theDesigns[d]).parent + "/" + File(theDesigns[d]).name.replace("right", "left")).exists == true ) {
		theLefts.push(String(File(theDesigns[d]).parent + "/" + File(theDesigns[d]).name.replace("right", "left")));
		theRights.push(theDesigns[d])
		}
	};
};
// if equal number of left and right;
if (theLefts.length > 0 || theLefts.length == theRights.length) {
replaceTwoSmartObject (app.activeDocument, theLefts, theRights);
};
}
};
////// replace smart object content if only one smart object exists //////
function replaceTwoSmartObject (theFile, theLefts, theRights) {
var myDocument = theFile;
var theSmartObjects = collectSmartObjects2017();
var check1 = false;
var check2 = false;
for (var c = 0; c < theSmartObjects.length; c++) {
if (theSmartObjects[c][0] == "SO1") {
	theLeftSO = theSmartObjects[c]
	check1 = true
	};
if (theSmartObjects[c][0] == "SO2") {
	theRightSO = theSmartObjects[c];
	check2 = true
	};
};
// if the smart objects have been found;
if (check1 == true || check2 == true) {	
var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
// 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;
// work through the array;
for (var m = 0; m < theRights.length; m++) {
// replace smart object;
theLayer1 = replaceContentsByIdentifier (theLefts[m], theLeftSO);
theLayer2 = replaceContentsByIdentifier (theRights[m], theRightSO);
var theNewName = theRights[m].name.match(/(.*)\.[^\.]+$/)[1].replace("left", "");
//save jpg;
myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true);
}
};
myDocument.close();
};
////// get psds, tifs and jpgs from files //////
function getFiles (theFile) {
if (theFile.name.match(/\.(psd|tif|psb|jpg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
////// replace contents //////
function replaceContentsByIdentifier (newFile, theSO) {
selectLayerByID (theSO[1], false);
//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
};
////// collect smart objects, probably based on code by paul, mike or x //////
function collectSmartObjects2017 () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {theLayers.push([theName, theID])}
};
}
catch (e) {};
};
return theLayers
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
      desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
   try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};

 

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 17, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

That works as expected, thanks 🙂

Where was the mistake?

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 17, 2020 0
LEGEND ,
Nov 17, 2020

Copy link to clipboard

Copied

I don’t know, maybe I had made some corrections in the code after posting in that old thread. 

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 17, 2020 0
LEGEND ,
Nov 13, 2020

Copy link to clipboard

Copied

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 13, 2020 0
Most Valuable Participant ,
Nov 13, 2020

Copy link to clipboard

Copied

Did you try mine?  Many users use them. Free Photoshop Photo Collage and Mockup Toolkit  

The "BatchMockupTemplates.jsx"  can populate multiple mockup templates the need multiple  smart object layers populated.  With design from folders for each of the Smart object layer on the top of the layer stack.  If all replacement content have the correct size the Script will use replace content.  If some need to be resized for the template the script will fit each replacement into the top level smart object layers.

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 13, 2020 3
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

Jes, i have tried your script, which also looked very promising. I have created a custom Template which mimics the SOs. Running the script "BatchMultiImageCollage" works.

Problems are:

1. No output is being saved, the result is one open psd with placed images in the right spots.

2. Even when setting Layer Styles, nothing i changing in the ouput document.

 

collage_settings.png

 

When using "BatchMockupTemplate.jsx" with the Mockup which has 3 Layers, one Background an two SOs i get this error.

collage_error.pngcollage_settings.png

 

Really appreciate your time looking into this.

 

Wallace

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 17, 2020 0
Community Beginner ,
Nov 17, 2020

Copy link to clipboard

Copied

Don't mind my last post, i confused naming the images obj0,obj1... with the folder structure.

 

It works perfectly now.

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 17, 2020 1
JJMack LATEST
Most Valuable Participant ,
Nov 17, 2020

Copy link to clipboard

Copied

Yes there are two type of templates my package supports  Templates that use Alpha Channels to map Image location, size and shape.  And  Templates that have Smart Object layers that get replacement content.   The former always get PSD files saved so you can tweak  the populated template for the scripts do not support  rotation and distortion of placed in images.  However text  file name stamping is supported and  Text and Image layer styles cans be added by the scripts and jpg can optionally be saved.  Templates with smart object layer supports  rotation and distortion by the templates recorded object transform.  So jpeg files are saved.  However PSD file can optionally be saved if you want to be able to edit the populated template.  Replacement content can be done either with Replace content which requires replacement content to be the correct size or replacements files can be fitted to the templates object with various edit content options.

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 17, 2020 0