Skip to main content
Participating Frequently
November 13, 2020
Question

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

  • November 13, 2020
  • 3 replies
  • 890 views

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

This topic has been closed for replies.

3 replies

JJMack
Community Expert
Community Expert
November 13, 2020

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
Participating Frequently
November 17, 2020

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.

 

 

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

 

Really appreciate your time looking into this.

 

Wallace

Participating Frequently
November 17, 2020

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

 

It works perfectly now.

c.pfaffenbichler
Community Expert
Community Expert
November 13, 2020
c.pfaffenbichler
Community Expert
Community Expert
November 13, 2020

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. 

Participating Frequently
November 17, 2020

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

 

 

 

 

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.

c.pfaffenbichler
Community Expert
Community Expert
November 17, 2020

»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 …