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

Move pstyle to specific location in group

Community Beginner ,
Oct 07, 2018 Oct 07, 2018

Copy link to clipboard

Copied

Hi guys,

I'm struggling with something that should be straightforward.
I want to move a paragraph style to a position BEFORE or AFTER another paragraph style.

The target location could be in a nested group or not.

The documentation explicitly says that I can use a paragraph style as a reference, yet I keep getting "Invalid value for parameter 'reference' of method 'move'. Expected..., but received ParagraphStyle."

So, to test, I want to move P3 to AFTER P2:

movePStyle.PNG

The code

//IDs got from a loop via app.activeDocument.allParagraphStyles

var P2 = app.activeDocument.paragraphStyles.itemByID(222);

var P3 = app.activeDocument.paragraphStyles.itemByID(223);

P3.move(LocationOptions.AFTER, P2);

Quite stuck here : /

Thanks in advance!

Gus

TOPICS
Scripting

Views

497

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 07, 2018 Oct 07, 2018

Something like that should work in this special case:

var doc = app.documents[0];

var pStyles = doc.allParagraphStyles;

var p2, p3;

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

{

    if( pStyles.name == "P2" ){ p2 = pStyles };

    if( pStyles.name == "P3" ){ p3 = pStyles };

};

p2.move( LocationOptions.AT_BEGINNING, p3.parent );

p2.move( LocationOptions.AFTER, p3 );

One should test first if parent of p2 and parent of p3 is the same, then a simple move() like in line 12 of my snippet is possible right from the start.

Re

...

Votes

Translate

Translate
Community Expert ,
Oct 07, 2018 Oct 07, 2018

Copy link to clipboard

Copied

The reference of the P2 object that you obtain is not valid. The paragraphstyles present in a group are not present in the paragraphStyles collection. You can verify it by checking the value of P3.isValid, it will be false. The paragraphStyles collection contains just the paragraph styles that are without a group, so in your case it would be [No Paragraph Style], [Basic ParagraphStyle] and P3

What you should do is get the valid reference for your paragraph style P2 by either iterating allParagraphStyle and matching the name or traversing the ParagraphStyleGroups which in turn can contain ParagraphStyleGroups or ParagraphStyles.

-Manan

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 07, 2018 Oct 07, 2018

Copy link to clipboard

Copied

Hi Manan,

in the script's comment Gus is saying that the paragraph style's ID is from a loop on the array of allParagraphStyles.

So basically this should work.

Just tested this where I used the index of the allParagraphStyles array to identify the styles. Did not work, if the two styles are in different style groups. If the styles are in the same group, there's no problem.

So the solution would be to move P3 to the same style group first and then to move after P2 if necessary.

Regards,
Uwe

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 07, 2018 Oct 07, 2018

Copy link to clipboard

Copied

Something like that should work in this special case:

var doc = app.documents[0];

var pStyles = doc.allParagraphStyles;

var p2, p3;

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

{

    if( pStyles.name == "P2" ){ p2 = pStyles };

    if( pStyles.name == "P3" ){ p3 = pStyles };

};

p2.move( LocationOptions.AT_BEGINNING, p3.parent );

p2.move( LocationOptions.AFTER, p3 );

One should test first if parent of p2 and parent of p3 is the same, then a simple move() like in line 12 of my snippet is possible right from the start.

Regards,
Uwe

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 08, 2018 Oct 08, 2018

Copy link to clipboard

Copied

Hi Uwe,

Yes moving the paragraphStyles across groups using move results in a error and as you suggested. We would need to move it into the group first and then place it in the destination group as per the desired order.

However what i had suggested was based on the fact the the following code does not return the name of any paragraphStyle which is inside a group. But when i tried the itemByID api on paragraphStyles using a valid id of a style i get a valid pstyle, this is interesting. Do you also see this on your end? Any ideas why this is happening?

app.activeDocument.paragraphStyles.everyItem().name

-Manan

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 Beginner ,
Oct 08, 2018 Oct 08, 2018

Copy link to clipboard

Copied

Hi Manan, Uwe,

Thank so much for your reply.

The 2-step process suggested by Uwe works perfectly, but it is confusing why we can't complete the whole task in single line – even more considering that ID's documentation explicitly tells us that it is possible...

The collection from allParagraphStyles returns all styles regardless of their groups, while paragraphStyles.everyItem().getElements() returns only the styles on the base level. Now, the styles IDs are unique and should work as a target location. The same reasoning works for textFrames

Many thanks!

Best,

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 08, 2018 Oct 08, 2018

Copy link to clipboard

Copied

LATEST

Hi Manan,

with itemByID() you simply drill through all nested structures in your document.
You can reach everything. Even items in not active states of a multistate object.

That's the beauty of itemByID() 🙂

That the move() command is not working as we expect for nested paragraph styles is a different thing.

An idiosyncratic part of the DOM which is not documented and commented. We have to live with that…

Best,
Uwe

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