//ImageCatalog.js
//An InDesign CS JavaScript
//
//Creates an image catalog from the graphic files in a selected folder.
//Each file can be labeled with the file name, and the labels are placed on
//a separate layer and formatted using a paragraph style ("label") you can
//modify to change the appearance of the labels.
//
//For more information on InDesign scripting, go to http://www.adobe.com/...esign/scripting.html
//Or visit the InDesign Scripting User to User forum at http://www.adobeforums.com.
//
var myFiles = new Array;
var myFilteredFiles = new Array;
//The myExtensions array contains the extensions of the graphic file types you want
//to include in the catalog. You can remove extensions from or add extensions to this list.
var myExtensions = [".jpg", ".jpeg", ".eps", ".ps", ".pdf", ".tif", ".tiff", ".gif", ".psd", ".ai"]
//Display the folder browser.
var myFolder = Folder.selectDialog("Select the folder containing the images", "");
//Get the path to the folder containing the files you want to place.
if(myFolder != null){
for(myExtensionCounter = 0; myExtensionCounter < myExtensions.length; myExtensionCounter++){
myExtension = myExtensions[myExtensionCounter];
myFiles = myFolder.getFiles("*"+ myExtension);
if(myFiles.length != 0){
for(var myFileCounter = 0; myFileCounter < myFiles.length; myFileCounter++){
myFilteredFiles.push(myFiles[myFileCounter]);
}
}
}
if(myFilteredFiles.length != 0){
myDisplayDialog(myFilteredFiles, myFolder);
}
}
function myDisplayDialog(myFiles, myFolder){
var myLabelWidth = 115;
var myDialog = app.dialogs.add({name:"Image Catalog"});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Information"});
}
}
with(borderPanels.add()){
with(dialogColumns.add()){
with(dialogRows.add()){
staticTexts.add({staticLabel:"Source Folder:", minWidth:myLabelWidth});
staticTexts.add({staticLabel:myFolder.path + "/" + myFolder.name});
}
with(dialogRows.add()){
staticTexts.add({staticLabel:"Number of Images:", minWidth:myLabelWidth});
staticTexts.add({staticLabel:myFiles.length + ""});
}
}
}
with(dialogRows.add()){
staticTexts.add({staticLabel:"Options"});
}
with(borderPanels.add()){
with(dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Number of Rows:", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myNumberOfRowsField = integerEditboxes.add({editValue:3});
}
}
with(dialogRows.add()){
staticTexts.add({staticLabel:"Number of Columns:", minWidth:myLabelWidth});
var myNumberOfColumnsField = integerEditboxes.add({editValue:3});
}
with(dialogRows.add()){
var myLabelsCheckbox = checkboxControls.add({staticLabel:"Add Labels", checkedState:true});
}
with(dialogRows.add()){
var myRemoveEmptyFramesCheckbox = checkboxControls.add({staticLabel:"Remove Empty Frames:", checkedState:true});
}
}
}
}
var myResult = myDialog.show();
if(myResult == true){
var myNumberOfRows = myNumberOfRowsField.editValue;
var myNumberOfColumns = myNumberOfColumnsField.editValue;
var myLabels = myLabelsCheckbox.checkedState;
var myRemoveEmptyFrames = myRemoveEmptyFramesCheckbox.checkedState;
myMakeImageCatalog(myFiles, myNumberOfRows, myNumberOfColumns, myLabels, myRemoveEmptyFrames);
}
myDialog.destroy();
}
function myMakeImageCatalog(myFiles, myNumberOfRows, myNumberOfColumns, myLabels, myRemoveEmptyFrames){
var myPage, myFile, myCounter, myX1, myY1, myX2, myY2, myRectangle, myLabelStyle, myLabelLayer;
var myFramesPerPage = myNumberOfRows * myNumberOfColumns;
var myDocument = app.documents.add();
myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
var myDocumentPreferences = myDocument.documentPreferences;
var myNumberOfFrames = myFiles.length;
var myNumberOfPages = Math.round(myNumberOfFrames / myFramesPerPage);
if ((myNumberOfPages * myFramesPerPage) < myNumberOfFrames){
myNumberOfPages++;
}
myDocumentPreferences.pagesPerDocument = myNumberOfPages;
myDocumentPreferences.facingPages = false;
var myPage = myDocument.pages.item(0);
var myMarginPreferences = myPage.marginPreferences;
var myLeftMargin = myMarginPreferences.left;
var myTopMargin = myMarginPreferences.top;
var myRightMargin = myMarginPreferences.right;
var myBottomMargin = myMarginPreferences.bottom;
var myLiveWidth = myDocumentPreferences.pageWidth - (myLeftMargin + myRightMargin);
var myLiveHeight = myDocumentPreferences.pageHeight - (myTopMargin + myBottomMargin);
var myFrameWidth = myLiveWidth / myNumberOfColumns;
var myFrameHeight = myLiveHeight / myNumberOfRows;
var myPages = myDocument.pages;
if(myLabels == 1){
//Create the label paragraph style if it does not already exist.
myLabelStyle = myDocument.paragraphStyles.item("labels");
try{
myLabelStyle.name;
}
catch (myError){
myLabelStyle = myDocument.paragraphStyles.add({name:"labels"});
}
myLabelStyle.pointSize = 8;
//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"});
}
}
// Construct the frames in reverse order. Don't laugh--this will
// save us time later (when we place the graphics).
for (myCounter = myDocument.pages.length-1; myCounter >= 0; myCounter--){
myPage = myPages.item(myCounter) ;
for (var myRowCounter = myNumberOfRows; myRowCounter >= 1; myRowCounter--){
myY1 = myTopMargin + (myFrameHeight * (myRowCounter - 1));
myY2 = myTopMargin + (myFrameHeight * myRowCounter);
for (var myColumnCounter = myNumberOfColumns; myColumnCounter >= 1; myColumnCounter--){
myX1 = myLeftMargin + (myFrameWidth * (myColumnCounter - 1));
myX2 = myLeftMargin + (myFrameWidth * myColumnCounter);
myRectangle = myPage.rectangles.add(myDocument.layers.item(-1), undefined, undefined, {geometricBounds:[myY1, myX1, myY2, myX2], strokeWeight:0, strokeColor:myDocument.swatches.item("None")});
}
}
}
// Because we constructed the frames in reverse order, rectangle 1
// is the first rectangle on page 1, so we can simply iterate through
// the rectangles, placing a file in each one in turn. myFiles = myFolder.Files;
for (myCounter = 0; myCounter < myNumberOfFrames; myCounter++){
myFile = myFiles[myCounter];
myRectangle = myDocument.rectangles.item(myCounter);
myRectangle.place(File(myFile));
myRectangle.label = myFile.fsName.toString();
myRectangle.fit(FitOptions.proportionally);
myRectangle.fit(FitOptions.centerContent);
myRectangle.fit(FitOptions.frameToContent);
if(myLabels == 1){
myX1 = myRectangle.geometricBounds[1];
myY1 = myRectangle.geometricBounds[2];
myX2 = myRectangle.geometricBounds[3];
myY2 = myRectangle.geometricBounds[2]+24;
myTextFrame = myRectangle.parent.textFrames.add(myLabelLayer, undefined, undefined,{geometricBounds:[myY1, myX1, myY2, myX2], contents:myRectangle.label});
myTextFrame.textFramePreferences.firstBaselineOffset = FirstBaseline.leadingOffset;
myTextFrame.paragraphs.item(0).appliedParagraphStyle = myLabelStyle;
}
}
if (myRemoveEmptyFrames == 1){
for (var myCounter = myDocument.rectangles.length-1; myCounter >= 0;myCounter--){
if (myDocument.rectangles.item(myCounter).contentType == ContentType.unassigned){
myDocument.rectangles.item(myCounter).remove();
}
else{
//As soon as you encounter a rectangle with content, exit the loop.
break;
}
}
}
}