0

I'm successfully loading a JSON file into InDesign via basil.js.

var jsonString = b.loadString("data.json");
var jsonData;

function setup() {

  jsonData = b.JSON.decode(jsonString);

  for (var i = 0; i < jsonData.length; i++) {
    var myText = jsonData[i].text;
    b.text(myText, 100, 10 * i, 100, 20);
  };
}

b.go();

The result looks as expected:

The result looks as expected: What I'd like to achieve is

  1. That the text frames automatically fit their height to the content, smth. like fit(FitOptions.FRAME_TO_CONTENT);
  2. That the textBoxes continue until the page bounds and then a new page is added.

Any hints are warmly appreciated. Cheers!

SMAKSS
  • 9,606
  • 3
  • 19
  • 34
ranzen
  • 3
  • 2

1 Answers1

0

for your first problem

That the text frames automatically fit their height to the content, smth. like fit(FitOptions.FRAME_TO_CONTENT);

you can just use myText.fit(FitOptions.FRAME_TO_CONTENT);. You don't have to stick with Basil if something is not implemented there.

That the textBoxes continue until the page bounds and then a new page is added.

This is a little trickier. If you extend the textframes their size will change. You will need another variable outside of your loop that tracks the geometricBounds of the last frame and adds the next frame at the beginning of them. When your last bounds exceed the pageHeight you will need to add a new page.

This is something to get you started. It is untested code.

/**
 * InDesign Objects have a property called 
 * geometricBounds
 * these are the bounds in page coordinates sorted like this
 * [y1, x1, y2, x2]
 * or in words
 * [
 *   y of upper left corner, 
 *   x of upper left corner,
 *   y of lower right corner, 
 *   x of lower right corner
 * ]
 */
var prevGeometricBounds = [100, 10, 100, 10]; 
for (var i = 0; i < jsonData.length; i++) {
  var myText = jsonData[i].text;
  var x = prevGeometricBounds[1];
  var y = prevGeometricBounds[0];
  var w = prevGeometricBounds[3] - prevGeometricBounds[1];
  var h = prevGeometricBounds[2] - prevGeometricBounds[1];
  var textFrame = text(myText, x, y, w, h);
  textFrame.fit(FitOptions.FRAME_TO_CONTENT);
  prevGeometricBounds = myText.geometricBounds;
  if(prevGeometricBounds[2] > height){
    addPage();
  }
}

Another hint. As you can see I omitted the b. Basil evolved a lot but we did not yet release the version 2. You can grab the latest version from the develop branch over at GitHub. The docs for that are located here.


Edit 1

In Basil 2 you should be able to load the JSON using loadString and then decode it using JSON.parse

The Basil text function returns a textFrame. So the fit function should work. If you run into bugs we are happy to accept bug reports over at github.


Edit 2

The fit was called on the string you loaded from the JSON not on the result of the text() function.

fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41