Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Batch replace smart objects in mockup file

Community Beginner ,
Feb 10, 2018 Feb 10, 2018

Hi there,

I want to know if it's possible to import x-number of png images into a mockup template with a smart object and then all saved in jpgs? The following script is not working for me but it's probably close to what I need:

Batch replace smart objects

The script must do the following:

Search in folder for PNG files

Place the PNG image into the Smart Object (top center)

Save file as JPG into the source folder with the same name as the source file

For example:

Before the script

\Images\Image 1\PNG\Design1.png

\Images\Image 2\PNG\Design2.png

\Images\Image 3\PNG\Design3.png

\Images\Image 4\PNG\Design4.png

After the script

\Images\Image 1\PNG\Design1.png

\Images\Image 1\PNG\Mockup.jpg

\Images\Image 2\PNG\Design2.png

\Images\Image 2\PNG\Mockup.jpg

\Images\Image 3\PNG\Design3.png

\Images\Image 3\PNG\Mockup.jpg

\Images\Image 4\PNG\Design4.png

\Images\Image 4\PNG\Mockup.jpg

I hope this makes sense. I will appreciate if someone can give me an advise what is the best way to do that.

Kind regards,

Katt

TOPICS
Actions and scripting
16.6K
Translate
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 Beginner ,
Feb 20, 2018 Feb 20, 2018

I already tried the same line but the JPG is saved with the PNG name, not the Mockup name.

Translate
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 ,
Feb 20, 2018 Feb 20, 2018

Sorry I had misunderstood, try

myDocument.saveAs((new File(thePath+"/"+basename+".jpg")),jpgopts ,true);

Translate
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 ,
Feb 20, 2018 Feb 20, 2018

That's the line! Thanks again

Translate
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 ,
Feb 21, 2018 Feb 21, 2018

Hi again I thought "This is it!" but I was wrong. Now some of my designs are too short, as a result they are placed too high and they look ridiculous. Apparently I have to place them manually in the correct place, so I removed the line which saves them to jpgs. After I finish with all the designs I need to export them to PNGs with size of the SO (12.5x16) keeping the transparent area. That's how I can run the script and it will place those designs correctly onto the mockups. Now do I have to open another thread for the new script which exports the PNGs from the SO? Thanks

Translate
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 ,
Feb 22, 2018 Feb 22, 2018
That's how I can run the script and it will place those designs correctly onto the mockups.

What exactly is »correct«? What are the rules determining the position?

Apparently I have to place them manually in the correct place, so I removed the line which saves them to jpgs. After I finish with all the designs I need to export them to PNGs with size of the SO (12.5x16) keeping the transparent area.

What exactly are you describing here?

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

designtest.jpg

Sorry that I wasn't clear with the previous post. This is one example with a design which is not 12.5" x 16" (full design) in size and it's scaled to 12'5" width. With the last script the design is too high (top) on the garment and with the previous script (centered) alignment it's too low, so I need something in between. The position depends from the design due to the fact that some of these designs are completely different in length. Most of my designs are 12.5" x 16" so they fill the grey area (SO) and they look great.

So what's the Idea? The idea is to have all of them into the PSD file, checked one by one, positioned correctly if they are not (as the above example) and then exported to 12.5"x16" PNG files. The idea behind the 12.5" x 16" format is that next time when I run the script the design will be exactly on the right place and even if I change the mockup with different one as long as the SO is 12.5" x 16" in size they will be positioned correctly. I know it sounds complicated but for me at this point that's the only way to do it. The other way is to export all these PNGs one by one which will take more time. From now on I will save the designs in the 12.5" x 16" format so I guess it will be an one-off job to fix the position now. Thanks

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

To summarize:

1. I removed the command line which exports the mockup to jpgs. Now I have a PSD file with all the designs already imported into the SO, so I don't have to do that one by one when I fix the position of every design design, I will simply jump to the next layer.

2. Once they are fixed I have to export all the SO layers to PNGs with the size of the SO (12.5" x 16") - at this point nothing depends from the visible area - the design can be full (12.5" x 16"), half (12.5" x 8") or quarter (12.5" x 4") they will be all exported as 12.5" x 16" PNGs.

3. Once I have all the 12.5" x 16" PNGs I can run the script again with the command line which saves the PSD to jpgs.

Done!

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

So is there still a question or have you mastered the issue by yourself anyway?

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

The question is how to export the layers to their folders? Is it possible? How Photoshop will find out which layer from which folder is?

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

Now I've noticed that the script at some point started placing the top of the PNGs from the center instead from the top and it cuts the design in the middle if it's full size. What's wrong?

Translate
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 ,
Feb 22, 2018 Feb 22, 2018
Now I've noticed that the script at some point started placing the top of the PNGs from the center instead from the top and it cuts the design in the middle if it's full size. What's wrong?

I don’t know, I don’t have neither the files nor the final Script you are using.

Also didn’t you say you placed the images manually?

Quite frankly your process seems to have gone a bit wild.

When the Script was supposed to align the images to the top I posted the line

activeDocument.activeLayer.translate(0, activeDocument.activeLayer.bounds[1] * (-1))

which uses the Layer’s bounds values to move it to the top, so I am not sure what exactly is the purpose and the problem now.

If you want to manually determine the placement of the individual images without disrupting the rest of the process you could change

executeAction( idTrnf, desc24, DialogModes.NO );

to

executeAction( idTrnf, desc24, DialogModes.ALL );

and you should get the Transformation controls for each places png, but the Script should simply process after each one is OK-ed.

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

Probably I am doing it wrong. I removed the line which saves the PSD to JPG so I have:

// place png files in selected smart object and save jpg;

// 2018, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

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

//var thePath = myDocument.path;

var theLayer = myDocument.activeLayer;

// jpg options

var jpgopts = new JPEGSaveOptions();

jpgopts.embedProfile = true;

jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;

jpgopts.matte = MatteType.NONE;

jpgopts.quality = 10;

// check if layer is smart object;

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

else {

// select files;

var theFolder = Folder.selectDialog ("select folder");

var theFiles = retrievePngFiles (theFolder, []);

if (theFiles) {

// work through the array;

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

var thePath = File(theFiles).path;

// open smart object;

var smartObject = openSmartObject (theLayer);

var theLayer2 = smartObject.activeLayer;

// place file and fit to convas;

var theNewOne = placeScaleRotateFile (theFiles, 0, 0, 100, 100, 0);

scaleLayerToFitCanvas ();

hideOthers ();

// close;

smartObject.close(SaveOptions.SAVECHANGES);

// undo;

myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length - 1];

}

}

}

};

////// get png from subfolders //////

function retrievePngFiles (theFolder, theFiles) {

  if (!theFiles) {var theFiles = []};

  var theContent = theFolder.getFiles();

  for (var n = 0; n < theContent.length; n++) {

  var theObject = theContent;

  if (theObject.constructor.name == "Folder") {

  theFiles = retrievePngFiles(theObject, theFiles)

  };

  if (theObject.name.slice(-4).match(/\.(png)$/i) ) {

  theFiles.push(theObject)

  }

  };

  return theFiles

  };

////// place //////

function placeScaleRotateFile (file, xOffset, yOffset, theXScale, theYScale, theAngle) {

// =======================================================

var idPlc = charIDToTypeID( "Plc " );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

    desc5.putPath( idnull, new File( file ) );

    var idFTcs = charIDToTypeID( "FTcs" );

    var idQCSt = charIDToTypeID( "QCSt" );

    var idQcsa = charIDToTypeID( "Qcsa" );

    desc5.putEnumerated( idFTcs, idQCSt, idQcsa );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc6 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc6.putUnitDouble( idHrzn, idPxl, xOffset );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc6.putUnitDouble( idVrtc, idPxl, yOffset );

    var idOfst = charIDToTypeID( "Ofst" );

    desc5.putObject( idOfst, idOfst, desc6 );

    var idWdth = charIDToTypeID( "Wdth" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc5.putUnitDouble( idWdth, idPrc, theYScale );

    var idHght = charIDToTypeID( "Hght" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc5.putUnitDouble( idHght, idPrc, theXScale );

    var idAngl = charIDToTypeID( "Angl" );

    var idAng = charIDToTypeID( "#Ang" );

    desc5.putUnitDouble( idAngl, idAng,theAngle );

    var idLnkd = charIDToTypeID( "Lnkd" );

    desc5.putBoolean( idLnkd, true );

executeAction( idPlc, desc5, DialogModes.NO );

return app.activeDocument.activeLayer;

};

////// scale layer to fit canvas //////

function scaleLayerToFitCanvas () {

var ref = new ActionReference();

ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("bounds"));

ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var layerDesc = executeActionGet(ref);

var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));

var layerX = theBounds.getUnitDoubleValue(stringIDToTypeID("left"));

var layerY = theBounds.getUnitDoubleValue(stringIDToTypeID("top"));

var layerWidth = theBounds.getUnitDoubleValue(stringIDToTypeID("right"))-layerX;

var layerHeight = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))-layerY;

var ref1 = new ActionReference();

ref1.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var docDesc = executeActionGet(ref1);

var docWidth = docDesc.getUnitDoubleValue(stringIDToTypeID("width"));

var docHeight = docDesc.getUnitDoubleValue(stringIDToTypeID("height"));

var docRes = docDesc.getInteger(stringIDToTypeID("resolution"));

var scaleX = docWidth/layerWidth*docRes/72*100;

var scaleY = docHeight/layerHeight*docRes/72*100;

var theScale = Math.min(scaleX, scaleY);

layerX = ((docWidth*docRes/144) - (layerX+layerWidth/2));

layerY = ((docHeight*docRes/144) - (layerY+layerHeight/2));

activeDocument.activeLayer.translate(0, activeDocument.activeLayer.bounds[1] * (-1))

// =======================================================

var idTrnf = charIDToTypeID( "Trnf" );

    var desc24 = new ActionDescriptor();

//    desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ),

charIDToTypeID( "Qcs0" ) );

    desc24.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID(

"Qcsa" ) );

    var idOfst = charIDToTypeID( "Ofst" );

        var desc25 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc25.putUnitDouble( idHrzn, idPxl, layerX);

        var idVrtc = charIDToTypeID( "Vrtc" );

        desc25.putUnitDouble( idVrtc, idPxl, layerY);

    desc24.putObject( idOfst, idOfst, desc25 );

    var idWdth = charIDToTypeID( "Wdth" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc24.putUnitDouble( idWdth, idPrc, theScale );

    var idHght = charIDToTypeID( "Hght" );

    desc24.putUnitDouble( idHght, idPrc, theScale );

executeAction( idTrnf, desc24, DialogModes.NO );

};

////// open smart object //////

function openSmartObject (theLayer) {

if (theLayer.kind == "LayerKind.SMARTOBJECT") {

// =======================================================

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

    var desc2 = new ActionDescriptor();

executeAction( idplacedLayerEditContents, desc2, DialogModes.NO );

};

return app.activeDocument

};

////// hide others //////

function hideOthers () {

// hide others;

// =======================================================

var idShw = charIDToTypeID( "Shw " );

    var desc2 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var list1 = new ActionList();

            var ref1 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref1.putEnumerated( idLyr, idOrdn, idTrgt );

        list1.putReference( ref1 );

    desc2.putList( idnull, list1 );

    var idTglO = charIDToTypeID( "TglO" );

    desc2.putBoolean( idTglO, true );

executeAction( idShw, desc2, DialogModes.NO );

};

This gives me a Mockup template with all the PNGs into the SO and nothing else. Once I have the correct PNGs as I explained prevoiusly I will add the line which saves the PSD to JPG. But before that I need to export all the PNGs from the SO into their folders with the new size 12.5" x 16". I don't know if the script has an issue with the quantity of the PNGs imported into the SO but for me should be fine. If the script can import 1 png it can import a 1000 too because it repeats the same steps again and again. Weird!

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

I guess I am doing it wrong by importing the PNGs into the SO because I don't need the SO for this. I simply need the PNG imported and scaled into a PSD file with dimensions 12.5"x16". The question is if I have one PSD file and too scripts (one to import and one to export the PNGs) how Photoshop will know where to export the PNGs with the second script?

Translate
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 ,
Feb 22, 2018 Feb 22, 2018

It seems you had not thought the process through in the beginning.

If you had prepared all the pngs to fit the SO’s dimensions from the start (and said as much) the whole alignment issue would have been moot. And I would not have included the function to scale the placed png to fit the opened SO’s canvas.

But instead of manually placing the images within the Script-run you seem to be trying to break the task into two separate Scripts.

how Photoshop will know where to export the PNGs with the second script?

The placed pngs should have the files’ names, so at worst selecting the Folder again should allow identifying the original png-location.

You may want to start a new thread so someone else may help you.

Good luck!

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

Now all PNGs are saved as 12.5"x16" and still the script puts them randomly - too high too low. What's wrong? Why it's not working the same way for every file? I tried with few different mockups and the results are different.

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

The script now saves the first jpg and it's ok but then the next png is scaled to may be about 20% of it's size! I don't understand. Is there any error in the script which makes the whole process weird?

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

It your png images have transparent borders these will most likely be trimmed during the process the layers will not be 12.5x16 or even have that aspect ratio.  Try this when you save your png files before the save put a 1% opaque pixel in the top left and bottom right corners of the image so the borders will not be completely empty.

JJMack
Translate
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 ,
Feb 23, 2018 Feb 23, 2018

That's what I just discovered. Do you have a script which can take all the PNGs from the folder and then will put a pixel in both corners?

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

No Id do it manuall you should write script or record an action to do it..

JJMack
Translate
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 ,
Feb 23, 2018 Feb 23, 2018

Thanks, this was very helpful I am afraid that I have to do it one by one. One more day in hell...

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

If you have many to do why not simply record that into an action????

JJMack
Translate
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 ,
Feb 23, 2018 Feb 23, 2018

I have nearly a 1000 pngs to do. Half are already imported into the SO using the script from this thread, I just removed the part where the script saves the jpg. The only thing I have to do now is to export them into their own folders. I put on top of the layers a blank layer with 4 pixels in each corner with opacity 1%. I hope that works this time!

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

Do your exported PNG files the have the camvas size you want and are the items pixels in the area you want theme in?  What happened to the original PNG files you scripted into the mockup?

JJMack
Translate
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 ,
Feb 23, 2018 Feb 23, 2018

I think the problem is with the script not the PNGs I have now pixels in every corner with opacity 100% and still the script puts the designs in the middle or too high so the design is half. It doesn't make any sense. I even tried to resize the canvas to 101% X and Y, absolutely no difference. The more I try the more weird it gets!

Translate
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 ,
Feb 23, 2018 Feb 23, 2018

If the pngs have the same dimensions and resolution as the original SO then opening the SO and placing and scaling them is not useful – this was a precaution to handle unclear input.

Instead of that approach you should simply use Replace Contents.

Translate
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