Known Participant
September 28, 2019
Answered
script for phone case mockup
- September 28, 2019
- 10 replies
- 11557 views
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 whatever folders you want to use. I also added a "-Flat" suffix to the saved jpg files to avoid overwriting anything.
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;
for (var i=0;i<fileList.length;i++){
replaceSO (fileList[i]);
var fName = fileList[i].name.split('.')[0];
dupeFile ();
var doc2 = activeDocument;
doc2.saveAs(new File(outputFolder +'/'+fName + '-flat.jpg'),jpgOptions);
doc2.close(SaveOptions.DONOTSAVECHANGES);
}
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 );
}Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.