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

Iterate Group's Layers, Selecting Next | help?

Explorer ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

Javascript/Photoshop Question:

How do you iterate through a Group/Layers creating a selection & next selection .

Im strugging with how to make "next selection." I can find make selection by layer name, but i dont need that. I need it to loop through layers so i can apply an action. Can anyone help?

 

Goal: Group / Group's Layers / Loop Group Layers / Make selection / Apply "Call to" photoshop action. 

 

 

function loopGroupLayersVisibility() {

var doc = app.activeDocument;

if (app.documents.length > 0) {
if (app.activeDocument.activeLayer.layers) {

} else {
alert("OOPS! \nLayer Group not Selected!!!");
}
}

var group = doc.activeLayer;
var groupLength = group.layers.length;

for (var i = 0; i < groupLength; i++) {
group.layers[i].visible = false;
}

for (i = 0; i < groupLength; i++) {


var layer = group.layers[i];
var layerIndex = i + 1;

layer.visible = true; // Instead of using visibility, make selection (and then next selection in loop)

 

// Ill add my CALL TO Photoshop Action. 

doAction("My Action","Default Actions");

layer.visible = false;


}


}

TOPICS
Actions and scripting

Views

728

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
Adobe
Community Expert ,
Aug 12, 2020 Aug 12, 2020

Copy link to clipboard

Copied

I just hack a scripting there is a lot I do not understand. But it looks to me like your action would need to know what layers to process. I would think your script would need to set the document activeLayer for the action to work on it.  I do not see the script setting the documents activeLayer.  I also do not know how an action would know what kind of layer is the visible layer in the Group to know how to process it correctly. I think you should do all the processing you want to do in your script.

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
Explorer ,
Aug 13, 2020 Aug 13, 2020

Copy link to clipboard

Copied

No worries.

 

Im just trying to figure out how to select each layer instead of turning on and off the visibility to each layer. 

 

I cannot find any references on how to do it within a group. 

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 ,
Aug 13, 2020 Aug 13, 2020

Copy link to clipboard

Copied

That is what your script FUNCTION last "for" loop seem to be doing.  But it does not check layer kind it just set  a layerIndex var that is never referenced. Set the layer visibility  on. Plays an action.  I have no Idea what the action may do.  There may be many visible layer in other layer groups in the document. The script does not set the document active layer to be the layer the script is processing.  The action has not way to access the layer the script is processing or know what type of layer is being processed. When the action completes the script turns off the layer visibility.  All in all, all the function seem to do to the layers in the layer group is turn their visibility off.

 

 

for (i = 0; i < groupLength; i++) {
	var layer = group.layers[i];
	var layerIndex = i + 1;
	layer.visible = true; // some comment
	// Ill add my CALL TO Photoshop Action. 
	doAction("My Action","Default Actions");
	layer.visible = false;
}

 

 

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
Explorer ,
Aug 14, 2020 Aug 14, 2020

Copy link to clipboard

Copied

I figured it out. 

For anyone who needs something similar, below is the code. Like most of you I have to do a lot of research to figure out how to do something. It's always nice for someone to share what they've learned. 

 

addAdjustments();

function addAdjustments() {
var doc = app.activeDocument;
var group = doc.activeLayer;
var groupLength = group.layers.length;

group = doc.layers.getByName("Your Group Name");

for (var i = 0; i < groupLength; i++) {
var selectLayer = group.layers[i];

doc.activeLayer = selectLayer;

// Do something below.
alert("Layer Number: " + [1 + i]);
app.doAction("Your Action", "Default Actions");
}
}

 

 

 

 

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 ,
Aug 14, 2020 Aug 14, 2020

Copy link to clipboard

Copied

Yes your action could now add an adjustment because you set the active layer to be the layer the script is processing.  Still the action does not know what kind a layer its adding an adjustment to.  Would the adjustment be appropriate?  All your action knows is to add an adjustment.    Your script does not even look at layer kind.   Lets say you action add an level adjustment layer.   If a layer in the selected group has three adjustment layers  above it and one is a levels adjustment do you want to add four more level adjustment layer above it?

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
Explorer ,
Aug 14, 2020 Aug 14, 2020

Copy link to clipboard

Copied

I understand. Note: I'm very new to Javascript. Like a couple months new lol. 

 

I simply wanted to learn how to loop through a Group's layer's and apply an action to each layer within the group. I do, however, have an IF statement. If layer kind a smart object, exicute. But I wanted to only post the easy version for those seeking something similiar. I have no doubts we i'll add more to it. 

 

You are helping me consider the things im missing from my code, though 🙂 Im sure it'll get better as i go, and ill learn more as i go. 

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 ,
Aug 14, 2020 Aug 14, 2020

Copy link to clipboard

Copied

LATEST

We all start new study this till you understand the code.  Pay attention to function processArtLayers This script is mainly a  Photoshop Script Main() template that Runs some code().  The code uses the function  to process some art layer types.

 

I feel if you going to script something you should want keep all processing in the script which  may include other scripts. Keep maintenance in a single place and make distribution easy olly scripts need to be installed. Do not pay Action in your scripts.  An exception would be you are creating a script like the image processors where users actions can add additional processing to your scripted process.

 

/* ==========================================================
// 2017  John J. McAssey (JJMack) 
// ======================================================= */
// This script is supplied as is. It is provided as freeware. 
// The author accepts no liability for any problems arising from its use.
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
// ensure at least one document open
if (!documents.length) alert('There are no documents open.', 'No Document');
else {
	// declare Global variables
	var allLayers = ""; 
	//main(); // at least one document exists proceed
    app.activeDocument.suspendHistory('Some Process Name','main()');
}
///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main() {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	var orig_type_units = app.preferences.typeUnits;
	var orig_display_dialogs = app.displayDialogs;
	app.preferences.rulerUnits = Units.PIXELS;  	// Set the ruler units to PIXELS
	app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS
	app.displayDialogs = DialogModes.NO;	    	// Set Dialogs off
	try { code(); }
	// display error message if something goes wrong
	catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
	alert(allLayers);
	app.displayDialogs = orig_display_dialogs;  	// Reset display dialogs 
	app.preferences.typeUnits  = orig_type_units;	// Reset ruler units to original settings 
	app.preferences.rulerUnits = orig_ruler_units;	// Reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// The real code is embedded into this function so that at any point it can return //
// to the main line function to let it restore users edit environment and end      //
/////////////////////////////////////////////////////////////////////////////////////
function code() {
	processArtLayers(activeDocument) 
}
function processArtLayers(obj) {  
    for( var i = obj.artLayers.length-1; 0 <= i; i--) {/* alert(obj.artLayers[i]); */processLayers(obj.artLayers[i])}  
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {/* alert(obj.layerSets[i]); */processArtLayers(obj.layerSets[i])} // Process Layer Set Layers  
} 
function processLayers(layer) { 
	//alert('Layer Name = "' + layer.name + '" Layer Kind ' + layer.kind );
	allLayers = allLayers + "ID " + layer.id + ", " + layer.name + ", " + layer.kind + "\n";
	switch (layer.kind){
	case LayerKind.SMARTOBJECT : processSmartObj(layer); break;
	default : break; // none process layer types catch all
	}
} 
//////////////////////////////////////////////////////////////////////////////////
//			Layer Type Functions					//
//////////////////////////////////////////////////////////////////////////////////
function processSmartObj(obj) { 
//	alert('Smart Object = "' + obj.name + '"');
	var localFilePath = "";
	var ref = new ActionReference();    
    ref.putIdentifier(charIDToTypeID('Lyr '), obj.id );    
    var desc = executeActionGet(ref);  
	//alert(actionDescriptor(desc));
    var smObj = desc.getObjectValue(stringIDToTypeID('smartObject'));  
	//alert(actionDescriptor(smObj));	
	try {
		//alert(obj_to_str(smObj), "Object Properties"); 
		//var thePlace = smObj.getEnumerationType(stringIDToTypeID('place'));
		var theDocumentID = smObj.getString(stringIDToTypeID('documentID'));
		//var theCompsList = smObj.getObjectPropertyType(stringIDToTypeID('compsList'));
		var theLinked = smObj.getBoolean(stringIDToTypeID("linked"));
		var theFileReference = smObj.getString(stringIDToTypeID("fileReference"));
		alert(["Smart Object Layer " + obj.name, "\n" + theDocumentID, "\nLinked=" + theLinked, "File='" + theFileReference + "'" ]);
	}
	catch(e) {alert(e);}
	return localFilePath;
}

// Thanks to  r-bin
function obj_to_str(obj){var str = ""; for (var p in obj) if(obj.hasOwnProperty(p))try{str+=p+"::"+obj[p]+"\n";}catch(e){};return str;}  

// Thanks to SuperMerlin

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function actionDescriptor(desc){  
	if(desc.typename == 'ActionDescriptor'){  
		var c = desc.count;  
		var msg= "Action Descriptor Item Count = " + c;
		for(var i=0;i<c;i++){ //enumerate descriptor's keys  
			//$.writeln('AD '+zeroPad( i+1, 2 )+' = '+IDTz(desc.getKey(i)) +' : '+desc.getType(desc.getKey(i)));   
			msg =  msg + "\n" + 'AD '+zeroPad( i+1, 2 )+' = '+IDTz(desc.getKey(i)) +' : '+desc.getType(desc.getKey(i)) ; 
		} 
    return msg; 
	}  
	function IDTz(id){  
		try {  
			var res = typeIDToStringID( id );  
			if(res == '' ){  
				var res = typeIDToCharID( id );  
			}  
		}
		catch(e){}  
		return res;  
	}  
	function zTID( s ){  
		if( s.length == 4 ) var res = charIDToTypeID( s );  
		if( s.length > 4 ) var res = stringIDToTypeID( s );  
		return res;  
	}  
	function zeroPad(num,pad) {  
		var z = Math.pow(10,Number(pad))  
		return num <= z ? ((Number( num) + z).toString().substr(1)): num  
	}  
};  

 

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