Skip to main content
Inspiring
January 4, 2012
Question

Can anyone tells me why this fails?

  • January 4, 2012
  • 3 replies
  • 1354 views

Maybe I'm missing something?

var chapterStyles = [

      doc.paragraphStyles.item("ChapterTitle (Chapter)"),

      doc.paragraphStyles.item("FrontBackTitle (Chapter)")

];

alert(chapterStyles[0].name);

If you change the alert to  ChapterStyles[0]  it works... seems like this should be pretty simple.

This topic has been closed for replies.

3 replies

Harbs.
Legend
January 5, 2012

You are not using the correct method of referencing a style within a style group.

It should be something like this:

var chapterStyles = [

  doc.paragraphStyleGroups.item("Chapter").paragraphStyles.item("ChapterTitle"),

  doc.paragraphStyleGroups.item("Chapter").paragraphStyles.item("FrontBackTitle")

];

Harbs

Inspiring
January 5, 2012

Yeah, I just saw that, I was trying to find an easier way of accessing them since there were a ton... came up with this (approximately, excuse any mistakes):

It doesn't seem like you can access the allParagraphStyles array by name. Is there a better way?

var styles = {};

for (var i=0,l=doc.allParagraphStyles.length; i<l; i++) {

     var styleName = doc.allParagraphStyles.name;

     styles[styleName] = doc.allParagraphStyles;

}

styles["Heading1"];

Community Expert
January 6, 2012

@Josh:
Hm. The "name" property of a paragraph style might not be unique if you use paragraph style groups. To identify unique paragraph styles I would store the  "id" of all paragraph styles and use "doc.paragraphStyles.itemByID(Integer)". For already applied paragraph styles you can look for ".appliedParagraphStyle.id".

Example (German InDesign): (I have three paragraph styles with the same name "Absatzformat 1" in several subgroups)

var _d = app.documents[0];

var _allParaStyles = _d.allParagraphStyles;

for(var n=0;n<_allParaStyles.length;n++){

    $.writeln(_allParaStyles.name);

    $.writeln(_allParaStyles.id);

    };

/*

Result:

[Kein Absatzformat]

105

[Einf. Abs.]

108

Absatzformat 1

208

Absatzformat 1

210

Absatzformat 1

213

*/

Uwe

John Hawkinson
Inspiring
January 4, 2012

      doc.paragraphStyles.item("ChapterTitle (Chapter)"),

alert(chapterStyles[0].name);

If you change the alert to  ChapterStyles[0]  it works... seems like this should be pretty simple.

I believe there are several situations where ID can return either a ParagraphStyle object or a string. It sounds like it is returning the string here. I suspect it has something to do with your use of parens, rather than going through the paragraphStyleGroup.

Inspiring
January 4, 2012

When it alerts without the name property it shows the objects like this:

[object Paragraph Style], [object Paragraph Style]

which leads me to believe that the array is holding objects and not strings?

You would think it would either return the object or return the name of the object... very strange

tomaxxi
Inspiring
January 4, 2012

Josh,

Your variable contains invalid references.

If you try this:

alert(chapterStyles[0].isValid);

it will return false because

doc.paragraphStyles.item("ChapterTitle (Chapter)")

is not valid, but it didn't returns error if item is not found.

Hope that helps.

--

Marijan (tomaxxi)

http://tomaxxi.com

Jongware
Community Expert
Community Expert
January 4, 2012

Fascinating ... it gives me an alert that 'doc' is not recognized.

Oh well, other than that it works on CS4 and CS5, Mac OSX. What version & OS are you on?

Inspiring
January 4, 2012

Sorry about that I usually preface my scripts with

var doc = app.activeDocument;

I'm using ID CS5 on snow leopard

Inspiring
January 4, 2012

The styles are grouped btw... hence the ( )