Copy link to clipboard
Copied
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!
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
PS It works the same with character styles.
Copy link to clipboard
Copied
could you help with a sample script i could copy and paste? ... i'm very new and just piecing together code
thank you!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 ...

Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
awesome! thank you! this was super helpful ... i'm slowing picking these things up now ![]()
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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";
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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"

Copy link to clipboard
Copied
‌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 ...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more