Article View: adobe.scripting.indesign
Article #14816JS: PlaceAllFiles.js
From: Magnus_Strand
Date: Tue, 13 Jan 2004 08:05
Date: Tue, 13 Jan 2004 08:05
105 lines
4311 bytes
4311 bytes
Hi,
I have made a quick translation of the cool script PlaceAllFiles.vbs
by Olav Kvern to JavaScript. Someone asked about this before.
Please send me an e-mail if something should be wrong.
Regards,
Magnus Strand
ms @ timbits.se
//----------PlaceAllFiles.js-----------
// Enter the number or rows and columns you want. If you're adding
// a user interface to this script, you can get these values from
// a pair of text fields.
myNumberOfRows = 1
myNumberOfColumns = 1
// If you want to remove any empty frames, set myRemoveEmptyFrames to true.
myRemoveEmptyFrames = true;
// Enter the path to the folder containing the images you want to place.
myFolderName = "C:\\images";
myFramesPerPage = myNumberOfRows * myNumberOfColumns;
myDocument = app.documents.add();
myDocumentPreferences = myDocument.documentPreferences ;
// Fill in the path to the folder containing the files you want to place.
// If you're add a user interface to this script, you can use a DirListBox
// to set the folder location.
folder = new Folder(myFolderName);
myFiles = new Array();
myFiles2 = folder.getFiles("*");
n = myFiles2.length;
item = 0;
for (i = 0; i < n; i++)
{
if (myFiles2[i] instanceof File)
{
myFiles[item] = myFiles2[i];
item++;
}
}
myNumberOfFrames = myFiles.length;
myNumberOfPages = Math.round(myNumberOfFrames / myFramesPerPage);
if ((myNumberOfPages * myFramesPerPage) < myNumberOfFrames)
{
myNumberOfPages++;
}
myDocumentPreferences.pagesPerDocument = myNumberOfPages;
myDocumentPreferences.facingPages = false;
myPage = myDocument.pages.item(1);
myMarginPreferences = myPage.marginPreferences;
myLeftMargin = myMarginPreferences.left;
myTopMargin = myMarginPreferences.top;
myRightMargin = myMarginPreferences.right;
myBottomMargin = myMarginPreferences.bottom;
myLiveWidth = myDocumentPreferences.pageWidth - (myLeftMargin + myRightMargin);
myLiveHeight = myDocumentPreferences.pageHeight - (myTopMargin + myBottomMargin);
myFrameWidth = myLiveWidth / myNumberOfColumns;
myFrameHeight = myLiveHeight / myNumberOfRows;
myPages = myDocument.pages;
// 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 (myRowCounter = myNumberOfRows; myRowCounter >= 1; myRowCounter--)
{
myY1 = myTopMargin + (myFrameHeight * (myRowCounter - 1));
myY2 = myTopMargin + (myFrameHeight * myRowCounter);
for (myColumnCounter = myNumberOfColumns; myColumnCounter >= 1; myColumnCounter--)
{
myX1 = myLeftMargin + (myFrameWidth * (myColumnCounter - 1));
myX2 = myLeftMargin + (myFrameWidth * myColumnCounter);
myRectangle = myPage.rectangles.add();
myRectangle.geometricBounds = Array(myY1, myX1, myY2, myX2);
}
}
}
// 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 (i = 0; i < myNumberOfFrames; i++)
{
myFile = myFiles[i];
pi = myDocument.pageItems.item(i);
n = myDocument.pageItems.length;
s = myFile.fsName.toString();
pi.select(SelectionOptions.replace);
pi.place(s);
pi.fit(FitOptions.proportionally);
pi.fit(FitOptions.centerContent);
}
if (myRemoveEmptyFrames)
{
n = myDocument.rectangles.length;
for (i = 0; i < n; i++)
{
rect = myDocument.rectangles[i];
if (rect.ContentType == ContentType.unassigned)
{
rect.remove();
}
}
}
Technical Details
Message-ID:
<2ccf5713.-1@webx.la2eafNXanI>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!webxShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f939a,f7d80a9345f3e54c,start X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2004-01-13 00:07:32 PST From: Magnus_Strand@adobeforums.com Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!webx Newsgroups: adobe.scripting.indesign Subject: JS: PlaceAllFiles.js Message-ID: <2ccf5713.-1@webx.la2eafNXanI> Date: Tue, 13 Jan 2004 00:05:38 -0800 Lines: 117 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit