0

Here is my code to build a 20 page document with prime numbered grids and random image positions and formats but they go out of the page, and it would be good to get the images to snap to the grid and have an individual comment/text per image.

I think I would need to include a JSON file for the individual text and tile of each image. Ideally the document should be for 500 images.

#include "basiljs/bundle/basil.js";

function draw() {
    var nbPages= 20;
    var wImage = 100;
    var hImage = 100;
    b.units(b.MM);
    
    // add one page at the end of the document and jump to it
   
    for(var i=0;i<nbPages;i++){

        var posX = posWidth()
        var posY = posHeight();
        
        if(i<10){
        var imgPath = "img00000000"+i+".png";
        }else if(i<100){
            var imgPath = "img0000000"+i+".png"; 
        }else{
            var imgPath = "img000000"+i+".png"; 
 
        }
        b.addPage();
        createIMG(imgPath, posX, posY, wImage, hImage); 
    }

    
}

function createIMG(imgPath, posX, posY, w, h){

    var bleed = 10;
    var random = b.random(0,4);

b.noStroke();
    b.image(imgPath, bleed +posX,bleed + posY, b.random(0,175), 100);
    b.text('I would need an individual text per image in a JSON file i guess', bleed +posX,bleed + posY+100, 150, 50); 
    b.margins(20,20,18,15,b.pageCount(20))
    b.pageNumber()
//   Prime number *10 grid
    b.guideY(20);
    b.guideY(30);
    b.guideY(50);
    b.guideY(70);
    b.guideY(110);
    b.guideY(130);
    b.guideY(170);
    b.guideY(190);
    b.guideY(230);
    b.guideY(290);
    b.guideY(310);
    b.guideY(370);
    b.guideY(410);
    b.guideY(430);
    b.guideY(470);
    b.guideY(530);
    b.guideY(590);
    b.guideY(610);
    b.guideY(670);
    b.guideY(710);
    b.guideY(730);
    b.guideY(790);
    b.guideY(830);
    b.guideY(890);
    b.guideY(970);
  
    b.guideX(20);
    b.guideX(30);
    b.guideX(50);
    b.guideX(70);
    b.guideX(110);
    b.guideX(130);
    b.guideX(170);
    b.guideX(190);
    b.guideX(230);
    b.guideX(290);
    b.guideX(310);
    b.guideX(370);
    b.guideX(410);
    b.guideX(430);
    b.guideX(470);
    b.guideX(530);
    b.guideX(590);
    b.guideX(610);
    b.guideX(670);
    b.guideX(710);
    b.guideX(730);
    b.guideX(790);
    b.guideX(830);
    b.guideX(890);
    b.guideX(970);
    
}

function posWidth(){
    var widthPage =210;
    var random = b.random(0,4);

    if(random<1){
        return 0;
    }else if(random<2){
        return widthPage/4;
    }else if(random<3){
        return widthPage/2;

    }else{
        return widthPage/4*3;

    
    }
}
function posHeight(){
    var widthPage =210;
    var random = b.random(0,4);

    if(random<1){
        return 0;
    }else if(random<2){
        return widthPage/4;
    }else if(random<3){
        return widthPage/2;

    }else{
        return widthPage/4*3;

    
    }
}
function sizeAndPosImg(){
    var  heightPage= 297
    var random = b.random(0,4);

    if(random<1){
        return 0;
    }else if(random<2){
        return widthPage/4;
    }else if(random<3){
        return widthPage/2;

    }else{
        return widthPage/4*3;    
    }
}
b.go();

ambaamba
  • 71
  • 6
  • (1) It would be easier if you show what you want to get after all, screenshot or something (.indd or .idml file would be great). (2) Do you need an exclusive `basil.js` solution or it can be a pure extendscript code? – Yuri Khristich Mar 07 '22 at 14:50
  • 1
    https://www.swisstransfer.com/d/b67122d6-82eb-4a4f-8f0d-c99e2da94fe9 : HERE IS the indesign file. Ideally every position text/image randomized exept for title. I use Basil.js to simplify the language but I am willing to learn if there are other ways to do it definitely! – ambaamba Mar 07 '22 at 15:15
  • Basically the idea would be to make a generative book only with code and no indesign use. except for the rules applied by the code. – ambaamba Mar 07 '22 at 15:35
  • Oh... last time I wrote something like that it was 1000+ lines of code. Not sure if it makes much sense to try to build a project like this via quesitons-answers on stackoverflow if you're not able to maintain it by you self. There are a hell of little steps that need to figure out. Even in this question it's need: to parse JSON, to make guides grid, to read from disk and paste images. It's already overkill for a beginner. And the most important thing will be not these little steps but the architecture a whole. – Yuri Khristich Mar 07 '22 at 17:58
  • I'd propose to make 3 separate questions. For example here we could figure out how to make the guides grid. Could you clarify how the grid should look like? From your .indd file and from the provided code I didn't manage to understand it. You can just create it manually and add to the question the screenshot. – Yuri Khristich Mar 07 '22 at 18:40
  • Maybe the first question would be how to integrate a JSON file into my code(with image name and description, page title). second question is how to make the images stay on the pages and not have a value beyond. – ambaamba Mar 07 '22 at 22:21
  • Okay. How the JSON file looks like? "Integrate" — get some info from the JSON file? – Yuri Khristich Mar 07 '22 at 23:53
  • Basically you can read and parse json file (not any json, though) this way: `var json = $.evalFile(File('my_file.json'));` – Yuri Khristich Mar 07 '22 at 23:58
  • And is there any way to script the images so they stay or move into the page not out of the layout? thanks – ambaamba Mar 08 '22 at 08:44
  • 1
    Of course there is the way to put any image to any place on any page. But let figure out how to handle json first. Where you get your json from? Do you write it manually? Show it – Yuri Khristich Mar 08 '22 at 13:53
  • Yes I would like to write it but I don't know how to start, I am completely lost. – ambaamba Mar 10 '22 at 06:24
  • So, could you show how the book should look like? I don't understand what you have (images? texts?) and what you trying to get exactly. And one more thing, for why do you need it at all? Do you want to make dozens 'books' with a script? (From hundreds images and texts?) Since if you want to make just one book it's much easier to do in the usual manual way with InDesign – Yuri Khristich Mar 12 '22 at 11:48

0 Answers0