Skip to content

Instantly share code, notes, and snippets.

@chriscooning
Last active October 26, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriscooning/1c4c9e5a38f34b80903633617113f2dc to your computer and use it in GitHub Desktop.
Save chriscooning/1c4c9e5a38f34b80903633617113f2dc to your computer and use it in GitHub Desktop.
[photoshop] Apply action to each layer in file
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
myDocument.suspendHistory("operation", "main(myDocument)");
};
////////////////////////////////////
function main () {
var theLayers = getSelectedLayersIdx();
// do stuff;
// reselect layers;
for (var p = 0; p < theLayers.length; p++) {
selectLayerByIndex(theLayers[p], false);
app.doAction('Scale layer to select', 'SHOES MODIF TAILLE');// ici defi l'action pour les layers selectiones
};
};
//
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);
}
};
////// by paul mr;
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++){
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
try{
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
}catch(e){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
}
return selectedLayers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment