Question

I would like to know what is the proper way to reference a character style, a paragraph style or any other style in InDesign Scripting.

Please keep in mind that they could be under style group.

Was it helpful?

Solution 2

I took an approach where you have to work with what I call the Fully Qualified Name (FQN) of a style, which is combination of all the groups and the style name separated by a pipe (|)

I've implemented the two following function in our code. They can be used with all type of styles with just a little bit of modification to the getStyleByFullyQualifiedName() function to support the other 4 types.

//getStyleFullyQualifiedName allow you to retrieve the style FQN,
//by providing a style object. Combine the name of the style and
//groups together separated by pipe (|).
function getStyleFullyQualifiedName(object){
    var objectName = object.name;

    if(object.parent.constructor.name != "Document"){
        return getStyleFullyQualifiedName(object.parent) + "|" + objectName;
    }

    return objectName;
}

function getStyleByFullyQualifiedName(paragraphStyleFQN, document){
    var tmp = paragraphStyleFQN.split("|");

    var object = document;

    for(var i=0; i < (tmp.length - 1); i++){
        if(object.isValid){
            object = object.paragraphStyleGroups.itemByName(tmp[i]);
        }else{
            return null;
        }
    }

    if(!object.isValid){
        return null;
    }

    object = object.paragraphStyles.itemByName(tmp[(tmp.length - 1)]);

    if(!object.isValid){
        return null;
    }

    return object;
}

//Assuming you have a style "Heading 1" under group "Title" and "Center.
//You can retrieve the style object like this.
var styleObject = getStyleByFullyQualifiedName("Title|Center|Heading 1", app.activeDocument);

//You can get the style FQN when you have a style object like this.
var styleFQN = getStyleFullyQualifiedName(styleObject);

alert("Valid: " + styleObject.isValid + " styleFQN: " + styleFQN);

OTHER TIPS

This should do it. Have a look at the function get_style.

update: use id instead of names

// written for
// http://stackoverflow.com/questions/19302941/how-to-reference-styles-properly-in-indesign-scripting
// author: @fabiantheblind
// license: wtfpl http://www.wtfpl.net
main();
function main(){
var doc = app.documents.add();// add a document

// create some styles
doc.paragraphStyles.add({name:"one"});
doc.paragraphStyles.add({name:"two"});
doc.paragraphStyles.add({name:"three"});
// add a group
var grp1 = doc.paragraphStyleGroups.add({name:"grp1"});
// add a style in the group
grp1.paragraphStyles.add({name:"four"});
// add a group in the group
var grp2 = grp1.paragraphStyleGroups.add({name:"grp2"});
// add a style in the group in the group
var parstylefive = grp2.paragraphStyles.add({name:"five"});
var fiveid = parstylefive.id;

var pg = doc.pages[0];// first page in new doc
var tf=  pg.textFrames.add({geometricBounds:[0,0,100,100]});// add a textframe
    tf.contents = TextFrameContents.PLACEHOLDER_TEXT;// add some cintent


var astyle = get_style(doc, fiveid);//get a style by name

    if(astyle === null){
        // error
          alert("There is no style with that name");
        }else{
          // woohoo! \o/
          tf.paragraphs.everyItem().appliedParagraphStyle = astyle;
        }

}

/**
 * This loops through all paragraph styles and returns one by his name
 *  could be rewritten for allCharacterStyles, allObjectStyles etc
 */
function get_style(d, id){
    var thestyle = null;// result

    for(var i = 0; i <d.allParagraphStyles.length;i++ ){

        var onestyle = d.allParagraphStyles[i]; // isolate
        if(id === onestyle.id){
            thestyle = onestyle;// found it
            } // end of check
        } // end of loop i
    // if we did not finds it we return null else Object ParagraphStyle
    return thestyle;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top