• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Do action For selected layer

Enthusiast ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

hello genius
I want the Do Action code on the selected layers
In other words, I have selected layers and I want to run a static action on these selected layers
- I found codes that make a drop-down list of the actions and I choose the action .. but I want to put a fixed action to do the required every time
I hope I have succeeded in conveying the idea

TOPICS
Actions and scripting , SDK

Views

695

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 19, 2021 Dec 19, 2021

Well guys, I did a search and found a code and I just took what I wanted from it
And it did what was required already.. I can customize any action and execute it on many specific layers
Thank you for your interest

 

app.bringToFront();
var selectedLayers= getSelectedLayersIdx();

var actionSets = new Array();
actionArray=[];

    var Ai=("Action Name Here"); var As=("Action Set Here");
if(actionArray.length ==0) actionArray.push([[Ai],[As]]);

    selectedLayers= getSelectedLayersIdx();

for(var a 
...

Votes

Translate

Translate
Adobe
Community Expert ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

There was  scripts posted in this forum to do an action one selected layers.  I believe the script had a dialog or prompt where you  selected the Action set and action or that you targeted the Action int the Action Palette  then targeted the layers in the document and ran the script.  I do not remember the exact interface. Do a search.  If you search before asking you are likely  to get what you are looking for faster.

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

Actually I searched and found a code but it puts all the actions in a dropdown list..
But I want to apply a static action to all the selected layers
In general, I am looking into it and I will find a solution soon
Thank you for caring

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

Well guys, I did a search and found a code and I just took what I wanted from it
And it did what was required already.. I can customize any action and execute it on many specific layers
Thank you for your interest

 

app.bringToFront();
var selectedLayers= getSelectedLayersIdx();

var actionSets = new Array();
actionArray=[];

    var Ai=("Action Name Here"); var As=("Action Set Here");
if(actionArray.length ==0) actionArray.push([[Ai],[As]]);

    selectedLayers= getSelectedLayersIdx();

for(var a in selectedLayers){
    makeActiveByIndex(Number(selectedLayers[a]));
     if(app.activeDocument.activeLayer.kind != LayerKind.NORMAL) continue;
     for(var z in actionArray){
         try{
         doAction(actionArray[z][0].toString(), actionArray[z][1].toString());	
         }catch(e){alert(e+" - "+e.line);}
         }
    }

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; 
 };
function makeActiveByIndex( idx, visible,add ){ 
    if(visible == undefined) visible = false;
    if(add == undefined) add=false;
    var desc = new ActionDescriptor(); 
      var ref = new ActionReference(); 
      ref.putIndex(charIDToTypeID( "Lyr " ), idx) 
      desc.putReference( charIDToTypeID( "null" ), ref ); 
      if(add) desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
      desc.putBoolean( charIDToTypeID( "MkVs" ), visible ); 
   executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO ); 
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

Thx for sharing and please link us to the thread containing a code you found useful...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

Here is a script you can use without any dialog or prompt.  Target the layers in the document and target the action in the action Palette then run the script. After running the script you can target an other action and run the script agin to play other actions on the layers the actions though must not delete a layer they are played on.

 

 

// JJMack
#target photoshop
var s2t = stringIDToTypeID;
if (app.documents.length > 0) main();

function main() {
	try {
		var selectedLayerID = new Array;
		selectedLayerID = get_selected_layers_id();
		if (selectedLayerID=="") {alert("No Selected Layers"); return;}
		selectedAtn =  getSelecteAction();
		if (selectedAtn=="") {alert("No Action Selected"); return;}
		for (var i = 0; i < selectedLayerID.length; i++) {
			selectLayerByID(selectedLayerID[i])
			playAction(selectedAtn);
		}
		add = false;
		for (var i = 0; i < selectedLayerID.length; i++) {
			selectLayerByID(selectedLayerID[i],add)
			add=true;
		}		
	}
	catch(e) { alert(e + ': on line ' + e.line, 'Photoshop Error', true); }  
}

function get_selected_layers_id() {  
	try {  
		var r = new ActionReference();      
		r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));      
		r.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));  
		var d = executeActionGet(r);  
		if (!d.hasKey(stringIDToTypeID("targetLayers"))) {  
			var r = new ActionReference();  
			r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("layerID"));      
			r.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));  
			return [ executeActionGet(r).getInteger(stringIDToTypeID("layerID")) ];  
        }  
		var list = d.getList(stringIDToTypeID("targetLayers"));  
		if (!list) return null;  
		var n = 0;  
		try { activeDocument.backgroundLayer } catch (e) { n = 1; }  
		var len = list.count;  
		var selected_layers = new Array();  
		for (var i = 0; i < len; i++) {  
			try {  
				var r = new ActionReference();  
				r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("layerID"));      
				r.putIndex( charIDToTypeID("Lyr "), list.getReference(i).getIndex() + n);  
				selected_layers.push(executeActionGet(r).getInteger(stringIDToTypeID("layerID")));  
			}  
			catch (e) { _alert(e); return null; }  
		}  
		return selected_layers;  
	}  
	catch (e) { alert(e); return null; }  
}   

function get_layer_by_id(id, doc_id) {    
	try {    
		var doc;  
		if (doc_id == undefined) doc = activeDocument;  
		else { 
			for (var i = 0; i < documents.length; i++) {  
				if (documents[i].id == doc_id) {  
					doc = documents[i];  
					break;  
                }  
            }
		}
		if (doc == undefined) { alert("Bad document " + doc_id); return null; }  
		var r = new ActionReference();      
		r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("json"));   
		if (doc_id == undefined) r.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));  
		else                     r.putIdentifier(charIDToTypeID("Dcmn"), doc_id);  
		eval("var json = " + executeActionGet(r).getString(stringIDToTypeID("json")));    
		if (json == undefined) return null;    
		var set = new Array();  
		function search_id(layers, id) {    
			for (var i = 0; i < layers.length; i++) {  
				if (layers[i].id == id) { set.push(i); return true; }  
            }  
			for (var i = 0; i < layers.length; i++) {  
				if (layers[i].layers) {    
					if (search_id(layers[i].layers, id)) { set.push(i); return true; }   
				}  
			}  
		}     
		if (search_id(json.layers, id)) {  
			var ret = doc.layers;  
			for (var i = set.length-1; i > 0; i--) { ret = ret[set[i]].layers;}  
			return ret[set[0]];  
		}  
		return null;  
	}    
	catch (e) { alert(e); }    
}

// 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); }
};

function getSelecteAction() {
	r = new ActionReference();
	r.putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
	try {
		var atn = executeActionGet(r).getInteger(s2t('itemIndex')),
		set = executeActionGet(r).getInteger(s2t('parentIndex'));
	} 
	catch(e) { return ""; }
	
	r = new ActionReference();
	r.putIndex(s2t('action'), atn);
	r.putIndex(s2t('actionSet'), set);
	d = new ActionDescriptor();
	d.putReference(s2t('target'), r);
	return d;
}
	
	
function playAction(atn) {	
	try { executeAction(s2t('play'), atn) } catch (e) { alert(e) }
}

 

 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2021 Dec 19, 2021

Copy link to clipboard

Copied

LATEST

Of course the Script Jazz-y posted is faster but does not reselect the targeted layers.  It Pays to search. I'm a hacker it works for me.  If I can not find something like I need  Jazz-y, R-bin, Super Merlin,  Kukurykus, c.pfaffenbichler, departed Paul Riggottl, and the ghost of Mike Hale will bail me out .

 

//Jazz-y

#target photoshop
var s2t = stringIDToTypeID;


(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p);

(r = new ActionReference()).putEnumerated(s2t('action'), s2t('ordinal'), s2t('targetEnum'));
try {
    try {
        var atn = executeActionGet(r).getString(s2t('name')),
            set = executeActionGet(r).getString(s2t('parentName'));
    }
    catch (e) { throw 'Before start select any action from actions palette!' }

    for (var i = 0; i < lrs.count; i++) {
        (r = new ActionReference()).putIdentifier(s2t('layer'), lrs.getReference(i).getIdentifier(s2t('layerID')));
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('select'), d, DialogModes.NO); } catch (e) { throw e + '\nCannot select layer!' }
        (r = new ActionReference()).putName(s2t('action'), atn);
        r.putName(s2t('actionSet'), set);
        (d = new ActionDescriptor()).putReference(s2t('target'), r);
        try { executeAction(s2t('play'), d) } catch (e) { throw e + '\nCannot play action "' + atn + '" from set "' + set + '"' }
    }
} catch (e) { alert(e) }

 

 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines