Script to add a Paragraph Style to text
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to add a Paragraph Style to text
- This topic has 8 replies, 3 voices, and was last updated 4 years, 10 months ago by Theunis De Jong.
-
AuthorPosts
-
-
May 31, 2019 at 3:25 pm #116968Year 1917Member
Hi
is there a script that would add a certain paragraph style to selected text?
thanks
-
May 31, 2019 at 6:37 pm #116976Kal StarkisMember
Why do you need a script to do this, when it’s one click (or keyboard shortcut) to apply a style to selected text the usual way?
-
June 1, 2019 at 6:53 pm #116991Year 1917Member
thanks kal!
you cannot set a shortcut only with a numpad.
-
June 4, 2019 at 6:57 pm #117046Kal StarkisMember
Well yes, but Adobe also calls it a ‘shortcut’. :-) I always give my styles these shortcuts, and have done for so many years, I forget that not everyone works with an extended keyboard.
-
June 5, 2019 at 11:38 am #117056Year 1917Member
does any one have a script?
-
June 5, 2019 at 6:19 pm #117059Kal StarkisMember
The following one-liner will do what you’re asking:
app.selection[0].applyParagraphStyle(app.documents[0].paragraphStyles.itemByName("My Paragraph Style"));
But I’m still at a loss as to how this is going to save you any work, since running the script isn’t any easier than clicking on the style in the Paragraph Styles panel?
-
June 6, 2019 at 4:59 pm #117078Year 1917Member
thanks
i will add a shortcut to the script.
-
June 6, 2019 at 7:11 pm #117079Kal StarkisMember
Ah I see. No worries.
-
June 7, 2019 at 4:03 am #117083Theunis De JongMember
Here is a useful variant: save this single line as “apply-YourStyle.jsx” and it will apply the style “YourStyle”. Save it as “apply-somethingElse.jsx” and it will apply “somethingElse” – the name of the style gets taken from the script name! So you can copy the script as many times as you like, and you only have to change its file name.
app.selection[0].applyParagraphStyle(app.documents[0].paragraphStyles.itemByName(app.activeScript.name.match(/apply-(.+)\.jsx?/)[1].replace(/%20/g, ' ')));
Do note, however, that this only works with very basic style names, containing simple filename characters only. That is, you cannot make it work with an overly fancy style name such as “Header 1 + line above (when not at top of page)”, nor does it work with Style Groups. Having spaces in the style name should be okay, though.
-
-
AuthorPosts
- You must be logged in to reply to this topic.