Copy link to clipboard
Copied
can someone help me how to replace image on mockup with auto action or script and save it as jpeg ?
i try action but keep fail and dont know wahts wrong with my action script
i still new on photoshop and sry for my bad english
this is my mockup and photo example
Here is a script that will replace the selected Smart Object layer with a file from a source folder, then save the file as a jpg to an output folder. It will loop through the source folder and replace the original smart object with each file in the source folder, All the source files have to be the same size! In the script, I have the source folder being on the desktop and called "source files." The output folder is a subfolder, in source files, called "done." You can change the code to reflect
...Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
hey chuck i need your help, i try ue your script and this eror popout. I dont know how to fix it. already try any method but stil no work
Copy link to clipboard
Copied
DOD you try my suggestions on you other post regarding the names of the files? That first file in you screen shot has all that garbage lettering after it.
Copy link to clipboard
Copied
already change the name to 1 letter but still not work
Copy link to clipboard
Copied
sardonicHumour();
function sardonicHumour() {
alert("Thx for giving away free iPhone cases. Not every people want to share they product for free");
}
Copy link to clipboard
Copied
Can you post a screen shoot of your psd file with the layer panels showing? Also show what the file names are for the files in your source folder.
Copy link to clipboard
Copied
Something is not right with your file. It looks like PS crashed and you opned a recovered file, based on the name.
Copy link to clipboard
Copied
This happen after 5-8 image not from first image.
Copy link to clipboard
Copied
Try this script. It will add a number after the new saved file.
#target photoshop
var srcFolder = new Folder('~/desktop/source files/');
var outputFolder = new Folder('~/desktop/source files/done/');
var searchMask = '*.???'
var fileList = srcFolder.getFiles(searchMask);
var doc = activeDocument;
var curLayer = doc.activeLayer;
var soName
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var count = 1
for (var i=0;i<fileList.length;i++){
replaceSO (fileList[i]);
var fName = fileList[i].name.split('.')[0];
dupeFile ();
var doc2 = activeDocument;
try{
doc2.saveAs(new File(outputFolder +'/'+fName + '-flat-'+count+'.jpg'),jpgOptions);
}
catch(e){
count++
doc2.saveAs(new File(outputFolder +'/'+fName + '-flat-'+count+'.jpg'),jpgOptions)
}
doc2.close(SaveOptions.DONOTSAVECHANGES);
count++
}
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 );
}
function dupeFile(){
var idDplc = charIDToTypeID( "Dplc" );
var desc11 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idFrst = charIDToTypeID( "Frst" );
ref1.putEnumerated( idDcmn, idOrdn, idFrst );
desc11.putReference( idnull, ref1 );
var idMrgd = charIDToTypeID( "Mrgd" );
desc11.putBoolean( idMrgd, true );
executeAction( idDplc, desc11, DialogModes.NO );
}
Copy link to clipboard
Copied
Try this script. I don't know why they other one was stopping to save the image, but this one seems to work.
#target photoshop
var srcFolder = new Folder('~/desktop/source files/');
var outputFolder = new Folder('~/desktop/source files/done/');
var searchMask = '*.???'
var fileList = srcFolder.getFiles(searchMask);
var doc = activeDocument;
var curLayer = doc.activeLayer;
var soName
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var count = 1
for (var i=0;i<fileList.length;i++){
replaceSO (fileList[i]);
var fName = fileList[i].name.split('.')[0];
dupeFile ();
var doc2 = activeDocument;
try{
saveFile (outputFolder +'/'+fName + '-flat-'+count+'.jpg')
}
catch(e){
count++
saveFile (outputFolder +'/'+fName + '-flat-'+count+'.jpg')
}
doc2.close(SaveOptions.DONOTSAVECHANGES);
count++
}
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 );
}
function dupeFile(){
var idDplc = charIDToTypeID( "Dplc" );
var desc11 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idFrst = charIDToTypeID( "Frst" );
ref1.putEnumerated( idDcmn, idOrdn, idFrst );
desc11.putReference( idnull, ref1 );
var idMrgd = charIDToTypeID( "Mrgd" );
desc11.putBoolean( idMrgd, true );
executeAction( idDplc, desc11, DialogModes.NO );
}
function saveFile(saveName){
var idsave = charIDToTypeID( "save" );
var desc10 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc11 = new ActionDescriptor();
var idEQlt = charIDToTypeID( "EQlt" );
desc11.putInteger( idEQlt, 12 );
var idMttC = charIDToTypeID( "MttC" );
var idMttC = charIDToTypeID( "MttC" );
var idNone = charIDToTypeID( "None" );
desc11.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc10.putObject( idAs, idJPEG, desc11 );
var idIn = charIDToTypeID( "In " );
desc10.putPath( idIn, new File( saveName ) );
var idDocI = charIDToTypeID( "DocI" );
desc10.putInteger( idDocI, 474 );
var idCpy = charIDToTypeID( "Cpy " );
desc10.putBoolean( idCpy, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc10.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc10, DialogModes.NO );
}
Copy link to clipboard
Copied
Try this script. Hopefully, it will so what you want.
#target photoshop
var srcFolder = new Folder('~/desktop/source files/');
var outputFolder = new Folder('~/desktop/source files/done/');
var searchMask = '*.???'
var fileList = srcFolder.getFiles(searchMask);
var doc = activeDocument;
var curLayer = doc.activeLayer;
var soName
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var count = 1
for (var i=0;i<fileList.length;i++){
var layerName = doc.activeLayer.name
replaceSO (fileList[i]);
var fName = fileList[i].name.split('.')[0];
dupeFile ();
var doc2 = activeDocument;
try{
saveFile (outputFolder +'/'+fName + layerName +'-'+count+'.jpg')
}
catch(e){
count++
saveFile (outputFolder +'/'+fName + layerName + '-'+count+'.jpg')
}
doc2.close(SaveOptions.DONOTSAVECHANGES);
count++
}
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 );
}
function dupeFile(){
var idDplc = charIDToTypeID( "Dplc" );
var desc11 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idFrst = charIDToTypeID( "Frst" );
ref1.putEnumerated( idDcmn, idOrdn, idFrst );
desc11.putReference( idnull, ref1 );
var idMrgd = charIDToTypeID( "Mrgd" );
desc11.putBoolean( idMrgd, true );
executeAction( idDplc, desc11, DialogModes.NO );
}
function saveFile(saveName){
var idsave = charIDToTypeID( "save" );
var desc10 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc11 = new ActionDescriptor();
var idEQlt = charIDToTypeID( "EQlt" );
desc11.putInteger( idEQlt, 12 );
var idMttC = charIDToTypeID( "MttC" );
var idMttC = charIDToTypeID( "MttC" );
var idNone = charIDToTypeID( "None" );
desc11.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc10.putObject( idAs, idJPEG, desc11 );
var idIn = charIDToTypeID( "In " );
desc10.putPath( idIn, new File( saveName ) );
var idDocI = charIDToTypeID( "DocI" );
desc10.putInteger( idDocI, 474 );
var idCpy = charIDToTypeID( "Cpy " );
desc10.putBoolean( idCpy, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc10.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc10, DialogModes.NO );
Copy link to clipboard
Copied
Hi Chuck,
Thank you so much for sending this over!
At first I got the message "no matching closing brace found. Line 56," so I added one after the executeAction on Line 80 to match what you had done for the other functions.
Once I did that and went to load the script I unfortunately got the error "functionality may not be available in this version of Photoshop," specifically it looks like in reference to the executeAction on Line 79. Interestingly when I ran it a second time it indicated the Command "Replace Contents" in reference the executeAction on Line 38 was the issue.
I guess generally the executeAction is not happy. I currently have my Photoshop up-to-date. Is there an older version you work with that I should download to give this a try?
Thanks again so much for your help with this, you're amazing!
Best,
Matthew
Copy link to clipboard
Copied
It seems to be working for me, and I too am using the latest version. You do have the smart object layer selected, when you run the script?
Copy link to clipboard
Copied
Hi Chuck,
I have the script running now. Not sure why it wasn't running before.
I guess I have 2 questions now.
1) Do you think there is a way to get rid of the numbers being added at the end? It was able to combine the 2 file names perfectly otherwise. I can always just manually adjust these names though if not since the hard part was done by the script.
2) Weirdly although I needed all of these files as jpgs to be uploaded to the website as buttons for people to click on (thank you!), I also need these files separately as pngs without the white background since I am then going to be putting them mocked up on the finished product. I've put an example below so you can see what I am trying to accomplish as the end goal.
I tried to modify the script you sent over to do this by changing anywhere that references jpg to png. Unfortunately that caused an error message to pop up.
Essentially for step 2 of this process I guess png versions of the designs need to be imported onto the bags by the script from the "source files" folder, and then the new combined image has to be saved as a jpg (since the website will only take jpgs).
Thank you again, you're a saint!
Best,
Matthew
Copy link to clipboard
Copied
Hi Chuck,
I just thought of a better much more concise way of saying all of what I wrote above.
Essentially I think we need a version of the script that outputs Jpegs (without the numbers at the end of the name if possible), and then a basically identical version that outputs Pngs instead.
Thanks again,
Matthew
Copy link to clipboard
Copied
To save a png, you would need to use scriptListener to record saving a png then add that code before or after the code that saves the jpg. As far as the number added, just remove count along with the extra + sign in the two lines that create the file name:
(outputFolder+'/'+fName+layerName+'.jpg)
Of course, you will need to make lines for a png name.
I will have to look at this later.
Copy link to clipboard
Copied
Okay, try this script:
#target photoshop
var srcFolder = new Folder('~/desktop/source files/');
var outputFolder = new Folder('~/desktop/source files/done/');
var searchMask = '*.???'
var fileList = srcFolder.getFiles(searchMask);
var doc = activeDocument;
var curLayer = doc.activeLayer;
var soName
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var count = 1
for (var i=0;i<fileList.length;i++){
var layerName = doc.activeLayer.name
replaceSO (fileList[i]);
var fName = fileList[i].name.split('.')[0];
dupeFile ();
var doc2 = activeDocument;
try{
saveFileJpg (outputFolder +'/'+fName + layerName +'.jpg')
saveFilePng (outputFolder +'/'+fName + layerName +'.png')
}
catch(e){
alert(e)
}
doc2.close(SaveOptions.DONOTSAVECHANGES);
count++
}
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 );
}
function dupeFile(){
var idDplc = charIDToTypeID( "Dplc" );
var desc11 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
var idOrdn = charIDToTypeID( "Ordn" );
var idFrst = charIDToTypeID( "Frst" );
ref1.putEnumerated( idDcmn, idOrdn, idFrst );
desc11.putReference( idnull, ref1 );
var idMrgd = charIDToTypeID( "Mrgd" );
desc11.putBoolean( idMrgd, true );
executeAction( idDplc, desc11, DialogModes.NO );
}
function saveFilePng(saveName){
var idsave = charIDToTypeID( "save" );
var desc19 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc20 = new ActionDescriptor();
var idMthd = charIDToTypeID( "Mthd" );
var idPNGMethod = stringIDToTypeID( "PNGMethod" );
var idquick = stringIDToTypeID( "quick" );
desc20.putEnumerated( idMthd, idPNGMethod, idquick );
var idPGIT = charIDToTypeID( "PGIT" );
var idPGIT = charIDToTypeID( "PGIT" );
var idPGIN = charIDToTypeID( "PGIN" );
desc20.putEnumerated( idPGIT, idPGIT, idPGIN );
var idPNGf = charIDToTypeID( "PNGf" );
var idPNGf = charIDToTypeID( "PNGf" );
var idPGAd = charIDToTypeID( "PGAd" );
desc20.putEnumerated( idPNGf, idPNGf, idPGAd );
var idCmpr = charIDToTypeID( "Cmpr" );
desc20.putInteger( idCmpr, 6 );
var idPNGF = charIDToTypeID( "PNGF" );
desc19.putObject( idAs, idPNGF, desc20 );
var idIn = charIDToTypeID( "In " );
desc19.putPath( idIn, new File( saveName ) );
var idDocI = charIDToTypeID( "DocI" );
desc19.putInteger( idDocI, 219 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc19.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc19, DialogModes.NO );
}
function saveFileJpg(saveName){
var idsave = charIDToTypeID( "save" );
var desc10 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc11 = new ActionDescriptor();
var idEQlt = charIDToTypeID( "EQlt" );
desc11.putInteger( idEQlt, 12 );
var idMttC = charIDToTypeID( "MttC" );
var idMttC = charIDToTypeID( "MttC" );
var idNone = charIDToTypeID( "None" );
desc11.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc10.putObject( idAs, idJPEG, desc11 );
var idIn = charIDToTypeID( "In " );
desc10.putPath( idIn, new File( saveName ) );
var idDocI = charIDToTypeID( "DocI" );
desc10.putInteger( idDocI, 474 );
var idCpy = charIDToTypeID( "Cpy " );
desc10.putBoolean( idCpy, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc10.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc10, DialogModes.NO );
}
Copy link to clipboard
Copied
Hi Chuck,
This is basically perfect!
The last bit of confusion is that when I go to run the script the second time to apply the design to the bag it is for some reason moving the design down.
Here is a screenshot of how one of the designs looks in photoshop when it is opened after it was generated by the script. The bottom tip of the design is about 1/5 of the way up the canvas.
When I run the script the second time though to put it on the product image, the design is placed lower on the canvas (bottom tip is about 1/8 from the bottom of the canvas). I double checked and the size of the file coming in, and the file it is being applied to are both the same (1000 x 1000 pixels), so I'm not sure why it is being shifted down.
Also, this isn't important at all, but is there a way to reorder how the names are being combined? At the moment it is putting the name of what is being imported from the Source Folder first, and then the name of the active layer in Photoshop second. I can work with it either way, but in terms of what the assets represent the final SKU on the image name is now kind of backwards.
Thanks,
Matthew
Copy link to clipboard
Copied
In the lines that start with saveFileJpg and saveFilePng, switch the variables: fName and layer name around. As far as the alignment, is the main file the same ppi as the source files?
Copy link to clipboard
Copied
Hi Chuck,
I switched the order of those 2 variables, and that works prefectly thank you!
Also I checked the PPI. At first I thought you were right and that may have been the case, but now that I've made sure they are the same it is still happening.
It does appear to be moving it down by the same amount each time though, so I can always trial and error move the active layer up a bit before running the script to account for it.
Best,
Matthew
Copy link to clipboard
Copied
Thanks for sharing the script!
Copy link to clipboard
Copied
Your solution worked great for me.
Copy link to clipboard
Copied
I did a presentation with a set of phone case mockups on design bundles. It's much easier. This way, you can showcase your logos, branding, and more with their mockups. After all, they are made in high resolution. And smart objects can save a lot of time. Just click on the smart object a couple of times, add your graphics, and save the result. I took the iPhone 14 Pro Max case as an example. There are different colors and types here, so I grouped them by type and color in my presentation.