fontsadobe-indesignbasil.js

How to apply a random font to a textbox by scripting InDesign?


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!


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.