Question

I want to choose a random font from my installed fonts and apply it to a text box. I am using basil.js to facilitate coding.

I don't want to write a list of all available fonts by myself, like this:

var font = [];
font[0] = "Times New Roman";
font[1] = "Myriad Pro";
font[2] = "Impact";
b.textFont( font[Math.floor(Math.random()*font.length)] );     

(this idea is from: http://forums.adobe.com/thread/325180)

Many Thanks!

Était-ce utile?

La solution

Does your basil.js play nice along with ID's native Extendscript? If so, you can use this:

var allFonts = app.fonts.everyItem().getElements();
b.textFont( allFonts[Math.floor(Math.random()*allFonts.length)] );

The first line accesses the live app.fonts object in InDesign, and since this is slow I prefer to use the everyItem().getElements() trick to 'convert' it to a static array first. Typically you would do this only once, near the start of your script.

Autres conseils

in addition to Jongware's answer ... there is now also an example which does exactly that: https://github.com/basiljs/basil.js/blob/master/examples/typography/RandomFonts.jsx

#includepath "~/Documents/;%USERPROFILE%Documents";
#include "basiljs/bundle/basil.js";

function draw() {
  var textFramesCount = 20;
  var fonts = app.fonts;

  for (var i=0; i < textFramesCount; i++) {
    var posX = b.random(0,600);
    var posY = b.random(0,850);
    var randomFontSize = b.round(b.random(2,20));
    var randomIndex = b.floor( b.random(app.fonts.length) );
    var fontName = fonts[randomIndex].fullName;
    b.println(fontName);

    b.textSize( randomFontSize );
    b.textFont( fonts[randomIndex] );
    var textFrame = b.text(fontName+" "+randomFontSize, posX,posY, 300,20);
  };
}

b.go();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top