1

我想知道在 InDesign Scripting 中引用字符样式、段落样式或任何其他样式的正确方法是什么。

请记住,它们可能属于样式组。

4

2 回答 2

2

这应该这样做。看看函数 get_style。

更新:使用 id 而不是名称

// 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;
    }
于 2013-10-11T09:42:38.160 回答
2

我采取了一种方法,您必须使用我称之为样式的完全限定名称 (FQN),它是所有组的组合和由竖线 (|) 分隔的样式名称

我在我们的代码中实现了以下两个函数。它们可以与所有类型的样式一起使用,只需对 getStyleByFullyQualifiedName() 函数稍作修改即可支持其他 4 种类型。

//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);
于 2014-07-18T18:54:13.433 回答