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

[JS InDesign CS3] Style groups, begone! (or: How do I take paragraph and character styles out of style groups?)

New Here ,
Oct 28, 2009 Oct 28, 2009

Copy link to clipboard

Copied

Sorry for this question (and my terrible English, by the way), I'm a javascript noob and I know when I've reached my limits.

Well, I'm trying to take a set of paragraph and character styles out of the style groups they are placed in, in hundreds of InDesign documents (that cannot be treated as a book). As far as I've tried (thanks to the invaluable help of previous posts in this forum) I've been able to move a style into a group and change it's position inside the group, inside the root level or even between groups. But it doesn't matter how I try, I don't know which move reference should I try in case I want to take every style out of their style group and place them after their original group folder, at the [Root] style level .

I have tried:

var doc=app.activeDocument;

var pGroups=doc.paragraphStyleGroups;

for (i=pGroups.length-1; i>=0; i--){

     var pStylesInGroup=pGroups.paragraphStyles;

     for (j=pStylesInGroup.length-1; j>=0; j--){

// Here I am, trying to move a style outside the folder that contains it, and failing miserably.

          pStylesInGroup.move (LocationOptions.after, pGroups);

     }

}

It didn't work, the script sent an invalid parameter value in the reference field, so I cant use the group folder itself as reference.

Tried other (obviously wrong) solutions, like use the first available style as reference. Therefore, my second script was

var doc=app.activeDocument;

var pStyles=doc.allParagraphStyles;

var pGroups=doc.paragraphStyleGroups;

for (i=pGroups.length-1; i>=0; i--){

     var pStylesInGroup=pGroups.paragraphStyles;

     for (j=pStylesInGroup.length-1; j>=0; j--){

// Now I try placing the styles after the [Basic Paragraph], that is, the second paragraph style in the document style list

          pStylesInGroup.move (LocationOptions.after, pStyles[1]);

     }

}

It didn't work either, another invalid parameter in the reference field. Similar results with my other attempts (I even tried "[Root]" as literal with similar luck).

So, my question is: Which command (or script, in case my whole approach is utterly wrong) should I use to achieve my goal?

Thanks in advance to whoever decides to spend more than a minute thinking about my humble worries, and my apologies for shamelessly ripping some of your lines of code for my purposes.

TOPICS
Scripting

Views

3.0K

Translate

Translate

Report

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

Community Expert , Oct 29, 2009 Oct 29, 2009

Okay, tried a few things and got really weird results!

1. You can move a style around inside its group.

2. You can move a style out of a group "to" another, but it will appear 'inside' that style. I got my test style as a sub-item of [Basic Paragraph], using index #1. With index #0 ([No Paragraph Style]) InDesign crashed.

3. You can duplicate the style, but then you get a copy in the same group. Still no luck.

4. Finally! What is the parent of a paragraph style?

Document | Application | ParagraphStyl

...

Votes

Translate

Translate
Community Expert ,
Oct 28, 2009 Oct 28, 2009

Copy link to clipboard

Copied

.. my terrible English, by the way ..

What, you're kidding me? Read some of the other messages

You seem to have had the right ideas; well, at least, you mention you tried both ways that popped up in my mind. Perhaps it's just failing because you try to move items around inside doc.allParagraphStyles -- I've had a few nasty collisions with this, as it seems not to be quite the same as "doc.paragraphStyles". This latter one only returns you the "root" styles as a regular array; allParagraphStyles is (I think!) a cheat/kludge kind of thing.

Try your second piece of code with doc.paragraphStyles[0] (or [1]) as reference.

Votes

Translate

Translate

Report

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
New Here ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Thanks for your really fast response. When I posted this message, I believed that it would get lost among the huge mass of similar help requests, but seeing that someone is taking interest is truly a joy.

Well, following your advice I changed my code to this:

var doc=app.activeDocument;

// As you may see, now I'm using 'paragraphStyles' instead of 'allParagraphStyles' due to the fact that the latter is a little tricky

var pStyles=doc.paragraphStyles;

var pGroups=doc.paragraphStyleGroups;

for (i=pGroups.length-1; i>=0; i--){

     var pStylesInGroup=pGroups.paragraphStyles;

     for (j=pStylesInGroup.length-1; j>=0; j--){

// Now I try placing the styles after the [Basic Paragraph], that is, the second paragraph style in the document style list

         pStylesInGroup.move (LocationOptions.after, pStyles[1]);

     }

}

Unfortunately, I got the same error after executing this script. This invalid parameter value is really driving me nuts, I don't understand what I'm doing wrong; InDesign CS3 object model says that you may place a paragraphStyle or paragraphStyleGroup as reference (in this particular case), but the script keeps on telling me that it got an 'Invalid parameter value as reference for the move event. Expected ParagraphStyle, ParagraphStyleGroup, CharacterStyle, yaddayaddayadda... , but only received ParagraphStyle' (sorry if the message isn't exactly the same, but my Extendscript editor is in Spanish). I suppose that trying to move a paragraph style to the character style or object style lists should generate this kind of warning, but in this particular case, I'm really lost.

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Okay, tried a few things and got really weird results!

1. You can move a style around inside its group.

2. You can move a style out of a group "to" another, but it will appear 'inside' that style. I got my test style as a sub-item of [Basic Paragraph], using index #1. With index #0 ([No Paragraph Style]) InDesign crashed.

3. You can duplicate the style, but then you get a copy in the same group. Still no luck.

4. Finally! What is the parent of a paragraph style?

Document | Application | ParagraphStyleGroup

"Application" is easy -- that's when you make a style global. So what's the difference between 'Document' and 'paragraphStyleGroup'? Simple -- well, when you finally get it...

pStylesInGroup.move (LocationOptions.AT_END, doc);

moves the style out of the group and to the end of the list in the document. I don't think it's possible to move it directly to a specific position into the main style list -- you first have to move it out of a group, then move it around in its own list.

Fortunately, it returns its new position as a ParagraphStyle again, so if needed, you can use

newStyle = pStylesInGroup.move (LocationOptions.AT_END, doc);

newStyle.move (LocationOptions.AFTER, pStyles[1]);

-- I didn't really try that out, but it should work.

Votes

Translate

Translate

Report

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
New Here ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

Wow! That really DID work, at last! Mr. Jongware, you're really a lifesaver.

It's weird that you have to aim to the document, instead of a more logical reference in the paragraph style list. But it works, so I wont complain.

Thanks a lot, and kudos to you, sir.

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 29, 2009 Oct 29, 2009

Copy link to clipboard

Copied

I learned something as well: ID is not kidding if it tells you that the parent of a style is a document.

In retrospect, it there is some logic behind it; app.activeDocument.paragraphStyles point to the paragraph styles "in" the document, while app.activeDocument.paragraphStyleGroups[0].paragraphStyles point to the styles in that group. Each one's parent is immediately to the left.

As for the moving around in and out groups, I think it has been designed deliberately that way. You can move all kinds of stuff around, but only with one single reference point -- the list itself (moving up and down) or straight into another hierarchical object. I've seen this behaviour before, trying to move frames around from one layer to another on another page.

Time well spent, after all.

Votes

Translate

Translate

Report

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
Participant ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

I know is ten years before, but the only place I found the solution to do that was here.

Please can you write here the final solution code?

 

I don't get it.

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

Hi Ribnog,

I think, the case is very clear.

 

What did you try code wise?

Perhaps we can help to find the issue. Best also show a screenshot with your paragraph styles and style groups and note what style you try to move to what position.

 

Note: This thread is eleven years old and addressed InDesign CS3 version 5.

Today we have InDesign version 15.

 

What's your specific InDesign version?

What's your code that is not working?

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Participant ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

Hi!

Here are me Style Groups. I want to take off all the styles from the groups, and if is possible delete all empty groups after that.

 

Captura de Tela 2020-10-01 às 16.37.33.png

 

Trying to use the mencioned code give this error:
Captura de Tela 2020-10-01 às 16.44.44.png
I'm using the lastest Indesign version (15.1.2).

What I'm doing wrong?

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

Well. As a first test you could loop the allParagraphStyles array of your document and read out the parent of every style in the array. Also check the name of every parent and every style. Then rearrange one single style and loop the allParagraphStyles array again. Maybe you will then understand how paragraph styles are organized in the InDesign DOM.

 

When you like to move a style out of a group to the root, do like Jongware suggested.

Move it at the end of the document. Only as a second step move it up to a different position in the root.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Participant ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

I understood the concept, but I don't know how to write this, and the codes above don't work to take off styles from the groups.

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 01, 2020 Oct 01, 2020

Copy link to clipboard

Copied

In your error line, you need to refer to the group as pGroups[i] and then after the inner for loop is run, call pGroups[i].remove(); to remove the empty group. 

Votes

Translate

Translate

Report

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
Participant ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

Thank you!

I needed tu change the pStylesInGroup.move to pStylesInGroup[j].move too and the "move" command woks.

 

But I have one more problem, the file has groups inside groups. The script works only on the first groups level. How can I reach the other ones?

Votes

Translate

Translate

Report

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
Participant ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

Captura de Tela 2020-10-02 às 08.02.13.png

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

Hi Ribnog,

go with my approach I outlined below.

Iterate through the allParagraphStyles array of the document.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Participant ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

LATEST

And it works!

Thank you so much for the help!

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 02, 2020 Oct 02, 2020

Copy link to clipboard

Copied

Hi Ribnog,

in your case I would do the following:

 

[1] Loop the allParagraphStyles array of your document.

Leave out the first and the second style that cannot be moved to style groups.

[2] Write the value for id of every visited paragraph style to an array.

[3] Loop that new array of id values and address every paragraph style of your document by its id number.

var myStyleToMove = 
app.documents[0].paragraphStyles.itemByID(/*id number goes here*/);

Further in that loop you first check:

[3.1] Is the parent of that style your document? If yes, loop on, do nothing.

if( myStyleToMove.parent == app.documents[0] ){ continue };

[3.2] Is there a style with the same name already in the root of the styles?

If yes, you first have to rename the style you want to move, because no two styles in the root or in a style group can share the same name. To get something like a unique name I added a date as string to the original name of the style. You could do that differently. That's just a suggestion to differenciate the styles:

if( app.documents[0].paragraphStyles.itemByName( myStyleToMove.name ).isValid )
{ myStyleToMove.name = myStyleToMove.name +"-"+Date.now() };

 

Only then, you move the style in that two-process steps Jongware showed.

 

[4] Finally remove all paragraphStyleGroups in one go with:

app.documents[0].paragraphStyleGroups.everyItem().remove();

 

To accomplish [1] and [2] you need some basic knowledge about building arrays and working with arrays in ExtendScript.

 

Well, did not test this, but it should work.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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