You shoud not be "happy when it works". The problem when copying and pasting snippets of code without understanding them is that it usually does not work, and when it does work, it does not work reliably. You "write" the script, it works for the initial tests (which usually involve the same data you used to develop the script), you put it into production, and somewhere, somehow, in page 230 in a 600 pages document something goes wrong and everyting is messed up. Someone goes through the file, sees that the first few oages are ok, then the next 100 pages are ok so ... ok... and the file gets to the printer then to market and you are suddenly in a big hole! It is exactly what happend to me when I had just started with scripting and "wrote" (as in copy-pasted a bunch of stuff I had no idea how it worked) a script that did some extensive text manipulations based mostly on GREP patterns. One of the things it was supposed to do was to remove all spaces inside specially marked anchored text frames. But, in some 1%-chance of happening circumstances bug, it also removed all the spaces in the parent story of the anchored frame. The company I worked for back then had to pay damages to the customer of over 45000 euros because documents diseminated and printed in about 20 different countries around the globe had 20-30 pages of text in them whithout any spaces in. Now that the rant is over I'm gonna do something I very rarely do these days and give you the whole solution In the hope that you will actually learn how it works and use it to go forward. First step is to read and understand the requirements: you need to read data from a CSV file and use that data to create and paginate a new document. You should already see two distinctive parts (READ and PAGINATE) connected by a common thread (DATA). /* First step is to read and understand the requirements: you need to read data from a CSV file and use that data to create and paginate a new document. You should already see two distinctive parts (READ and PAGINATE) connected by a common thread (DATA). So let's start with the data. There are many many ways to structure your data (some better than others), but for this a simple array of objects somewhat mimicking the csv structure should suffice: data=[ { page:Number, adFile:String, geometricBounds:[Number,Number,Number,Number] }] Next up is reading data: */ (function() { //IIFE is always better than "main" crap: https://stackoverflow.com/questions/8228281/what-is-the-function-construct-in-javascript function readCSV() { // Open a dialog to import the CSV file var f = File.openDialog("Please select the CSV File…", function(f) { return /\.csv$/i.test(f.name); }); //do you understand how that inline function works? if (!f) { return; //the user hit cancel on the dialog. Nothing to do! } // we need some more variables and it's best to declare them early. var line, //the current csv line entry ret = [], //the return array that will contain all of the data lineIndex = 1, //the current csv line number, used for error reporting. We start from "1" because that is how non-programmers (and some Pascal and older BASIC programmers) count. gb, //the geometric bounds (as used by InDesign of the csv entry) obj; //the full entry object. Just to make code more readable. f.open('r'); //first line is the headers, we ignore them. line = f.readln(); while (!f.eof) { //this means while we havenot reached the End Of File line = f.readln().split(';'); //walk and chew gum at the same time: read a line of text from the file and convert it to an array lineIndex++; //increment the line index, this can be read as lineIndex=lineIndex+1; if (line.length !== 6) { // this entry in the csv is wrong. you can either ingore it or quit the whole thing. your choice, comment and uncomment as needed. throw 'Invalid csv data on line ' + lineIndex + ' (' + line.join(';') + ')'; //can you explain what this does? //other choice: //continue; } //csv entry is: Page;AdName;Width(mm);Height(mm);X-value;Y-value //canonic format of geometric bounds is [y0,x0,y1,x1]: gb = [ parseFloat(line[5]), //Y-value parseFloat(line[4]), //X-value parseFloat(line[5]) + parseFloat(line[3]), //y+height parseFloat(line[4]) + parseFloat(line[2]), //x+width ]; obj = { page: parseInt(line[0]) - 1, //remember that programmers count from 0? adFile: File("/Users/vlad/Downloads/" + line[1]), geometricBounds: gb }; //add the object to the return array ret.push(obj); } //at this point we have all our data. But because the program works on a page by page basis it is good to sort it as such. See https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort return ret.sort(function(a, b) { return a.page - b.page; }); r } /* we now need a function that will paginate the data*/ function paginate(data) { //always declare your variables! var doc, page, frame; doc = app.documents.length ? app.documents[0] : app.documents.add(); /*see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator for the above statement. It is a shorthandend "if": if there are documents (documents.length>=0, use the front-most one (documents[0]), otherwise create a new one */ //because the data is in mm let's make sure the script will use mm as a measurement (but i strongly recommend you start dealing with point asap!) app.scriptPreferences.measurementUnit=MeasurementUnits.MILLIMETERS; //first let's make sure our document has enough pages. Since we have the data storted knowing how many pages we need is easy. Just look at the last data entry. But we do need to make sure we don't acidentally remove pages!! doc.documentPreferences.pagesPerDocument = Math.max(doc.pages.length, data[data.length-1].page + 1); /*things to note in the above statement: by using Math.max we are getting the bigger number between the number of currently existing nr of pages in the document and the biggest page number in our data (+1 because .. see above about counting)*/ //now let's go through out data. To keep code simpler we'll use another function "paginateData" (see below) for (var i = 0; i < data.length; i++) { page = doc.pages[data.page]; paginateData(page, data); } } //this function will paginate one entry on a set page function paginateData(page, entry) { var rect = page.rectangles.add(); rect.geometricBounds = entry.geometricBounds; //what if the ad file is not available? Let's convert it to text and add a message! if (!entry.adFile.exists) { rect.contentType = ContentType.TEXT_TYPE; // force to text frame rect = page.textFrames.itemByID(rect.id); rect.contents = 'File not found: ' + entry.adFile; return; } rect.place(entry.adFile); } /*now let's tie everything up togheter. Not that much to do */ var data = readCSV(); if (!data) { //user canceled return; } paginate(data); alert('All done!\nYou can go outside and play now!'); }());
... View more