CreativePro Forum
Join our community of graphic designers, publishers, and production artists from around the world. Our members-only forum is a great place to discuss challenges and find solutions!
- You must be logged in to reply to this topic.Login
Script or Plugin to Flag Certain Words
- This topic has 21 replies, 6 voices, and was last updated 1 year, 7 months ago by
Peter Kahrel.
-
AuthorPosts
-
-
July 11, 2023 at 9:37 am #14394088
Candi Crosson
MemberAt 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!
-
July 11, 2023 at 11:49 am #14394090
Petar Petrenko
ParticipantYou can do it using GREP styles. They are GREP formulas inside paragraph styles.
-
July 11, 2023 at 3:34 pm #14394092
Candi Crosson
MemberThank 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.
-
July 11, 2023 at 3:52 pm #14394093
Petar Petrenko
ParticipantWell, 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.
-
-
July 11, 2023 at 12:42 pm #14394091
David Blatner
KeymasterGREP 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-
July 11, 2023 at 3:58 pm #14394094
Candi Crosson
MemberThanks, 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!
-
-
August 3, 2023 at 6:43 am #14394741
Peter Kahrel
ParticipantMarc’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]); } }());
-
August 3, 2023 at 8:46 am #14394744
David Blatner
KeymasterWOW! 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:
and -
August 3, 2023 at 9:31 am #14394746
Anne-Marie Concepcion
MemberNice one Peter!
-
August 3, 2023 at 12:34 pm #14394758
Peter Kahrel
ParticipantGlad 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.
-
August 6, 2023 at 12:04 pm #14394807
David Blatner
KeymasterMike Rankin liked this so much he wrote it up here:
-
August 7, 2023 at 6:31 am #14394813
Candi Crosson
MemberThis 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.
-
August 7, 2023 at 7:02 am #14394816
Peter Kahrel
ParticipantThanks 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.
-
November 30, 2023 at 10:46 am #14397749
Brenda Eggert
ParticipantI’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?
-
November 30, 2023 at 1:53 pm #14397821
Peter Kahrel
ParticipantI’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?
-
November 30, 2023 at 2:20 pm #14397825
Brenda Eggert
ParticipantI’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?
-
November 30, 2023 at 2:22 pm #14397826
Peter Kahrel
ParticipantYou can send it to me at pkahrel@gmail.com
-
-
August 11, 2023 at 7:56 am #14394980
Candi Crosson
MemberThanks 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
-
November 30, 2023 at 3:30 pm #14397830
Peter Kahrel
ParticipantBrenda: 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).
-
-
August 11, 2023 at 11:29 am #14394993
Peter Kahrel
ParticipantAh — 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]); } }());
-
November 30, 2023 at 3:26 pm #14397827
Peter Kahrel
ParticipantUpdated 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]); } }());
-
-
August 11, 2023 at 1:04 pm #14394996
Candi Crosson
MemberAgain… 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.
-
-
AuthorPosts
- The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
