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

script to replace a Paragraph style with another styles, both within a folder

Community Beginner ,
Mar 03, 2016 Mar 03, 2016

I'm trying to write a script that will delete a paragraph or character style and then replace it with another paragraph or character style, both exist in a folder within those style groups. I've tried so many different scripts and haven't found the right combination to work.

any help would be greatly appreciated!! thank you!

TOPICS
Scripting
2.4K
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 , Mar 03, 2016 Mar 03, 2016

To target a paragraph style that is in a folder, you have to tunnel down to it via style groups, as so:

style1 = app.activeDocument.paragraphStyleGroups.itemByName("Group1").paragraphStyleGroups.itemByName("Group2").paragraphStyles.itemByName("myParagraphStyle");

Then find the second style (the one you want to replace with):

style2 = app.activeDocument.paragraphStyleGroups.itemByName("GroupA").paragraphStyles.itemByName("AnotherParaStyle");

Then simple remove the first style, replacing with the seco

...
Translate
People's Champ ,
Mar 03, 2016 Mar 03, 2016

To target a paragraph style that is in a folder, you have to tunnel down to it via style groups, as so:

style1 = app.activeDocument.paragraphStyleGroups.itemByName("Group1").paragraphStyleGroups.itemByName("Group2").paragraphStyles.itemByName("myParagraphStyle");

Then find the second style (the one you want to replace with):

style2 = app.activeDocument.paragraphStyleGroups.itemByName("GroupA").paragraphStyles.itemByName("AnotherParaStyle");

Then simple remove the first style, replacing with the second:

style1.remove(style2);

HTH,

Ariel

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 ,
Mar 03, 2016 Mar 03, 2016

PS It works the same with character styles.

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

could you help with a sample script i could copy and paste? ... i'm very new and just piecing together code

thank you!

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 ,
Mar 03, 2016 Mar 03, 2016

copy and paste the three lines I posted above (the first is one long line). Together, you've got a script. But obviously replace the various group names and style names with whatever's appropriate for you. If it doesn't work, paste your efforts here, and I'll see what I can do. But best is to try it yourself, first.

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

assuming "group1" and "groupA" is the first level folder, and "group2" is the second level folder

Both styles are in the second level folder, should i include the group2 in the second line of code?

its not working ...

Screen Shot 2016-03-03 at 3.52.41 PM.png

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 ,
Mar 03, 2016 Mar 03, 2016

Yes, you need to drill down to the actual paragraph style via the style groups. In the first line, there is supposed to be a paragraph style called "blank lines 2" inside a folder called "Lines" which is inside a folder called "Body". In the second line, you've got a para style called "blank_line" inside a folder called "Body". Modify as necessary for your particular case, adding groups as needed until you've drilled down to the para style.

But in the 3rd line, use the style2 variable you created! Don't try to use just the name of the style! That defeats all the hard work we've done in line 2. So line 3 should read:

style1.remove(style2);

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

awesome! thank you! this was super helpful ... i'm slowing picking these things up now

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

on another note ... do you know any code that would change the naming of folders? Ive got a script to rename all my styles but I need to fix the name on some group folders themselves too.

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 ,
Mar 03, 2016 Mar 03, 2016

Renaming a group is easy. If you look at the documentation (the DOM), you'll see that a style group has a name property. So you can just change that:

myGroup = app.activeDocument.paragraphStyleGroups.itemByName("SomeName");

myGroup.name = "a different name";

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

You are magnificent!! worked perfect! thank you!

ok last one sorry ...

create a new group

then move a chunk of styles from one group to the new group?

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 ,
Mar 03, 2016 Mar 03, 2016

New group:

myNewGroup = app.activeDocument.paragraphStyleGroups.add();

Move a paragraph style to that group: So, first make a variable that points to a paragraph style, as in the previous posts. Then:

myParaStyle.move(LocationOptions.UNKNOWN, myNewGroup);

(I haven't tested this last line. Perhaps you have to specify a LocationOption -- if so, choose a different one, such as LocationOptions.BEFORE.

Ariel

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016

I can't get it to work. Literally day 6 in writing scripts ... son dont laugh

this is what i have ... suggestions?

i'm trying to move paragraph style "new" from folder "body" into folder "lists"

Screen Shot 2016-03-03 at 6.21.00 PM.png

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
Community Beginner ,
Mar 03, 2016 Mar 03, 2016
LATEST

‌i could also be trying to make things more complicated than necessary, I'm trying to move all the styles that start with "list" into the "Lists" folder ...

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