Adding a function to
Hi
I am not very experienced with java and I thought that i would play with something in my free time, this has become an obsession and now I am going a little crazy.
I found an exist script that had some modifications done to it then tried to add in a few of my own things,
This is doing what I need it to do except for one thing that I couldn't do, until i realized I was doing it wrong (well i think)
I have an existing file name BTAS8234_P14-20 New.indd its a page filled with Text and Images.
I want to put a label on each image box with part of the file name, the actual page number the image is on and a number. so the the label would end up looking like 8234_14_001 (for page 14) or 8235_15_001 (for page 15)
I got some help with some very nice people and this is what I was using this to create the label myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' +app.activeWindow.activePage.name+'_'+padNumber(myCounter);
I thought the issue was that it was with the above.. But now i am thinking its more to do with one of the functions (line 128) so I thought and I may be way off the mark, of writing in a new function to Get a count of all pages in the document. (i started writing this line 121 but I raelly don't know what i am doing)
then with the function on Line 128 changing it so that it goes through allGraphics on each page in the order from first to last.
I am not sure if i am on the right track or not, but any help would be greatly appreciated.
Thank you
//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(){
// Make sure the swatch "ImageGreen" exists
try {
app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.CMYK, colorValue:[0,0,100,0], colorModel:ColorModel.PROCESS});
} catch (_) { }
LabelYellow = app.activeDocument.swatches.item("ImageLabel");
// Make sure the paragraph style "ImageSize" exists
try {
app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial\tRegular", pointSize:9, justification: Justification.CENTER_ALIGN});
} catch (_) { }
reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
}
firstStart();
//=============================================================\\
function firstStart(){
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 myStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
var mySwatch = app.activeDocument.swatches.item("ImageLabel");
var myDialog = app.dialogs.add({name:"Label Graphics"});
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:["Image Name","Image File Name", "Image File Path", "XMP Description", "XMP Author","Paste from clipboard"], selectedIndex:0});
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Label Offset", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myLabelOffsetField = measurementEditboxes.add({editValue:0});
}
}
}
//=============================================================\\
var myResult = myDialog.show();
if(myResult == true){
var myLabelType = myLabelTypeDropdown.selectedIndex;
var myLabelHeight = 14.173; // A generic label height that will be adjusted later
myPasteFailure = false;
var myLabelOffset = myLabelOffsetField.editValue;
var myStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
var mySwatch = app.activeDocument.swatches.item("ImageLabel");
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, myStyle, 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 getAllPages();
var getAllPages = myOpenDocument.Pages.length;
//=============================================================\\
function myAddLabels(myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch){
var myDocument = app.activeDocument.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 == "[object Rectangle]"){ //Check to make sure selection only includes rectangles
myGraphics.push(mySelections.allGraphics[0]);
}
else{
//alert("Objects other than graphics were selected!");
//Nothing happens if you don't select at least one graphic
}
}
}
var myStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
mySwatch = myDocument.swatches.item("ImageLabel");
if (myConfirmation == true){
for(var myCounter = 1; myCounter < myGraphics.length; myCounter++){
try{
myAddLabel(myDocument, myGraphics[myCounter], myCounter, myLabelType, myLabelHeight, myLabelOffset, myStyle, mySwatch, myStoriesArray);
}
catch(e){};
}
}
}
//=============================================================\\
//=============================================================\\
function myAddLabel(myDocument, myGraphic, myCounter, myLabelType, myLabelHeight, myLabelOffset, myStyle, 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("Coded Layout");
try{
myLabelLayer.name;
}
catch (myError){
myLabelLayer = myDocument.layers.add({name:"Coded Layout"});
}
//Label type defines the text that goes in the label.
switch(myLabelType){
//File name
case 0:
myLabel = myDocument.properties.name.replace(/\D+/,'').split(' ')[0].split('_')[0]+'_' + app.activeWindow.activePage.name+'_'+padNumber(myCounter);
break;
//Image name
case 1:
myLabel = myLink.name;
break;
//Image File path
case 2:
myLabel = myLink.filePath;
break;
//XMP description
case 3:
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 = myStyle;
myTextFrame.fillColor = mySwatch;
myFrameParentStory = myTextFrame.parentStory;
myStoriesArray.push(myFrameParentStory);
}
//=============================================================\\
function myGetParagraphStyleNames(){
var myStyle = app.activeDocument.paragraphStyles.item("ImageLabel");
return myStyle;
}
function myGetSwatchNames(){
var mySwatch = app.activeDocument.swatches.item("ImageLabel");
return mySwatch;
}
function padNumber(number) {
if (number<=9999) { number = ("000"+number).slice(-3); }
return number;
}
function resizeOverset() {
for (var j = myStoriesArray.length - 1; j >= 0; j--) {
myLastFrame = myStoriesArray
.texts[0].parentTextFrames[myStoriesArray .texts[0].parentTextFrames.length - 1];
myNewY2 = myLastFrame.geometricBounds[3]; //get the width of the text frame before doing fit()
myLastFrame.fit(FitOptions.CENTER_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
}
}