This website contains various projects I have worked on. Currently, this includes JavaScripts for Adobe applications and general production tutorials using Adobe Creative Suite applications. All projects can be accessed from the right-hand menu.

Adobe Scripts List
Creating labels for Swatches in InDesign

Script Description

This InDesign script will create labels for all selected rectangles with swatches applied to them. The labels contain the CMYK color values of the the applied swatch. The idea of this script is that it can be used to create a page of all swatches used in your document, with labels of the values of swatches used on top of each rectangle.

before and after labeling the rectangles

Looking at the Script

The script starts out by creating a new layer that will be used for the textframes that will hold the swatch labels. The layer is created so you can quickly delete all the labels once you are done with them. Next, it create paragraph styles for the white and black text that will be the label.

try
	{
    	myLabelLayer = myDocument.layers.add({name:"swatch_labels"}); //Create new layer for textframe labels
		}
		catch(e){myLabelLayer = myDocument.layers.itemByName("swatch_labels");}
		
		try
			{	
				myLabelStyleWhite = myDocument.paragraphStyles.add({name:"swatch_labels_W"}); //Create new paragraph style for labels(white)
				myLabelWhiteSwatch = myDocument.swatches.itemByName("Paper");
				myLabelStyleWhite.fillColor = myLabelWhiteSwatch;
				myLabelStyleWhite.leftIndent = 4.8;
				}
				catch(e){myLabelStyleWhite = myDocument.paragraphStyles.itemByName("swatch_labels_W");}

			try
				{
					myLabelStyleBlack = myDocument.paragraphStyles.add({name:"swatch_labels_K"});//Create new paragraph style for labels(black)	
					myLabelStyleBlack.basedOn = myLabelStyleWhite;		
					myLabelBlackSwatch = myDocument.swatches.itemByName("Black");
					myLabelStyleBlack.fillColor = myLabelBlackSwatch;
					}
			catch(e){myLabelStyleBlack = myDocument.paragraphStyles.itemByName("swatch_labels_K");}
      

Next the script will create new textframes for each rectangle selected. The size of the new textframe is equal to the size of the rectangle it will be a lable of. Color values are caputred for each rectangle and it is determined if they are CMYK values or not. If they are CMYK they are written out as such, if not they are listed as non-CMYK.

//For all the rectangles in the array, create a textframe that can hold the label if it has a CMYK color or gradient swatch
for (i = 0; i < myRecs.length; i++)
	{
		myRecSwatch = myRecs[i].fillColor;
					
		try
			{
				myRecColorSpace = myRecSwatch.space; //if the swatch is a color or tint
											
				//Determine if a tint of the color is being used
                myRecTint =myRecs[i].fillTint;
                if(myRecTint > 0)
                    {myRecTint = "@ " + myRecTint + "%";}
                    else{myRecTint = "";}
            
                //Create the texframe, make it the same size as the rectangle
                myX1 = myRecs[i].geometricBounds[1]; 
                myY1 = myRecs[i].geometricBounds[2]; 
                myX2 = myRecs[i].geometricBounds[3]; 
                myY2 = myRecs[i].geometricBounds[0]; 										
				var myTextFrame = myRecs[i].parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2]}); 
						
                if (myRecColorSpace == "1129142603") //if the color space is CMYK
                    {
                        myColorValue = myRecSwatch.colorValue //The CMYK values		
                    
                        //Insert label into the new textframe, apply styles
                        myTextFrame.contents = (Math.round(myColorValue[0]) + "c " + Math.round(myColorValue[1]) + "m " + Math.round(myColorValue[2]) + "y " + Math.round(myColorValue[3]) + "k " + myRecTint + "\r" + Math.round(myColorValue[0]) + "c " + Math.round(myColorValue[1]) + "m " + Math.round(myColorValue[2]) + "y " + Math.round(myColorValue[3]) + "k " + myRecTint);
                        myTextFrame.paragraphs.item(0).appliedParagraphStyle = myLabelStyleWhite.name;
                        myTextFrame.paragraphs.item(1).appliedParagraphStyle = myLabelStyleBlack.name;
                        }
        

The script will also label tint and gradient swatches. For tints, it will list the percentage of the color used, for swatches it will count all the stops used in the gradient swatch and for each stop it will write out the color values. If your gradient contains manys stops you will end up with a lot of text within the new textframe.

try
    {
        myStops = myRecSwatch.gradientStops;
        myCMYKCheck = true;		
            
        //Check to see if a colorspace other than CMYK is in use				
        for (j = 0; j < myStops.length; j++)
            {								
                myStopColor = myStops[j].stopColor;
                myStopColorSpace = myStopColor.space;
                if(myStopColorSpace != "1129142603")
                    {myCMYKCheck = false;}							
                }
            
            //Create the textframe for the label
            myX1 = myRecs[i].geometricBounds[1]; 
            myY1 = myRecs[i].geometricBounds[2]; 
            myX2 = myRecs[i].geometricBounds[3]; 
            myY2 = myRecs[i].geometricBounds[0]; 										
            var myTextFrame = myRecs[i].parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2]}); 
            }
    catch(e){}

back to top

Image of a notepad

Table-of-Contents

Downloads


Other Projects:

Adobe JavaScripts ↑↓

Adobe Creative Suite Tutorials ↑↓