Script to select all files in a given folder and save with each file name separately
Hello,
Since the past four days I was trying to build some script that can help me automate 700+ files that I want to create mockups for. The mockup file I have requires to open the smart object, therefore simple "replace smart object" macros failed. Using multiple resources I came up with below macro - it works flawlessly, but for one file only. I would need to specify a folder to pick up the file to be placed embeded, and then save it under the filename of each file used for placing. I tried adding this script modified to my script, but I kept gettings errors: https://community.adobe.com/t5/framemaker-discussions/loop-through-all-mif-files-in-a-given-folder/m-p/10581749#M62305
Here is my script:
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// JPG Options;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 8;
//path to the file
var filePath = new File('D:/Creative/Circuits/To print/50x70/Border/Light/50x70_Barcelona_FullColor_Light_Light (Chicane)_border.png');
//name of the saved file
var theNewName = filePath.name.match(/(.*)\.[^\.]+$/)[1];
function placeEmbeded(filePath) {
var actDesc = new ActionDescriptor();
actDesc.putPath(charIDToTypeID('null'), filePath);
actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));
executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);
};
////// open smart object //////
openSmartObject (app.activeDocument.activeLayer);
function openSmartObject (theLayer) {
current = app.activeDocument;
if (theLayer.kind == "LayerKind.SMARTOBJECT") runMenuItem(stringIDToTypeID('placedLayerEditContents'));
//place the file
placeEmbeded(filePath);
if ( current == app.activeDocument) {
//alert("Did mot open all the way");
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
}
catch (e) { throw theLayer + " Smart Object Did not Open"; }
if ($.os.search(/windows/i) != -1) var workFile = new File( "~/AppData/Local/Temp/" + name); // Windows user ID system temp space
else var workFile = new File( "~/AppData/Local/Temp/" + name); // should be in Mac users temp space I would think
if (workFile.exists)a pp.open(File(workFile));
if ( current == app.activeDocument) {
throw theLayer + " Smart Object Did not Open";
}
}
return app.activeDocument;
};
//save and close the smartobject
app.activeDocument.save();
app.activeDocument.close();
//save the file
myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);