//LabelGraphics.jsx (Modified)
//An InDesign CS3 JavaScript
/*
@@@BUILDINFO@@@ "LabelGraphics.jsx" 1.1.0 6-June-2008
*/
//Adds labels to the graphics in the active document.
/* This sample script has been modified. The modications are listed here: option added to create a label from clipboard data,
dropdown menu added to apply a swatch to the caption frame. This script can now apply lables to just the selected graphics
if you have one or more images selected. Otherwise it will apply the lables to all graphics in the document. The hieght of the
caption frame is determined by the amount of text, and paragraph style applied. the width of the caption frame is
determined by the width of the graphic it is under. Setting the caption height in the dialog box has been removed, as it is no
longer needed.
*/
main();
//=============================================================\\
function main(){
if(app.documents.length != 0){
if(app.documents.item(0).allGraphics.length != 0){
myDisplayDialog();
}
else{
alert("Document contains no graphics.");
}
}
else{
alert("Please open a document and try again.");
}
}
//=============================================================\\
function myDisplayDialog(){
var myLabelWidth = 100;
var myStyleNames = myGetParagraphStyleNames();
var mySwatchNames = myGetSwatchNames();
var myDialog = app.dialogs.add({name:"LabelGraphics"});
with(myDialog.dialogColumns.add()){
//Label type
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Label Type", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myLabelTypeDropdown = dropdowns.add({stringList:["File name", "File path", "XMP description", "XMP author","Paste from clipboard"], selectedIndex:4});
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Label Offset", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myLabelOffsetField = measurementEditboxes.add({editValue:0});
}
}
//Style to apply
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Label Style", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myLabelStyleDropdown = dropdowns.add({stringList:myStyleNames, selectedIndex:0});
}
}
//Swatch to apply
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Apply swatch", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var mySwatchDropdown = dropdowns.add({stringList:mySwatchNames, selectedIndex:0});
}
}
}
//=============================================================\\
var myResult = myDialog.show();
if(myResult == true){
var myLabelType = myLabelTypeDropdown.selectedIndex;
var myLabelHeight = 24; // A generic label height that will be adjusted later
myPasteFailure = false;
var myLabelOffset = myLabelOffsetField.editValue;
var myLabelStyle = myStyleNames[myLabelStyleDropdown.selectedIndex];
var mySwatch = mySwatchNames[mySwatchDropdown.selectedIndex];
myDialog.destroy();
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;
try{
myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch);
}
catch(e){
alert("Unable to add lables. " + e);
}
try{
resizeOverset() ;
}
catch(e){
alert("Unable to correct overset text. " + e);
}
if (myPasteFailure == true){
alert("Unable to paste from clipboard.");
}
app.documents.item(0).viewPreferences.horizontalMeasurementUnits = myOldXUnits;
app.documents.item(0).viewPreferences.verticalMeasurementUnits = myOldYUnits;
}
else{
myDialog.destroy();
}
}
//=============================================================\\
function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myLabelStyleName, mySwatchName){
var myDocument = app.documents.item(0);
myStoriesArray = new Array();
if (app.selection.length == 0) // If nothing is selected apply caption to all graphics in the document
{
var myConfirmation = confirm("Add captions to all images in the document?", false, "LabelGraphics.jsx" );
if (myConfirmation == true)
{
var myGraphics = myDocument.allGraphics;
}
}
else
{ // If graphics are selected, just add captions to the selected items, as long as they are rectangles(image frames)
var myConfirmation = true;
var mySelections = app.selection;
myGraphics = new Array();
for(i = 0; i < mySelections.length; i++){
if(mySelections[i] == "[object Rectangle]"){ //Check to make sure selection only includes rectangles
myGraphics.push(mySelections[i].allGraphics[0]);
}
else{
//alert("Objects other than graphics were selected!");
//Nothing happens if you don't select at least one graphic
}
}
}
myLabelStyle = myDocument.paragraphStyles.item(myLabelStyleName);
mySwatch = myDocument.swatches.item(mySwatchName);
if (myConfirmation == true){
for(var myCounter = 0; myCounter < myGraphics.length; myCounter++){
try{
myAddLabel(myDocument, myGraphics[myCounter], myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myStoriesArray);
}
catch(e){};
}
}
}
//=============================================================\\
function myAddLabel(myDocument, myGraphic, myLabelType, myLabelHeight, myLabelOffset, myLabelStyle, mySwatch, myStoriesArray){
var myLabel;
var myLink = myGraphic.itemLink;
var myPasteFromClipboard = false;
//Create the label layer if it does not already exist.
var myLabelLayer = myDocument.layers.item("labels");
try{
myLabelLayer.name;
}
catch (myError){
myLabelLayer = myDocument.layers.add({name:"labels"});
}
//Label type defines the text that goes in the label.
switch(myLabelType){
//File name
case 0:
myLabel = myLink.name;
break;
//File path
case 1:
myLabel = myLink.filePath;
break;
//XMP description
case 2:
try{
myLabel = myLink.linkXmp.description;
}
catch(myError){
myLabel = "No description available.";
}
break;
//XMP author
case 3:
try{
myLabel = myLink.linkXmp.author
}
catch(myError){
myLabel = "No author available.";
}
break;
//Paste from the clipboard
case 4:
try{
myPasteFromClipboard = true;
}
catch(myError){
myLabel = "No clipboard data available.";
}
break;
}
var myFrame = myGraphic.parent;
myX1 = myFrame.geometricBounds[1];
myY1 = myFrame.geometricBounds[2] + myLabelOffset;
myX2 = myFrame.geometricBounds[3];
myY2 = myY1 + myLabelHeight;
if (myPasteFromClipboard ==true)
{
try{
var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2]});
myTextFrame.insertionPoints.item(0).select();
app.paste();
}
catch(e){
myTextFrame.remove();
myPasteFailure = true;
}
}
else{
var myTextFrame = myFrame.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2], contents:myLabel});
}
myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;
myTextFrame.paragraphs.item(0).appliedParagraphStyle = myLabelStyle;
myTextFrame.fillColor = mySwatch;
myFrameParentStory = myTextFrame.parentStory;
myStoriesArray.push(myFrameParentStory);
}
//=============================================================\\
function myGetParagraphStyleNames(){
var myStyleNames = app.documents.item(0).paragraphStyles.everyItem().name;
return myStyleNames;
}
function myGetSwatchNames(){
var mySwatchNames = app.documents.item(0).swatches.everyItem().name;
return mySwatchNames;
}
function resizeOverset() {
for (var j = myStoriesArray.length - 1; j >= 0; j--) {
myLastFrame = myStoriesArray[j].texts[0].parentTextFrames[myStoriesArray[j].texts[0].parentTextFrames.length - 1];
myNewY2 = myLastFrame.geometricBounds[3]; //get the width of the text frame before doing fit()
myLastFrame.fit(FitOptions.FRAME_TO_CONTENT);
myNewY1 = myLastFrame.geometricBounds[1];
myNewX1 = myLastFrame.geometricBounds[2];
myNewX2 = myLastFrame.geometricBounds[0];
myLastFrame.geometricBounds = [myNewX1, myNewY1, myNewX2, myNewY2]; // reset the width to before fit() was ran
}
}