Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

swap text with specific paragraph style

  • This topic has 15 replies, 5 voices, and was last updated 9 years ago by J R.

Return to Member Forum

  • Author
    Posts
    • #88177
      Kathy Cote
      Member

      Hi,

      I do not know if this is something possible with Javascript but I would like to swap text with specific paragraph style.

      Example, the first line is red and the second is blue:

      I’am red text. (with paragraph style “Red”)
      I’am blue text. (with paragraph style “Blue”)

      At final, I would like to have this:

      I’am blue text. (with paragraph style “Blue”)
      I’am red text. (with paragraph style “Red”)

      Is that possible ?

      Thanks

    • #88178
      David Blatner
      Keymaster

      You can do that with the Find/Change dialog box.

    • #88180
      Kathy Cote
      Member

      Hi,
      I have bilingual flyers. Sometimes the french text is in first and the English text is after the French. But I have to do another version of this flyer, but with English text in first, and the French after the English.

    • #88230
      Peter Kahrel
      Participant

      David: you can swap the contents of two (consecutive) paragraphs, but not the paragraph objects. So you risk losing all formatting. And you need to reapply the paragraph styles.

      Kathy: You can use this script to swap Red and Blue. Works only to swap Red/Blue, Red/Blue, etc. To change Blue/Red, Blue/Red, exchange ‘Red’ and ‘Blue’ in the script:

      (function () {
        var i;
        var red;
        var next;
        app.findGrepPreferences = null;
        app.findGrepPreferences.appliedParagraphStyle = 'Red';
        red = app.documents[0].findGrep();
        for (i = red.length-1; i >= 0; i--) {
          next = red[i].insertionPoints[-1].paragraphs[0];
          if (next.characters[-1].contents !== '\r') {
            next.insertionPoints[-1].contents = '\r';
          }
          if (next.appliedParagraphStyle.name == 'Blue') {
            red[i].move (LocationOptions.AFTER, next.insertionPoints[-1]);
          }
        }
      }());

      Peter

    • #88365
      Kathy Cote
      Member

      Hi Peter,
      I just want to say thank you. That script is amazing!

    • #88491
      J R
      Member

      Hi I am trying to do something similar. I am working on a directory that has many listings. I need to get rid of inset spacing above any listing directly following a city and listing type. The style created currently has a .9 inset spacing above a new line and I want to set that spacing to 0. Both “City” and “Listing Type” are in all caps, so the only way I can think to do this is to write code that says “any paragraph starting after a paragraph with all caps should have no space” but I can’t seem to get the code correct.

      For example what I have is:

      CITY

      LISTING TYPE

      Company Name
      contact info

      What I want is:
      CITY
      LISTING TYPE
      Company Name
      contact info

      Any help would be appreciated!

    • #88502

      It’s not clear to me, where and how there is space. So please provide a idml file before/after, with some entries, that we can inspect your data.

      Kai

    • #88504
      J R
      Member

      I don’t see a way for me to upload a file, so I’ve provided a link:

      https://www.dropbox.com/s/4h0fa6m99tbtfww/Sample%20File.idml?dl=0

      Sorry if this isn’t the best way, I’m totally new to this.

    • #88508

      Haha, your example doesn’t really help ;-)

      First of all, you should have started a new thread for this, but anyway:

      1. The first paragraph have Basic Paragraph applied, not city …
      2. Why not simply change the styles and remove the space above?
      3. Are you sure, that everything is in All Caps? So, check the icons between city and listing type!

      I assume, that you have places, where space above is needed. But where is your example? What should be changed, and what should not be changed?

      Kai

    • #88509
      J R
      Member

      I knew I should’ve started a new thread but again was having trouble sorting it out. I swear I will be a better poster in the future.

      Having said that, I just created a second example that is clearer. https://www.dropbox.com/s/kvsdiml5vz9un9y/Sample2.indd?dl=0

      All “City” and “Listing Types” are set for All Caps, and they also are the only listing that both include paragraph rules, if that is an easier way to solve it.

      This is a really long listings book (about 400 pages) and most of the time I need the space between each listing, except where shown. I’m working from a template that another designer created, so there are a things I would’ve set up different if starting from scratch, but I’m trying to not have to change everything.

    • #88510

      Your second example is well prepaired, thank you. Then: All Caps is set to ‘City’, but not to ‘Listing’.

      So I decide to compare the acutal applied paragraph style with the style of the previous paragraph.

      Try this one:

      (function () {
        var curDoc = app.documents[0],
        allParas = curDoc.stories.everyItem().paragraphs.everyItem().getElements(),
        p,
        curPara,
        prevPara,
        pStyleName;
      
        for (p = 0; p < allParas.length; p++) {
          curPara = allParas[p];
          prevPara = allParas[p-1];
          pStyleName = curPara.appliedParagraphStyle.name;
          if (pStyleName == "COMPANY NAME" && (prevPara.appliedParagraphStyle.name == "LISTING TYPE" || prevPara.appliedParagraphStyle.name == "Listing type no space")) {
            allParas[p].appliedParagraphStyle = "COMPANY NAME - No space above";
          }
          else if (pStyleName == "LISTING TYPE" && allParas[p-1].appliedParagraphStyle.name == "CITY") {
            allParas[p].appliedParagraphStyle = "Listing type no space";
          }
        }
      }());
      
      
      Kai
    • #88511
      J R
      Member

      When I tried to run the script on my text file I got the following error:

      Error Number: 25
      Error String: Expected: )

      Engine: main
      Line: 9
      Source: for (p = ); p < allParas.length; p++){
      Offending Text: ;

    • #88518

      Did you copy everything from the forum. It works here correct with your example. In line 9 it should be p=0 instead of p=)
      Otherwise your document is needed.

    • #88519
      J R
      Member

      Yes, I did copy everything.

      line 9:
      for (p = 0 ; p < allParas.length; p++) {

    • #88529
      J R
      Member

      Kai, thank you so much for your help! It worked.

      Somehow during the process of copying your code into my text editor all instances of “&” changed to “&”

      Ex:
      i(pStyleName == “COMPANY NAME” &&

      it copied
      (pStyleName == “COMPANY NAME” &&

      Thanks again, this saved me hours of work.

    • #88530
      J R
      Member

      It looks like this forum automatically changes the abbrv for ampersand to &, so to clarify the coding changed from “&” to “& ampersand” when I copied and pasted

Viewing 15 reply threads
  • You must be logged in to reply to this topic.
Forum Ads