12 Replies Latest reply on May 18, 2008 2:40 AM by dougneale

    [JS] (CS2) How do you insert pseudo tags around italic text

    dougneale
      I need to find all the italic text strings wherever they occur (tables, headers, footers etc) in an InDesign CS2 document and insert a pseudo-xml tag before and after each string (something like before and <\I> after.)

      I'm positive this must be possible but my limited experience has failed me so far.

      Can anyone point me in the right direction please.

      TIA

      Doug Neale
        • 1. Re: [JS] (CS2) How do you insert pseudo tags around italic text
          Level 1
          Do a search for italic (is this applied by character style or by local formatting? Do we have to look for oblique and slanted also?).

          Process the found results backwards; for each, insert the "" at the last insertion point, then insert " " at the first.

          Dave
          • 2. Re: [JS] (CS2) How do you insert pseudo tags around italic text
            dougneale Level 1
            I would expect a fairly high standard of consistency as these are professionally produced documents, but nevertheless one cannot rule out any of the possible ways to produce the italic effect. I guess that means having to do the search for each of these four methods in turn.

            How do you find these different ways of italicising text?
            • 3. Re: [JS] (CS2) How do you insert pseudo tags around italic text
              Level 1
              Ah, I just realized that the forum software ate my tags. I meant do the </I> first and then the <I>. That way, the unprocessed references (which point at indexes in the story) are unaffected by the adding of the characters.

              How far have you gotten with scripting this?

              Dave
              • 4. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                dougneale Level 1
                I have prepared a test document of several pages, containing multiple text boxes and tables, each of which contains mixed styled text. But, I now realise I have to go over the test document again to produce each of the different ways of getting italics.

                I have started writing the script, which, so far, just opens the test document and displays it.

                I played around with the textcount sample for a while but couldn't see how to modify it to do what I wanted.

                I'm afraid I need pointing more firmly in the right direction as I can't see how to find the text areas by style.

                Regards,

                Doug Neale
                • 5. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                  Level 1
                  The fastest way to find text is to use the search feature. Take a look at the first 20 or so messages in this topic:

                  Dave Saunders, "How to begin scripting?" #1, 1 Dec 2005 5:45 am

                  When Diane King joins the conversation we go into how to do searches.

                  Dave
                  • 6. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                    dougneale Level 1
                    Thanks Dave,

                    It will take a while to go through that series of posts, but hopefully the answers will be there somewhere.

                    Regards,

                    Doug Neale
                    • 7. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                      dougneale Level 1
                      I have put together a simple script which just finds italic text runs and puts out an alert which tells me how many it has found. It coped with table text as well as ordinary text boxes but failed to recognise 'slanted' text. Attempts to recognise slanted text have failed.

                      This is where I am now:
                      // make sure we have at least one document open
                      if (app.documents.length == 0) {
                      // warn operator and exit immediately
                      alert("There are no open documents.");
                      exit();
                      }
                      // OK if we get here, we have an open doc
                      myDoc = app.activeDocument;
                      // now refer to the character style we are looking for
                      myCstyle = myDoc.characterStyles.itemByName('Italic');
                      // If we are going to search/change something, we use preferences
                      // So, the first thing we have to do is clear them out;
                      // they're application level properties; see pages 567 and 568:
                      app.findPreferences = null;
                      app.changePreferences = null;
                      // use the search() method to search entire document
                      //myFinds = myDoc.search('', false, false, undefined, {appliedCharacterStyle : myCstyle});
                      myFinds = myDoc.search('', false, false, undefined, {fontStyle : 'Italic'});
                      // myFinds will consist of an array of references into the text of the doc
                      // each reference pointing to a run offset
                      // run the loop backwards so that if we make any changes to the text runs
                      // it won't affect the array of references.
                      if (myFinds.length == 0) {
                      // tell operator we didn't find any italic style text
                      alert("There are no Italic text runs in this document");
                      //exit();
                      }
                      else {
                      alert (myFinds.length + " Italic Text runs found");
                      }
                      for (var j = myFinds.length-1;j>=0;j--){
                      // at each italic text run we want to insert
                      // a pseudo xml tag at beginning and end
                      //myFinds[j].texts[0] is start of italics
                      //myFinds[j].texts[-1] is end of italics
                      }
                      I found that the characterStyle approach gave errors so changed to fontStyle instead.

                      How can I identify the other ways of producing an italic effect?

                      Regards,

                      Doug Neale
                      • 8. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                        Level 1
                        Hi Doug,

                        First, this:

                        //myFinds[j].texts[0] is start of italics
                        //myFinds[j].texts[-1] is end of italics

                        Would be better as:

                        myFinds[j].insertionPoints.item(0); //start of italics
                        myFinds[j].insertionPoints.item(-1); //end of italics

                        Adding the tags is this simple (end tag first):

                        myFinds[j].insertionPoints.item(-1).contents = "<\i>";
                        myFinds[j].insertionPoints.item(0).contents = "<i>";

                        Finding slanted text--when text.skew is not zero, text has been slanted. Unfortunately, what you want to look for is cases where this value is NOT zero, and even InDesign's grep search can't find that (it can apply NOT to character classes, but not to formatting attributes)--or, at least, I can't figure out how to do it. So you have to "roll your own." Something like this:

                        //Given a story "myStory"...
                        for(var myCounter = myStory.textStyleRanges.length -1; myCounter >= 0; myCounter --){
                        if(myStory.textStyleRanges.item(myCounter).skew != 0){
                        //Found some slanted text.
                        myStory.textStyleRanges.item(myCounter).insertionPoints.item(-1).contents = "<\i>";
                        myStory.textStyleRanges.item(myCounter).insertionPoints.item(0).contents = "<i>";
                        }
                        }

                        Thanks,

                        Ole
                        • 9. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                          dougneale Level 1
                          Thanks Olav, that works a treat, all I have to do now is to extend the search for non zero skew into any tables that might be present.

                          BTW Are there any other elements which could contain skewed text, apart from tables, which would not be picked up in the 'story' category?

                          Regards,

                          Doug Neale
                          • 10. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                            Level 1
                            If you do a single search (for each italic variant) of the whole document, you'll get the lot, no matter where they are. Also, a search of a story will find instances inside a table. It'll even find them inside tables nested inside each other.

                            Dave
                            • 11. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                              Level 1
                              Hi Dave,

                              The trouble is that you can't search for all text that does not have skew value of zero. You can only search for specific skew values. The problem here isn't the difference between "Italic," "Oblique," etc., Doug has people who are actually *skewing the text* to create fake italics.

                              Thanks,

                              Ole
                              • 12. Re: [JS] (CS2) How do you insert pseudo tags around italic text
                                dougneale Level 1
                                With many thanks to the experts, I managed to sort out the table slant problem. For the benefit of all our many readers, here is the complete solution tested with a multi page document hosting all sorts of text and tables. It's quite long because I believe in plenty of comments.

                                // make sure we have at least one document open
                                if (app.documents.length == 0) {
                                // warn operator and exit immediately
                                alert("There are no open documents.");
                                exit();
                                }
                                // OK if we get here, we have an open doc
                                myDoc = app.activeDocument;
                                // set up start and end tags
                                startTag = "#.";// should really be " "
                                endTag = "#:";// should really be "<\i>"
                                slantTagCount = 0;// initialise our two counters
                                italicTagCount = 0;

                                // If we are going to search/change something, we use preferences
                                // So, the first thing we have to do is clear them out;
                                // they're application level properties; see pages 567 and 568:
                                app.findPreferences = null;
                                app.changePreferences = null;
                                // use the search() method to search entire document for italic fonts
                                myFinds = myDoc.search('', false, false, undefined, {fontStyle : 'Italic'});

                                if (myFinds.length != 0) {
                                // myFinds will consist of an array of references into the text of the doc
                                // each reference pointing to a run offset
                                // run the loop backwards so that if we make any changes to the text runs
                                // it won't affect the array of references.
                                // now add the italic tags
                                for (var j = myFinds.length-1;j>=0;j--){
                                // at each italic text run we want to insert
                                // a pseudo xml tag at beginning and end
                                // Adding the tags is this simple (end tag first):
                                myFinds[j].insertionPoints.item(-1).contents = endTag;
                                myFinds[j].insertionPoints.item(0).contents = startTag;
                                italicTagCount++;
                                }
                                }
                                // as the italic effect can also be achieved by applying a slant to each character
                                // we need to check the whole document again, but this time we do it story by story
                                // Finding slanted text--when text.skew is not zero, text has been slanted.
                                // Unfortunately, what you want to look for is cases where this value is NOT zero,
                                // and even InDesign's grep search can't find that (it can apply NOT to character classes,
                                // but not to formatting attributes)--or, at least, I can't figure out how to do it.
                                // So you have to "roll your own." Something like this:
                                myStories = myDoc.stories;
                                for (var jj = myStories.length-1; jj >= 0; jj--) {
                                // loop through all the stories in the document
                                myStory = myStories[jj];
                                //Given a story "myStory"...
                                for(var myCounter = myStory.textStyleRanges.length -1; myCounter >= 0; myCounter --){
                                if(myStory.textStyleRanges.item(myCounter).skew != 0){
                                //Found some slanted text.
                                myStory.textStyleRanges.item(myCounter).insertionPoints.item(-1).contents = endTag;
                                myStory.textStyleRanges.item(myCounter).insertionPoints.item(0).contents = startTag;
                                slantTagCount++;
                                }
                                }
                                }
                                // the above procedure does all the italic content in tables as well as ordinary text boxes
                                // but only does skewed (slanted) text in ordinary text boxes.
                                // We need to tackle skewed text in tables separately.
                                for (var i = myStories.length - 1; i >= 0; i--) {
                                // go through every story
                                for (var j = myStories[i].tables.length - 1;j >= 0; j--) {
                                // go through every table in this story
                                for (var k = myStories[i].tables[j].columns.length - 1; k >= 0; k--) {
                                // go through every column in this table
                                for (var m = myStories[i].tables[j].rows.length - 1; m >= 0; m--) {
                                // go through every row in this column to look at each cell
                                myCell = myStories[i].tables[j].columns[k].cells[m];
                                // introduce another variable to make next bit more readable
                                myCellStyle = myCell.textStyleRanges;
                                if (myCellStyle.length !=0) {
                                // go through all the text ranges in this cell
                                for (var myCounter = myCellStyle.length - 1; myCounter >= 0; myCounter --) {
                                // check each range item for skew
                                if (myCellStyle.item(myCounter).skew != 0) {
                                // found some slanted text so tag it
                                myCellStyle.item(myCounter).insertionPoints.item(-1).contents = endTag;
                                myCellStyle.item(myCounter).insertionPoints.item(0).contents = startTag;
                                slantTagCount++;
                                }
                                }
                                }
                                }
                                }
                                }
                                }
                                // tell operator what we did, note that document appearance not updated until
                                // alert has been acknowledged by operator.
                                alert ("Italic Tag Count = "+italicTagCount+"\nSlant Tag Count = "+slantTagCount);

                                Regards,
                                Doug Neale