Skip to content

Instantly share code, notes, and snippets.

@ff6347
Created May 10, 2012 07:23
Show Gist options
  • Save ff6347/2651660 to your computer and use it in GitHub Desktop.
Save ff6347/2651660 to your computer and use it in GitHub Desktop.
Create a image matrix from files in InDesign
// image_matrix.jsx
// creates a matrix of images ;)
// Copyright (c) 2012
// Fabian "fabiantheblind" Morón Zirfas
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to
// whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIO
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// see also http://www.opensource.org/licenses/mit-license.php
{ // START SCRIPT
main(); // you need a function to be able to cancel a script
function main(){ // all is in here
var w = 25; // the image sizes
var allImages = loadFiles("*.jpg"); // opens a prompt and lets the user choose a folder
if(allImages == null) return; // if that what the function returns null is - cancel
var pw = Math.round(Math.sqrt(allImages.length)) * w + (w*2); // this will hold the page width
var ph = pw;
// THIS IS THE ESSENTIAL PART
// if the rounded value is not the same as the value
// we have a float. so we must add another row for images
if(Math.round(Math.sqrt(allImages.length)) != Math.sqrt(allImages.length)){
ph = pw + w;
}
var doc = app.documents.add(); //build a basic document
doc.documentPreferences.pageWidth = pw; // set the width
doc.documentPreferences.pageHeight = ph; // set the height
var page = doc.pages.item(0); // finally - get the first page
var y = w; // the upper left corner
var x = w; // the upper left corner
for(var i = 0; i < allImages.length;i++){ // loop thru all images
var rect = page.rectangles.add({geometricBounds:[y,x,y + w,x + w]}); // add a rectangle to the page
rect.place(allImages[i] ); // place the image
rect.fit(FitOptions.FILL_PROPORTIONALLY); // fit it to the frame
rect.fit(FitOptions.CENTER_CONTENT); // fit it to the frame
x +=w; // increase x by an image width
if(x >= pw - (w)){ // if x is the width minus an image width
x = w; // reset x
y+= w; // and increment y by an image width
}; //end increase x and y conditional
};//close allImages loop
};// end main function
function loadFiles(type){ // the function that loads the files
var theFolder = Folder.selectDialog ("Choose the Folder");// user select a folder
if(!theFolder){ // if the folder is not a folder cancel the script
return; // this cancels the whole function image_loadFiles
}; // end folder check
var allImages = theFolder.getFiles(type);// get the files by type
if((allImages.length < 1)||(allImages == null) ){// again if check if there are images
alert("There are no images of the type: " + type);// hm something went wrong? User error
return null; // so we cancel the function
}else{
return allImages; // give back the images. Success!
};// end all images check
};// end function loadFiles
} // END OF SCRIPT
@ff6347
Copy link
Author

ff6347 commented May 11, 2012

This Gist is part of MT4D coming soon on fabiantheblind.info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment