// select umages that have "left" and "right" in their names to replace smart objects callled "SO1" and "SO2" and save jpg-files; // 2017, use it at your own risk; #target photoshop if (app.documents.length > 0) { // select files; if ($.os.search(/windows/i) != -1) {var theDesigns = File.openDialog ("please select files", "*.psd;*.psb;*.tif;*.jpg", true)} else {var theDesigns = File.openDialog ("please select files", getFiles, true)}; if (theDesigns.length > 0) { // get left and right; var theLefts = new Array; var theRights = new Array; for (var d = 0; d < theDesigns.length; d++) { if (File(theDesigns).name.indexOf("right") != -1) { if (File(File(theDesigns).parent + "/" + File(theDesigns).name.replace("right", "left")).exists == true ) { theLefts.push(String(File(theDesigns).parent + "/" + File(theDesigns).name.replace("right", "left"))); theRights.push(theDesigns) } }; }; // if equal number of left and right; if (theLefts.length > 0 || theLefts.length == theRights.length) { replaceTwoSmartObject (app.activeDocument, theLefts, theRights); }; } }; ////// replace smart object content if only one smart object exists ////// function replaceTwoSmartObject (theFile, theLefts, theRights) { var myDocument = theFile; var theSmartObjects = collectSmartObjects2017(); var check1 = false; var check2 = false; for (var c = 0; c < theSmartObjects.length; c++) { if (theSmartObjects[0] == "SO1") { theLeftSO = theSmartObjects check1 = true }; if (theSmartObjects[0] == "SO2") { theRightSO = theSmartObjects; check2 = true }; }; // if the smart objects have been found; if (check1 == true || check2 == true) { var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1]; var thePath = myDocument.path; // psd options; psdOpts = new PhotoshopSaveOptions(); psdOpts.embedColorProfile = true; psdOpts.alphaChannels = true; psdOpts.layers = true; psdOpts.spotColors = true; // jpg options; var jpegOptions = new JPEGSaveOptions(); jpegOptions.quality = 9; jpegOptions.embedColorProfile = true; jpegOptions.matte = MatteType.NONE; // work through the array; for (var m = 0; m < theRights.length; m++) { // replace smart object; theLayer1 = replaceContentsByIdentifier (theLefts, theLeftSO); theLayer2 = replaceContentsByIdentifier (theRights, theRightSO); var theNewName = theRights.name.match(/(.*)\.[^\.]+$/)[1].replace("left", ""); //save jpg; myDocument.saveAs((new File(thePath+"/"+theName+"_"+theNewName+".jpg")),jpegOptions,true); } }; myDocument.close(); }; ////// get psds, tifs and jpgs from files ////// function getFiles (theFile) { if (theFile.name.match(/\.(psd|tif|psb|jpg)$/i) != null || theFile.constructor.name == "Folder") { return true }; }; ////// replace contents ////// function replaceContentsByIdentifier (newFile, theSO) { selectLayerByID (theSO[1], false); //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 }; ////// collect smart objects, probably based on code by paul, mike or x ////// function collectSmartObjects2017 () { // the file; var myDocument = app.activeDocument; // get number of layers; var ref = new ActionReference(); ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); var applicationDesc = executeActionGet(ref); var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers")); // process the layers; var theLayers = new Array; for (var m = 0; m <= theNumber; m++) { try { var ref = new ActionReference(); ref.putIndex( charIDToTypeID( "Lyr " ), m); var layerDesc = executeActionGet(ref); var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection"))); var isBackground = layerDesc.getBoolean(stringIDToTypeID("background")); // if not layer group collect values; if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) { var theName = layerDesc.getString(stringIDToTypeID('name')); var theID = layerDesc.getInteger(stringIDToTypeID('layerID')); if(layerDesc.hasKey(stringIDToTypeID('smartObject'))) {theLayers.push([theName, theID])} }; } catch (e) {}; }; return theLayers }; // based on code by mike hale, via paul riggott; function selectLayerByID(id,add){ add = undefined ? add = false:add var ref = new ActionReference(); ref.putIdentifier(charIDToTypeID("Lyr "), id); var desc = new ActionDescriptor(); desc.putReference(charIDToTypeID("null"), ref ); if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); desc.putBoolean( charIDToTypeID( "MkVs" ), false ); try{ executeAction(charIDToTypeID("slct"), desc, DialogModes.NO ); }catch(e){ alert(e.message); } }; |