Numbered Adjustment Layers

What type of scripts do you need?

Moderator: byronnash

Post Reply
scribling
Posts: 143
Joined: May 1st, 2005, 1:52 pm

I use this script to create adjustment layers.

Code: Select all

{
   var activeItem = app.project.activeItem;
   if (activeItem == null || !(activeItem instanceof CompItem)){
       alert("Please establish a comp as the active item and run the script again");
   } else {

      app.beginUndoGroup("Levels Adjust");

      var activeComp = activeItem;

      var solidName = "Adjustment Layer ";
      var solidW = activeComp.width;
      var solidH = activeComp.height;
      var solidPixelAspectRatio = activeComp.pixelAspect;
      var solidDuration = activeComp.duration;

      var adjLayer = activeComp.layers.addSolid([1, 1, 1], solidName, solidW, solidH, solidPixelAspectRatio, solidDuration); 

      adjLayer.adjustmentLayer = true;
      adjLayer.guideLayer = false;
      adjLayer.moveToBeginning();

      app.endUndoGroup();
   }
} 
The problem is that every layer created with it is named "Adjustment Layer," instead of Adjustment Layer 1,2,3 ... as when manually choosing New -> Adjustment Layer.
Is there a way to have the script discover the name and numbers of the existing adjustment layers and name the new one in succession?

Thanks
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Here are a couple of methods. There are probably more efficient ways to do it.

This one will name a new adjustment layer 1 higher than the current highest:

Code: Select all

{
	var activeItem = app.project.activeItem;
	if (activeItem == null || !(activeItem instanceof CompItem)){
		alert("Please establish a comp as the active item and run the script again");
	} else {

		var layerNumber = 0;
		var layerName;
		var testNumber;

		for (var x = 1; x <= activeItem.numLayers; x++) {
			layerName = activeItem.layer(x).name;
			if (layerName.indexOf("Adjustment Layer ") != -1) {
				testNumber = parseInt(layerName.substring(layerName.lastIndexOf(" "), layerName.length));
				if (!isNaN(testNumber)) {
					layerNumber = Math.max(layerNumber, testNumber);
				}
			}
		}

		app.beginUndoGroup("Levels Adjust");

		var activeComp = activeItem;

		var solidName = "Adjustment Layer " + (layerNumber + 1);
		var solidW = activeComp.width;
		var solidH = activeComp.height;
		var solidPixelAspectRatio = activeComp.pixelAspect;
		var solidDuration = activeComp.duration;

		var adjLayer = activeComp.layers.addSolid([1, 1, 1], solidName, solidW, solidH, solidPixelAspectRatio, solidDuration); 

		adjLayer.adjustmentLayer = true;
		adjLayer.guideLayer = false;
		adjLayer.moveToBeginning();

		app.endUndoGroup();
	}
} 
This one will fill in earlier numbers if they don't already exist (i.e. if you have a 2 but not 1 it will create a 1):

Code: Select all

{
	var activeItem = app.project.activeItem;
	if (activeItem == null || !(activeItem instanceof CompItem)){
		alert("Please establish a comp as the active item and run the script again");
	} else {

		var layerNumber = 0;
		var layerExists = true;
		
		while (layerExists) {
			layerExists = false;
			layerNumber ++;
			for (var x = 1; x <= activeItem.numLayers; x++) {
				if (activeItem.layer(x).name == "Adjustment Layer " + layerNumber) {
					layerExists = true;
					break;
				}
			}
		}

		app.beginUndoGroup("Levels Adjust");

		var activeComp = activeItem;

		var solidName = "Adjustment Layer " + layerNumber;
		var solidW = activeComp.width;
		var solidH = activeComp.height;
		var solidPixelAspectRatio = activeComp.pixelAspect;
		var solidDuration = activeComp.duration;

		var adjLayer = activeComp.layers.addSolid([1, 1, 1], solidName, solidW, solidH, solidPixelAspectRatio, solidDuration); 

		adjLayer.adjustmentLayer = true;
		adjLayer.guideLayer = false;
		adjLayer.moveToBeginning();

		app.endUndoGroup();
	}
} 
Post Reply