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

Script or Plugin to Flag Certain Words

Return to Member Forum

  • Author
    Posts
    • #14394088

      At my firm we have a list of “risk words” that we want to avoid using in our proposal. I’m wondering if there’s an InDesign script or plugin that can easily highlight those words from the list. Or, if there’s a better method. Ideas welcomed. Thanks in advance!

    • #14394090
      Petar Petrenko
      Participant

      You can do it using GREP styles. They are GREP formulas inside paragraph styles.

      • #14394092

        Thank you, Peter. The GREP style can work, however, some words may be able to stay and not be replaced, depending how they are used in a sentence. For example, if one of the risk words is “all” the GREP would be applied to it, however, it might be okay in a certain sentence.

        So, if I create character style to “flag” them and a particular word can stay, it’s tied to the GREP style and I can’t simply apply a “none” character style. I’ll have to think this through some more.

      • #14394093
        Petar Petrenko
        Participant

        Well, in that case first solve the problem with “all” and then remove it from the GREP style list. Do the same with other problematic words.

    • #14394091
      David Blatner
      Keymaster

      GREP styles could work if it’s just a few words… but if you’re looking for a list of 20+ words and phrases, then I think it’s probably better to use a tool like IndexMatic — which is designed to create indexes, but I think you might be able to use it for this, too:

      Building an Index Using Character Styles or External Word List


      or

      IndexMatic3 Review

      • #14394094

        Thanks, David. Yes, it’s about 50 words. I’ll have read about this script to see if it’ll do what I’m hoping for. Ideally, I want InDesign to look for the 50 words of the list and flag them somehow within the document. For example, a risk word like “guarantee” might be problematic in one sentence, but could remain in another, depending on usage. A GREP style could be built into the style and find them, however, I would need to create another character style to override the words that should remain/not changed.

        Again, I’ll take a look at this and/or see if I can come with another automated solution. Thanks again!

    • #14394741
      Peter Kahrel
      Participant

      Marc’s excellent script doesn’t mark up words. But a script to highlight words in a text isn’t too hard.

      First, create a text file that contains all your risk words and save it in the document’s folder under the name risk-words.txt

      Then run the below script. It creates a text condition and applies that condition to all the risk words. The advantage of txt conditions is that they don’t show in print (or PDF), or in InDesign’s Preview mode.

      
      (function () {
        var condition;
        var riskWords;
      
        function getCondition () {
          app.documents[0].conditionalTextPreferences.showConditionIndicators = 
            ConditionIndicatorMode.SHOW_INDICATORS;
          var c = app.documents[0].conditions.item('Risk word');
          if (!c.isValid) {
            c = app.documents[0].conditions.add ({
              name: 'Risk word', 
              indicatorColor: UIColors.YELLOW, 
              indicatorMethod: ConditionIndicatorMethod.useHighlight
            });
          }
          return c;
        }
      
        function highlight (word) {
          app.findTextPreferences.findWhat = word;
          var w = app.documents[0].findText();
          for (var i = w.length-1; i >= 0; i--) {
            w[i].applyConditions (condition);
          }
        }
       
        function getWords () {
          var f = File (app.documents[0].filePath + '/risk-words.txt');
          if (f.exists) {
            f.encoding = ('utf-8');
            f.open('r');
            var w = f.read().split(/[\r]/);
            f.close();
            return w;
          }
          return null;
        }
       
        app.findTextPreferences = app.findChangeTextOptions = null;
        app.findTextPreferences.properties = {
          caseSensitive: false,
          wholeWord: true,
          includeLockedStoriesForFind: false,
        }
       
        condition = getCondition();
        riskWords = getWords();
        if (!riskWords) {
          alert ('Risk words not found');
          exit();
        }
       
        for (var i = riskWords.length-1; i >= 0; i--) {
          highlight (riskWords[i]);
        }
       
      }());
      
    • #14394744
      David Blatner
      Keymaster

      WOW! Thank you, Peter. Great solution to use Conditional Text. I haven’t tried it yet. But for anyone here who does not know about conditional text, here are some resources:

      InDesign How-to: Use Conditional Text


      and

      Unexpected Uses for Conditional Text

    • #14394746

      Nice one Peter!

    • #14394758
      Peter Kahrel
      Participant

      Glad you liked it! Conditional text is very useful, though I never use it as conditional text, only ever to highlight various bits of text. One thing I use it for was mentioned by Matt in the article you mentioned, to highlight references to figures, maps, tables, etc.

    • #14394807
      David Blatner
      Keymaster

      Mike Rankin liked this so much he wrote it up here:

      A Script to Flag Certain Words in InDesign

    • #14394813

      This is awesome… I can’t wait to give it a go. Now that my two-week marathon of project deadlines are completed, I now have the time to circle back to this one. I will for sure send a follow up once I have. Thanks again to everyone, especially Peter Kahrel! And how awesome that it inspired an article by Mike. BTW, Peter… I’m still working through your GREP book. I love learning GREP and finding ways to “work smarter, not harder” in using it.

    • #14394816
      Peter Kahrel
      Participant

      Thanks for the write-up, Mike!

      The script could be improved a bit. If you add and/or remove any words from the word list and run the script again, it doesn’t remove the highlights of the words that are no longer high risk. It’s a simple addition, let me know if you’re interested in the update.

      • #14397749
        Brenda Eggert
        Participant

        I’ve placed the .txt file in my job folder and created a test file, but it doesn’t find any of my risk words. I get an error stating “Object contains no text for find/change.” What could I be doing wrong?

      • #14397821
        Peter Kahrel
        Participant

        I’ve seen this error a while ago, but I can’t remember what it the problem is. And I can’t reproduce it. Could you send me that document?

      • #14397825
        Brenda Eggert
        Participant

        I’d like to send you the file, but I can’t attach anything here and the emails I get are “no-reply”. How can I get it to you?

      • #14397826
        Peter Kahrel
        Participant

        You can send it to me at pkahrel@gmail.com

    • #14394980

      Thanks again, Peter. I was able to experiment with this script today, and it found all the words on my list that existed in the document. However, my only concern is that it wasn’t just flagging the “whole” word only that’s defined in my risk-words.txt file. For instance, “all” it’s flagging: allow, equally, overall, challenged, etc. Or for “full” it’s flagging: successfully, fully, etc. I don’t know scripts (under the hood), so not sure how to modify it, or if can be done. I’ve been trying to get to this point for years, so a working script as is, is great. Thanks

      • #14397830
        Peter Kahrel
        Participant

        Brenda: The problem was an empty line in the text file. The script now deals with that. I updated the post where the script was, see below.
        Note that you need to override the text frame on to a document page (it’s now only on a parent page).

    • #14394993
      Peter Kahrel
      Participant

      Ah — sorry. The script was meant to search whole-word-only, but I made a mistake in that line. Below is the corrected version. It also first removes any highlighting so that you can run the script again against a document with a risk list with some words removed: the highlights on those words is then removed.

      
      (function () {
        var condition;
        var riskWords;
      
        // Remove and recreate the condition if it exists
        // so that words removed from the list are
        // 'unrisked' in the text
      
        function getCondition () {
      
          var c = app.documents[0].conditions.item('Risk word');
          
          if (c.isValid) {
            var p = c.properties;
            // This is quicker than removing the highlights in the text
            c.remove();
            c = app.documents[0].conditions.add(p);
          } else {
            app.documents[0].conditionalTextPreferences.showConditionIndicators = 
              ConditionIndicatorMode.SHOW_INDICATORS;
            c = app.documents[0].conditions.add ({
              name: 'Risk word', 
              indicatorColor: UIColors.YELLOW, 
              indicatorMethod: ConditionIndicatorMethod.useHighlight,
            });
          }
          return c;
        }
      
        function highlight (word) {
          app.findTextPreferences.findWhat = word;
          var w = app.documents[0].findText();
          for (var i = w.length-1; i >= 0; i--) {
            w[i].applyConditions (condition);
          }
        }
      
        function getWords () {
          var f = File (app.documents[0].filePath + '/risk-words.txt');
          if (f.exists) {
            f.encoding = ('utf-8');
            f.open('r');
            var w = f.read().split(/[\r]/);
            f.close();
            return w;
          }
          return null;
        }
      
        app.findTextPreferences = app.findChangeTextOptions = null;
        app.findChangeTextOptions.properties = {
          caseSensitive: false,
          wholeWord: true,
          includeLockedStoriesForFind: false,
        }
      
        condition = getCondition();
        riskWords = getWords();
        if (!riskWords) {
          alert ('Risk words not found');
          exit();
        }
      
        for (var i = riskWords.length-1; i >= 0; i--) {
          highlight (riskWords[i]);
        }
      
      }());
      
      • #14397827
        Peter Kahrel
        Participant

        Updated script to deal with empty lines in the text file.

        (function () {
          var condition;
          var riskWords;
          // Remove and recreate the condition if it exists
          // so that words removed from the list are
          // 'unrisked' in the text
        
          function getCondition () {
        
            var c = app.documents[0].conditions.item('Risk word');
            
            if (c.isValid) {
              var p = c.properties;
              // This is quicker than removing the highlights in the text
              c.remove();
              c = app.documents[0].conditions.add(p);
            } else {
              app.documents[0].conditionalTextPreferences.showConditionIndicators = 
                ConditionIndicatorMode.SHOW_INDICATORS;
              c = app.documents[0].conditions.add ({
                name: 'Risk word', 
                indicatorColor: UIColors.YELLOW, 
                indicatorMethod: ConditionIndicatorMethod.useHighlight,
              });
            }
            return c;
          }
        
          function highlight (word) {
            app.findTextPreferences.findWhat = word;
            var w = app.documents[0].findText();
            for (var i = w.length-1; i >= 0; i--) {
              w[i].applyConditions (condition);
            }
          }
        
          function getWords () {
            var f = File (app.documents[0].filePath + '/risk-words.txt');
            if (f.exists) {
              f.encoding = ('utf-8');
              f.open('r');
              var w = f.read().replace(/+$/,'').split(/[\r]/);
              f.close();
              return w;
            }
            return null;
          }
        
          app.findTextPreferences = app.findChangeTextOptions = null;
          app.findChangeTextOptions.properties = {
            caseSensitive: false,
            wholeWord: true,
            includeLockedStoriesForFind: false,
          }
        
          condition = getCondition();
          riskWords = getWords();
          if (!riskWords) {
            alert ('Risk words not found');
            exit();
          }
        
          for (var i = riskWords.length-1; i >= 0; i--) {
            highlight (riskWords[i]);
          }
        
        }());
    • #14394996

      Again… thank you, Peter. I updated the script and it works perfectly! It now only finds the whole word or phrase. I also tested removing a word from the list that was previously flagged and reran the script. That word was no longer highlighted. What a way to end the week. I appreciate your time and effort.

Viewing 12 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads