my code stock.com

Scripts to enhance GM_FoldLayers

Snippet options

Download: Download snippet as gm_foldlayers-scripts.js.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

// GM_FoldLayers Scripts (▾/▸)
// Rob Frese 2016
// ------------------------------------------------------------------------
// Scripts to link to keyboard shortcuts to enhance use of GM_FoldLayers
// ------------------------------------------------------------------------
// Current Keyboard Shortcuts:
//		Jump To Next Group Divider 	=	Ctrl Shift +
//		Jump To Prev Group Divider 	=	Ctrl Shift -
// 		Open Close All Dividers		=	Ctrl Shift I
// 		Select All Dividers			=	Ctrl U
// 		Select Group Layers			=	Ctrl Shift U
// 		Create Group Divider		=	Ctrl Shift V
//		--------------------------------------------
//		Open / Close Selected* 		=	Ctrl Shift Backspace 	
//			* linked to "Open Layer Source in Panel" -- not a custom script
// ------------------------------------------------------------------------

// ------------------------------------
// Jump To Next Group Divider
// ------------------------------------

function jumpToNextGroupDivider(){// ▾  ▸
	
	// ------------------------------------------------------------------------
	// Jumps to the next group divider from current location, or selects first divider if none selected
	// ------------------------------------------------------------------------

	// ----------
	// returns
	// ----------
	
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	if (comp.numLayers === 0) return;
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup('Next Group Divider')
	
	// ----------
	// STARTING INDEX
	// either 1 or first selected layer's index
	// ----------
	
	var lrindex = 1; 
	var wasSelected = false;
	if (comp.selectedLayers.length > 0){
		// if layer selected is LAST LAYER, abort
		if (comp.selectedLayers[0].index == comp.numLayers){
			clearOutput()
			writeLn('Last Layer Selected')
			return;
		}
		lrindex = comp.selectedLayers[0].index+1;
		// deselect all layers
		for (var i=comp.selectedLayers.length-1; i>=0; i--){comp.selectedLayers[i].selected=false;}
		var wasSelected = true;
	}
	
	// ----------
	// loop thru layers starting with index
	// ----------
	
	for (var i=lrindex; i<=comp.numLayers; i++){
		var lr = comp.layer(i)
		if (!(lr instanceof ShapeLayer)) continue;
		if (lr.Contents.numProperties != 1) continue;
		if (lr.Contents.property(1).name == '||0100'
		|| lr.Contents.property(1).name == '||1100'){
			lr.selected = true;
			clearOutput()
			writeLn('Jumped to Next Group')
			if (lr.name[0] == '▸') writeLn(lr.index+'. >'+lr.name.substr(1,lr.name.length));
			else
			if (lr.name[0] == '▾') writeLn(lr.index+'. V'+lr.name.substr(1,lr.name.length));
			else writeLn(lr.index+'. '+lr.name);
			return;
			/// ▾ ▸
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////
	
	// ----------
	// if we made it to here and nothing is selected, that means we couldn't find a next group layer, 
	// so reselect first layer (if applicable) and alert in output
	// ----------
	
	if (wasSelected) comp.layer(lrindex-1).selected = true;
	
	clearOutput()
	writeLn('No Next Group Found')

	app.endUndoGroup()
}

// ------------------------------------
// Jump To Previous Group Divider
// ------------------------------------

function jumpToPrevGroupDivider(){// ▾  ▸

	// ------------------------------------------------------------------------
	// Jumps to the previous group divider from current location, or selects last divider if none selected
	// ------------------------------------------------------------------------

	// ----------
	// returns
	// ----------
	
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	if (comp.numLayers === 0) return;
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup('Prev Group Divider')
	
	// ----------
	// STARTING INDEX
	// either totla number of layers OR first selected layer's index
	// ----------
	
	var lrindex = comp.numLayers; 
	var wasSelected = false;
	if (comp.selectedLayers.length > 0){
		/// if layer selected is FIRST LAYER, aboft
		if (comp.selectedLayers[0].index == 0){
			clearOutput()
			writeLn('First Layer Selected')
			return;
		}
		lrindex = comp.selectedLayers[0].index-1;
		// deselect all layers
		for (var i=comp.selectedLayers.length-1; i>=0; i--){comp.selectedLayers[i].selected=false;}
		var wasSelected = true;
	}
	
	// ----------
	// loop thru layers BACKWARDS starting with index
	// ----------
	
	for (var i=lrindex; i>=1; i--){
		var lr = comp.layer(i)
		if (!(lr instanceof ShapeLayer)) continue;
		if (lr.Contents.numProperties != 1) continue;
		if (lr.Contents.property(1).name == '||0100'
		|| lr.Contents.property(1).name == '||1100'){
			lr.selected = true;
			clearOutput()
			writeLn('Jumped to Prev Group')
			if (lr.name[0] == '▸') writeLn(lr.index+'. >'+lr.name.substr(1,lr.name.length));
			else
			if (lr.name[0] == '▾') writeLn(lr.index+'. V'+lr.name.substr(1,lr.name.length));
			else writeLn(lr.index+'. '+lr.name);
			return;
			/// ▾ ▸
		}
	}
	
	////////////////////////////////////////////////////////////////////////////////////////////////
	
	// ----------
	// if we made it to here and nothing is selected, that means we couldn't find a group layer, 
	// so reselect first layer (if applicable) and alert in output
	// ----------
	
	if (wasSelected) comp.layer(lrindex+1).selected = true;
	
	clearOutput()
	writeLn('No Prev Group Found')

	app.endUndoGroup()
}

// ------------------------------------ 
// Open / Close All Dividers
// ------------------------------------ 

function openCloseAllGroupDividers(){ 
	
	// -------------------------------------------------------------------------------------------------------------------
	// Opens / closes all group dividers.
	// Finds the first group divider -- if it's open, close all. If it's closed, open all.
	// DO NOT CHANGE WHAT LAYERS ARE SELECTED -- only do this if we're closing AND the selected layer is going to be shy'd
	// -------------------------------------------------------------------------------------------------------------------
	
	// ----------
	// returns
	// ----------
	
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	if (comp.numLayers === 0) return;
	
	// ----------
	// find first group layer and decide if we're opening or closing
	// ----------
	
	var numlrs = comp.numLayers;
	var firstGroupLayer = null
	for (var i=1; i<=numlrs; i++){
		var lr = comp.layer(i)
		if (!(lr instanceof ShapeLayer)) continue;
		if (lr.Contents.numProperties != 1) continue;
		if (lr.Contents.property(1).name == '||0100'
		|| lr.Contents.property(1).name == '||1100'){
			firstGroupLayer = lr;
			break;			
		}		
	}
	if (firstGroupLayer == null) return;
	var closegroups = true;
	if (firstGroupLayer.Contents.property(1).name == '||0100') closegroups = false;
	
	var undost = 'Close'; if (closegroups == false) undost = 'Open'
	
	////////////////////////////////////////////////////////////////////////////////////////////////
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup(undost+' All Groups')
	
	// ---------- 
	// CLOSE ALL GROUPS
	// ---------- 
	
	if (closegroups == true) {
		var groupLayers = []; // this will be populated IN REVERSE -- so LAST in this array = first group layer
		// loop all layers -- isolate everything that isn't a group layer, change group layers
		for (var i=comp.numLayers; i>=firstGroupLayer.index; i--){
			var lr = comp.layer(i)
			lr.shy = true;
			if (!(lr instanceof ShapeLayer)) {lr.selected = false; continue;}
			if (lr.Contents.numProperties != 1) {lr.selected = false; continue;}
			if (lr.Contents.property(1).name == '||0100'
			|| lr.Contents.property(1).name == '||1100'){
				groupLayers.push(lr)
				lr.shy = false;
				if (lr.Contents.property(1).name == '||1100'){
					lr.Contents.property(1).name = '||0100'
					// change name
					if (lr.name[0] != '▸' && lr.name[0] != '▾') // does not have arrow
						lr.name = '▸ '+lr.name
					else // does have arrow
						lr.name = '▸'+lr.name.substr(1, lr.name.length)
				}
			}
		}

	}
	
	// ----------
	// OPEN ALL GROUPS
	// ----------
	
	if (closegroups == false) {
		var groupLayers = []; // this will be populated IN REVERSE -- so LAST in this array = first group layer
		// loop all layers -- open all groups
		for (var i=comp.numLayers; i>=firstGroupLayer.index; i--){
			var lr = comp.layer(i)
			lr.shy = false;
			if (!(lr instanceof ShapeLayer)) {continue;}
			if (lr.Contents.numProperties != 1) {continue;}
			if (lr.Contents.property(1).name == '||0100'
			|| lr.Contents.property(1).name == '||1100'){
				groupLayers.push(lr)
				lr.shy = false;
				if (lr.Contents.property(1).name == '||0100'){
					lr.Contents.property(1).name = '||1100'
					// change name
					if (lr.name[0] != '▸' && lr.name[0] != '▾') // does not have arrow
						lr.name = '▾ '+lr.name
					else // does have arrow
						lr.name = '▾'+lr.name.substr(1, lr.name.length)
				}
			}
		}

	}


	// ----------
	// end
	// ----------
	
	comp.hideShyLayers = true;
	app.endUndoGroup()

}

// ------------------------------------
// Select All Dividers
// ------------------------------------

function selectGroupDividers(){
	
	// ------------------------------------------------------------------------
	// Selects all group dividers
	// ------------------------------------------------------------------------
	
	// ----------
	// returns
	// ----------
	
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	if (comp.numLayers === 0) return;
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup('Select Group Dividers')
	
	if (comp.selectedLayers.length > 0){
		for (var i=comp.selectedLayers.length-1; i>=0; i--){comp.selectedLayers[i].selected=false;}
	}

	for (var i=comp.numLayers; i>=1; i--){
		var lr = comp.layer(i)
		if (!(lr instanceof ShapeLayer)) continue;
		if (lr.Contents.numProperties != 1) continue;
		if (lr.Contents.property(1).name == '||0100'
		|| lr.Contents.property(1).name == '||1100'){
			lr.selected = true;
		}
	}
	
	app.endUndoGroup()
}

// ------------------------------------
// Select Divider's Group Layers
// ------------------------------------

function selectGroupLayers(selectDivider){
	
	// ------------------------------------------------------------------------
	// Selects all of the "group layers" inside of all selected dividers
	// ------------------------------------------------------------------------
	// selectDivider	=	true/false
	//		true = select divider along with group contents
	//		false = do not select divider
	//		DEFAULT = FALSE
	// ------------------------------------------------------------------------
	
	if (selectDivider == undefined) selectDivider = false;
	
	// ----------
	// returns
	// ----------
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	if (comp.numLayers === 0) return;
	if (comp.selectedLayers.length === 0) return;
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup('Select Group\'s Layers')
	
	// ----------
	// first, get all "group layers" that are selected
	// ----------
	
	var grps = [];
	for (var i = comp.selectedLayers.length-1; i>=0; i--){
		var lr = comp.selectedLayers[i]
		if (!(lr instanceof ShapeLayer)) continue;
		if (lr.Contents.numProperties != 1) continue;
		if (lr.Contents.property(1).name == '||0100'
		|| lr.Contents.property(1).name == '||1100'){
			grps.push(lr)
			
			// ----------
			// select divider ?
			// ----------
			
			if (selectDivider == false) lr.selected = false
		}
	}

	// ----------
	// if no groups, abort
	// ----------
	
	if (grps.length == 0) return;
	
	// ----------
	// loop and select everything inside of these groups...
	// ----------
	
	for (var v=grps.length-1; v>=0; v--){
		var gl = grps[v]; //group layer
		var gind = gl.index;	// group layer index;
		/// loop from this group layer till the next layer
		for (var i=gind; i<=comp.numLayers; i++){
			var lr = comp.layer(i)
			/// found another group...
			if (lr == gl) continue;
			if (lr instanceof ShapeLayer && lr.Contents.numProperties == 1 && (lr.Contents.property(1).name == '||0100' || lr.Contents.property(1).name == '||1100')) break;
			lr.selected = true;
		}
	}
	
	// ----------
	// end
	// ----------

	app.endUndoGroup()
}

// ------------------------------------
// Create Group Divider
// ------------------------------------

function createGroupDivider(){
	
	// ------------------------------------------------------------------------
	// Creates a group divider and asks for the name
	// ------------------------------------------------------------------------
	
	// ----------
	// returns
	// ----------
	
	var comp = app.project.activeItem;
	if (comp == null) return;
	if (!(comp instanceof CompItem)) return;
	
	// ----------
	// begin
	// ----------
	
	app.beginUndoGroup('Create Group Divider')
	
	// ----------
	// ask name
	// ----------
	
	var askName = Window.prompt ('Group Divider Name', '', '')
	if (askName == null) return;

	var lrindex = null
	if (comp.selectedLayers.length > 0){
		lrindex = comp.selectedLayers[0].index
		if (lrindex == 0 || lrindex == 1) lrindex = null;
	}
	
	// ----------
	// create divider layer
	// ----------
	
	var div = comp.layers.addShape()
	div.label = 16;
	
		// ADD "----------" TO BEGINNING OF NAME
		div.name = '▾ -------------- ' + askName
	
		// SWITCHES TO MAKE LAYER STAND OUT
		div.enabled = false;
		div.motionBlur = true;
		div.adjustmentLayer = true;
		div.threeDLayer = true;
	
	// FINALIZE 
	var g = div.Contents.addProperty('ADBE Vector Group')
	g.name = '||1100'
	
	// move divider to location
	if (lrindex == null) return;
	
	var lr = comp.selectedLayers[0];
	if (lr.index != lrindex+1) lr.moveBefore(comp.layer(lrindex+1))

	app.endUndoGroup()
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.