Home > InDesign Scripts > Get Font Styles from Font Family

Get Font Styles from Font Family

Few months ago on Adobe’s InDesign scripting forum question was asked about getting all font styles from selected font. I want to share my way for doing this. It involves two prototype Array functions, and nothing else. For this example I created small window for selecting font family. So, let’s get started.

First let’s see two array functions that we need. First one is for deleting all duplicated items in Array. We will create prototype for easier usage.

Array.prototype.unique = function (){
    var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++){
        for(var x = 0, y = r.length; x < y; x++){
            if(r[x]==this[i]) continue o;}
        r[r.length] = this[i];}
    return r;
}

Second one is little altered function that we discussed few days ago about searching inside Array. I added Array ‘r’ where we are going to push all found font style names. Line five is little messy, but it’s nothing hard to understand, I hope🙂 We are pushing sub-string of font name, but we cut off font family name based on tabulator position.

Array.prototype.findIn = function(search){
    var r = Array();
    for (var i=0; i<this.length; i++)
        if (this[i].indexOf(search) != -1){
            r.push(this[i].substr(this[i].indexOf("\t") + 1, this[i].length));
        }
    return r;
}

Good, now, let’s now analyze main part of this short snippet. First we collect all available fonts to myFonts, then we separate font families and delete duplicate entries in myFontsList. Why do we have duplicate entries? InDesign returns list of fonts, but with all available font styles, but we want just list of font families. Next, we push default drop-down box entry, and that’s it. Window is optional here. So, if we had selected something from drop-down, it’s time to get our selected font family name, and available font styles. We already have font family names in drop-down box, so we just use that. For searching font styles we use our second prototype function for searching inside array, and we just pass font family name to it. So, finished code looks like this:

var myFonts = app.fonts.everyItem();
myFontsList = myFonts.fontFamily.unique();
myFontsList.unshift("- Select Font Family -");

var myWin = new Window('dialog', 'My Fonts');
myWin.orientation = 'row';
with(myWin){
	myWin.sText = add('statictext', undefined, 'Select Font:');
	myWin.myFontDrop = add('dropdownlist',undefined,undefined,{items:myFontsList});
	myWin.myFontDrop.selection = 0;
	myWin.btnOK = add('button', undefined, 'OK');
	};
myWin.center();
var myWindow = myWin.show();

if(myWindow == true && myWin.myFontDrop.selection.index != 0){
    var myFont = myWin.myFontDrop.selection;
    var myFontStyles = myFonts.name.findIn(myFont);
}

As I said, using window is optional. I will show you how to get font styles from font family if you already know family name. We use just this two lines for that and of course we also use prototype function for searching inside Array.

var myFonts = app.fonts.everyItem();
var myFontStyles = myFonts.name.findIn("Arial");
  1. Shonky
    August 30, 2010 at 08:56

    Hi tomaxxi,

    Can you please explain your unique function. I am trying to understand but i can’t.

    Shonky

    • August 30, 2010 at 11:35

      It simply creates new array ‘r’ and fills it with elements from array you passed. First loop iterates through array you passed, and loop inside iterates through new created array, and if matches loop goes back to ‘o:’ and if it don’t have match, it adds new item to array ‘r’.

      tomaxxi

  2. Shonky
    August 30, 2010 at 12:23

    I have never seen using of for loop like this

    o:for(var i = 0, n = this.length; i < n; i++)

    Shonky

    • August 30, 2010 at 12:30

      Oh, I know, you must be confused with ‘o:’ With ‘continue o;’ you escape from second loop, and continue with first loop, it’s some kind of label. So if you have multiple loops inside each other, you can do that to escape from one to another.

      tomaxxi

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: