// SwatchLabels.jsx /* This JavaScript is intented to be used with InDesign CS3. Running it creates textframes containing the colo values of the the selected rectangles(with applied swatches. You must select at least one rectangle with a color swatch applied to it to run the script. The textframes the script creates will be created on a new layer, with created paragraph styles applied to them. */ var myDocument = app.documents.item(0); //Save current mesurement units, and change them to points var myOldXUnits = app.documents.item(0).viewPreferences.horizontalMeasurementUnits; var myOldYUnits = app.documents.item(0).viewPreferences.verticalMeasurementUnits; app.documents.item(0).viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; app.documents.item(0).viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; //Check what is selected if (app.selection.length == 0) // If nothing is selected { alert("Select some rectangles and try again."); } else // Else something is selected. { 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");} var mySelections = app.selection; //Build array of rectangles myRecs = new Array(); for(i = 0; i < mySelections.length; i++) { if(mySelections[i] == "[object Rectangle]") //Check to make sure selection only includes rectangles { myRecs.push(mySelections[i]); } else{alert("Objects other than graphics were selected!"); } //If it is not a rectangle, don't add it to the array } //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; } else //Else it is not in CMYK so insert generic label { myColorValue = myRecSwatch.colorValue myTextFrame.contents = "Non-CMYK: "; for (j = 0; j < myColorValue.length; j ++) { myTextFrame.contents = myTextFrame.contents + (Math.round(myColorValue[j]) + " "); } myTextFrame.contents = myTextFrame.contents + "\r" + myTextFrame.contents; myTextFrame.paragraphs.item(0).appliedParagraphStyle = myLabelStyleWhite.name; myTextFrame.paragraphs.item(1).appliedParagraphStyle = myLabelStyleBlack.name; } }//end of working with color or tint swatches / end of try catch(e) //Process gradient swatches { 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){} try { myLabelCharStyleGr = myDocument.characterStyles.add({name:"swatch_labels_Gr"});//Create new character style for gradients myLabelWhiteSwatch = myDocument.swatches.itemByName("Paper"); myLabelCharStyleGr.fillColor = myLabelWhiteSwatch; } catch(e){myLabelCharStyleGr = myDocument.characterStyles.itemByName("swatch_labels_Gr");} try { myLabelStyleGr = myDocument.paragraphStyles.add({name:"swatch_labels_Gr"});//Create new paragraph style for gradients myLabelBlackSwatch = myDocument.swatches.itemByName("Black"); myLabelStyleGr.basedOn = myLabelStyleWhite; myLabelStyleGr.fillColor = myLabelBlackSwatch; myNestedStyle = myLabelStyleGr.nestedStyles.add(); myNestedStyle.appliedCharacterStyle = myLabelCharStyleGr; myNestedStyle.repetition = 5; } catch(e){myLabelStyleGr = myDocument.paragraphStyles.itemByName("swatch_labels_Gr");} //Add label to the textframe fro all stops of the gradient try { if(myCMYKCheck == true) { for (j = 0; j < myStops.length; j++) { myStopColor = myStops[j].stopColor; myColorValue = myStopColor.colorValue; myTextFrame.contents = myTextFrame.contents + (j +1) +": "+ (Math.round(myColorValue[0]) + "c " + Math.round(myColorValue[1]) + "m " + Math.round(myColorValue[2]) + "y " + Math.round(myColorValue[3]) + "k / " + (j +1) +": " + Math.round(myColorValue[0]) + "c " + Math.round(myColorValue[1]) + "m " + Math.round(myColorValue[2]) + "y " + Math.round(myColorValue[3]) + "k\r"); myTextFrame.paragraphs.item(j).appliedParagraphStyle = myLabelStyleGr.name; //Apply paragraph style to label } } else //else if the gradient contains non-CMYK stops { myTextFrame.contents = "Non-CMYK\r"; for (j = 0; j < myStops.length; j++) { myStopColor = myStops[j].stopColor; myColorValue = myStopColor.colorValue; myTextFrame.contents = myTextFrame.contents + ((j+1) + ": "); for (k = 0; k < myColorValue.length; k ++) { myTextFrame.contents = myTextFrame.contents + (Math.round(myColorValue[k]) + " "); } myTextFrame.contents = myTextFrame.contents + "/ "; for (k = 0; k < myColorValue.length; k ++) { myTextFrame.contents = myTextFrame.contents + (Math.round(myColorValue[k]) + " "); } myTextFrame.contents = myTextFrame.contents + "\r"; } } } catch(e){} }//end of processing gradients } //end of for all rectanges } //end of else we have a selection //Reset measurement units app.documents.item(0).viewPreferences.horizontalMeasurementUnits = myOldXUnits; app.documents.item(0).viewPreferences.verticalMeasurementUnits = myOldYUnits; //End of SwatchLables.jsx