我想知道在InDesign Scripting中引用字符样式,段落样式或任何其他样式的正确方法是什么。
请记住,他们可能属于风格组。
答案 0 :(得分:2)
这应该这样做。看看函数get_style。
// 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;
}
答案 1 :(得分: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);