Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Assigning a paragraph-style in a loop

Engaged ,
Oct 24, 2016 Oct 24, 2016

Hi everybody.

I have a simple task: select some items in a document and assign to the penultimate paragraph a style in a for-loop.

var doc = app.documents; 

var Auswahl = app.selection[0];

var eineAuswahl = app.selection;

var Anzahl = Auswahl.paragraphs.count();

var Durchgaenge = eineAuswahl.length;

for (var x=0; x<Durchgaenge; x++) {

    //alert (eineAuswahl.paragraphs.count());

        var FormGrup_SF_Artikel = app.activeDocument.paragraphStyleGroups.itemByName('SF_Artikel');

        var AbsForm_SF_Art_Tx_2_Abst = FormGrup_SF_Artikel.paragraphStyles.itemByName('SF_Artikel_Text_2_Artikel_mehr Abstand');

        var letztArt = Auswahl.paragraphs.item(-2);       

        letztArt.appliedParagraphStyle = AbsForm_SF_Art_Tx_2_Abst; 

}

After running the script it stops. The error-message ist: "Object does not support the property or method '0'". What does it mean?

When it runs without a loop (assign only to one object), it works:

var FormGrup_SF_Artikel = app.activeDocument.paragraphStyleGroups.itemByName('SF_Artikel');

        var AbsForm_SF_Art_Tx_2_Abst = FormGrup_SF_Artikel.paragraphStyles.itemByName('SF_Artikel_Text_2_Artikel_mehr Abstand');

        var letztArt = Auswahl.paragraphs.item(-2);       

        letztArt.appliedParagraphStyle = AbsForm_SF_Art_Tx_2_Abst; 

TOPICS
Scripting
457
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

People's Champ , Oct 24, 2016 Oct 24, 2016

Again: In line 11 of the first script you have

     var letztArt = Auswahl.paragraphs.item(-2);         

The first time the loop is entered, x = 0.

So you're trying to access this property:

app.selection[0][0]

That property does not exist. Hence the error message.

Translate
People's Champ ,
Oct 24, 2016 Oct 24, 2016

In the first script, you have

letztArt = Auswahl

but

Auswhal = app.selection[0];

so what do you mean by

app.selection[0][0]

when x == 0 ??

Try it without the !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 24, 2016 Oct 24, 2016

I need the var x to change the paragraph in EACH of the selected text frames. If I try without it changes only the first of the selected objects. x stands for "next selected object".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Oct 24, 2016 Oct 24, 2016

Again: In line 11 of the first script you have

     var letztArt = Auswahl.paragraphs.item(-2);         

The first time the loop is entered, x = 0.

So you're trying to access this property:

app.selection[0][0]

That property does not exist. Hence the error message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 24, 2016 Oct 24, 2016
LATEST

Okay. Sorry, now I´ve got it: I have to use

var letztArt = eineAuswahl.paragraphs.item(-2);

Thank you! Now it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Oct 24, 2016 Oct 24, 2016

Hi,

So....:

var letztArt = eineAuswahl.paragraphs.item(-2);

TaW pointed the error

Jarek

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines