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

Viewing 8 reply threads
  • Author
    Posts
    • #116968
      Year 1917
      Member

      Hi

      is there a script that would add a certain paragraph style to selected text?

      thanks

    • #116976
      Kal Starkis
      Member

      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?

    • #116991
      Year 1917
      Member

      thanks kal!

      you cannot set a shortcut only with a numpad.

    • #117046
      Kal Starkis
      Member

      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.

    • #117056
      Year 1917
      Member

      does any one have a script?

    • #117059
      Kal Starkis
      Member

      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?

    • #117078
      Year 1917
      Member

      thanks

      i will add a shortcut to the script.

    • #117079
      Kal Starkis
      Member

      Ah I see. No worries.

    • #117083

      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.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.
>
Notice: We use cookies on our websites to give you a great online experience. If you keep browsing, we'll assume you're ok with this. For more information, see our privacy policy. By closing this banner, you agree to the use of cookies.I AGREENo