Add paragraph style only to a paragraph with 2 lines or more.

Home Forums General InDesign Topics Add paragraph style only to a paragraph with 2 lines or more.

This topic contains 16 replies, has 6 voices, and was last updated by  Year 1917 7 hours, 47 minutes ago.

  • Author
    Posts
  • #100939

    Year 1917
    Member

    hi

    is it possible to make a paragraph style only to paragraphs with 2 lines or more?

    (example: i want the first word of paragraphs with 2 lines or more to have a certain effect (bold etc.))

    thanks

  • #100940

    You can apply a character style to the first 2 lines and then change the character style using nested style by lines, inside the paragraph style options.

  • #100989

    Year 1917
    Member

    so i have to do all two line paragraphs manually?

  • #100991

    Aaron Troia
    Member

    I don’t think InDesign get’s that specific with their nested styles. I also don’t think there’s a native way to tell InDesign “Apply this style only if the paragraph has greater than or equal to 2 lines.” So yes you will probably have to do them manually, or you might have to look into having someone write a script for something like that.

  • #100992

    Year 1917
    Member

    is there someone who knows how to write such a kind of script?

    thanks

  • #100994

    Aaron Troia
    Member

    I don’t personally know anyone, but I would ask in the InDesign Add-ons (Scripts, Scripting, and Plug-ins) section of the forum.

  • #100998

    Vinny 38
    Member

    This forum is a goldmine to get exercises for scripting ^^
    Try this script and tell me if it works for you:
    Please note:
    – select a text frame, or place the cursor in order to apply the script to the whole story
    – an existing character style called “bold” must exist. You can change the name on line 37 (var myCSName = “bold”;)
    – script will clear all paragraphs overrides in the selected story, so be cautious
    – if you don’t use the download link below and copy/paste your code, please replace opening/closing quotation marks by straight one.
    here’s the download link: https://wetransfer.com/downloads/2887b2f63a8f8b3b0fd0adad10e99e7f20180116105700/19df9614bd16a55c5faf1d7bcc0eef8120180116105700/e08e08

    and here’s the javascript:

    //Written by Vinny
    // Feel free to modify and use

    main();

    function main() {
    //Make certain that user interaction (display of dialogs, etc.) is turned on.
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
    if (app.documents.length != 0) {
    if (app.selection.length == 1) {
    switch (app.selection[0].constructor.name) {
    case “Text”:
    case “TextFrame”:
    case “InsertionPoint”:
    case “Character”:
    case “Word”:
    case “Line”:
    case “TextStyleRange”:
    case “Paragraph”:
    case “TextColumn”:
    CheckifCharStyleExists();
    break;
    default:
    alert(“The selected object is not a text object. Select some text and try again.”);
    break;
    }
    } else {
    alert(“Please select some text and try again.”);
    }
    } else {
    alert(“No documents are open. Please open a document, select some text, and try again.”);
    }

    function CheckifCharStyleExists() {
    var myDocument = app.documents.item(0);
    var myParagraphs = app.selection[0].parentStory.paragraphs;
    var myCSName = “bold”;
    var myCharacterStyle = myDocument.characterStyles.item(myCSName);

    if (myDocument.characterStyles.item(myCSName) == null) {
    alert(“\””+myCSName+”\” character style does not exist. Please create it.”);
    } else {
    AddNestedStyle();
    }

    function AddNestedStyle() {
    app.selection[0].parentStory.clearOverrides(OverrideType.PARAGRAPH_ONLY);

    for (i = 0; i < myParagraphs.length; i++) {
    if (myParagraphs[i].lines.length > 1) {
    var myNestedStyle = myParagraphs[i].nestedStyles.add({
    appliedCharacterStyle: myCharacterStyle,
    delimiter: NestedStyleDelimiters.ANY_WORD,
    inclusive: true,
    repetition: 2
    });
    }
    }
    }
    }
    }

  • #100999

    Hi,

    Even if it’s not the way I’ld choose, it could of course be done natively into InDesign!

    I let the curious guys search a little! …

    (^/)

  • #101000

    Salut Vincent,

    2 lignes de code suffiraient, mais apparemment tu as choisi la version longue [non testée] !

    (^/)

  • #101001

    Vinny 38
    Member

    Salut Michel et… bonne année ! ^^
    Je te crois sur parole, mais je suis en plein apprentissage du scripting alors j’ai sûrement tendance à être très verbeux. N’hésite pas à partager tes deux lignes, ainsi après avoir été mon Maître es GREP, tu pourras aussi devenir mon Maître es Script ^^
    Quant à la possibilité de faire ça nativement, j’avoue je sèche. Utiliser Grep pour compter le nombre de caractères avant que ça “coupe” m’est bien venu à l’esprit, mais c’est évidemment très peu précis et donc à exclure. Un indice ?

  • #101002

    Bonne Année à toi aussi (ainsi qu’à tous ceux qui ne me l’ont pas encore souhaitée … et ils sont pléthore !) !

    Pour ce qui est des 2 lignes de code, elles sont depuis 2 heures sur l’autre section de ce forum [l’auteur a suivi les conseils d’Aaron] !

    Pour ce qui est de la solution native, InDesign n’est peut-être pas très fort en calcul et c’est vrai qu’il a du mal à compter les lignes, mais il peut le faire ! … avec les “Styles de lignes imbriqués”. Après, c’est un jeu d’enfant avec un F/R Grep dont voici le code : .$

    (^/)

  • #101003

    Ce code Grep est … plus intéressant : .$\r?

    [Grep est rempli de paradoxes !]

    (^/)

  • #101017

    Masood Ahmad
    Member

    English please………

    This post is a duplicate entry here on InDesign Secrets. Obi-wan have already given a code to achieve this:

    Add paragraph style only to a paragraph with 2 lines or more.

  • #101025

    Year 1917
    Member

    Thanks for the script

    But I have a few comments:

    1) The script does each paragraph as a separate action so I can’t do a CTRL Z, can you adjust the script that it adds the style as 1 action.

    2) It does not add a character style it just adds the effect (I can’t highlight it and change it to a different style after using the script, nothing will happen)

    3) It applies the effect to all paragraphs, can you adjust so it asks on which paragraph style you want to add the effect.

    4) It makes an override on the paragraph style.

    5) Is it possible to create this effect via the nested styles in the paragraph style dialog.

    Also is it possible in GREP format (I want to place a anchored object via GREP before the second word of all paragraphs with 2 lines or more and add a bold effect to the first word (this is necessary for Hebrew typesetting to create drop words))

  • #101026

    Year 1917
    Member

    i mean Vinny 38 script

  • #101033

    Vinny 38
    Member

    Year1917…
    When “a few comments” happen to be a list of very specific requests, I guess it’s time to think about hiring a professional scripter.
    You can ask obiwan, I’m sure he’ll be glad to help.
    All the best

  • #101037

    Year 1917
    Member

    what do you mean vinny 38 by:

    if you don’t use the download link below and copy/paste your code, please replace opening/closing quotation marks by straight one.

    can you explain better please?

You must be logged in to reply to this topic.