|
// replace layers of a certain name with
// 2014, use it at your own risk;
#target "photoshop-70.032"
if (app.documents.length > 0) {
// get folder;
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i)};
var theseFiles = new Array;
for (var n = 0; n < theFiles.length; n++) {
theseFiles.push ([theFiles.name.match(/(.*)\.[^\.]+$/)[1], theFiles])
};
// of there are files proceed;
if (theseFiles.length > 0) {app.activeDocument.suspendHistory("replace", "main ()")};
};
////////////////////////////////////
function main () {
// the file;
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
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'));
theLayers.push([theName, theID])
/*if (theName == searchName) {
var boundsDesc = layerDesc.getObjectValue(stringIDToTypeID('bounds'));
var theTop = boundsDesc.getUnitDoubleValue(stringIDToTypeID('top'));
var theLeft = boundsDesc.getUnitDoubleValue(stringIDToTypeID('left'));
var theWidth = boundsDesc.getUnitDoubleValue(stringIDToTypeID('width'));
var theHeight = boundsDesc.getUnitDoubleValue(stringIDToTypeID('height'));
var theCenter = [theLeft+theWidth/2, theTop+theHeight/2];
selectLayerByIndex(m,false);
theArray.push([myDocument.activeLayer, theCenter, theWidth, theHeight])
};*/
};
}
catch (e) {};
};
////////////////////////////////////
for (var o = 0; o < theLayers.length; o++) {
var thisLayer = theLayers;
for (var p = 0; p < theseFiles.length; p++) {
if (theseFiles [0] == thisLayer[0]) {
selectLayerByID(thisLayer[1],false);
var removeLayer = myDocument.activeLayer;
var theBounds = removeLayer.bounds;
var theSO = placeScaleRotateFile (theseFiles [1], 0, 0, 100, 100, 0);
if (Number(theBounds[0]) == 0 && Number(theBounds[2]) == 0) {}
else {removeLayer.remove()};
}
}
};
// restore;
app.preferences.rulerUnits = originalUnits;
};
// by mike hale, via paul riggott;
// http://forums.adobe.com/message/1944754#1944754
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
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);
}
};
// 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);
}
};
////// 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;
};
|