How to Compare the Clipboard Contents With a Blacklist?

Hi,
I have a list of words and expressions in a file named Blacklist.txt.

What I want KBM to do is to check if the clipboard contains exact matchs that are in this Blacklist file, and notify me (by displaying a message or any other similar way).

Example:
The clipboard content is: Local governments are too poor.
My Blacklist.txt file contains "Local governement".
How can I tell KBM to notify me? Any suggestion?

OK, but I need to clarity the following:

  1. A match is found IF the clipboard CONTAINS the entire phrase on a line in the Blacklist.txt file
  2. Check for match is case insensitive
  3. You must spell the words correctly :wink:, or a match will NOT be found.

Here's an example macro that should get you started.
Please let us know if this answers your question.

###MACRO:   Check Clipboard for Expressions in File [Example]

~~~ VER: 1.0    2017-09-29 ~~~

####DOWNLOAD:
Check Clipboard for Expressions in File [Example].kmmacros (13 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


###ReleaseNotes

Author.@JMichaelTX

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

3 Likes

JMichaelTX… It’s simply amazing!!! Many thanks, you made a happy man today!

1 Like

JMichaelTX, this technique is so useful! Could you please give me a hint on how to use a two dimensional array as a blacklist?

The format of the blacklist could be something like the generic wordlist (glossary) format:

Incorrect word TAB Replacement word

I’d want to adapt your macro in such a way that when the clipboard contains a flagged word from the blacklist, this word is replaced by the correct replacement word on the clipboard.

1 Like

Okay, found this: Proper Syntax for Array of Arrays?

I’ve modified Chris’ script to this:

------------------------------------------------
# Setting up your array for you.
------------------------------------------------
tell application "Keyboard Maestro Engine"
	setvariable "two_D_Array" to text 2 thru -2 of "
WrongOne	RightOne
WrongTwo	RightTwo
WrongThree	RightThree
"
	setvariable "arrayRow" to 3
	setvariable "arrayItem" to 2
	
end tell
------------------------------------------------

set theDelimiter to "	"

tell application "Keyboard Maestro Engine"
	set two_D_Array to getvariable "two_D_Array"
end tell

set two_D_Array to paragraphs of two_D_Array

set AppleScript's text item delimiters to theDelimiter

repeat with i in two_D_Array
	set contents of i to (text items of i)
end repeat

set returnArrayItem to item 2 of item 3 of two_D_Array

------------------------------------------------

In the Result pane of the Script Editor I get “RightThree”, so I guess that the modification is correct.

However, now I’m stuck. Please excuse a very basic question, but how do I access every first item (e.g. WrongOne) via a KM variable, and get the result back to another KM variable?

In other words: Let KM get WrongOne and RightOne (etc.) from the script.

If I learn that, I want to write this:

Let the macro check the clipboard content for every first item of every array line. If any first item is found, the second item of the corresponding array line is taken (and used for a replacement action). That part I should be able to manage :).

I don't think using a two-dimensional array would be the best approach here.
RegEx can do all we need.

This macro should get you started. It is just an example, so you will need to adapt to your specific use case. Please let us know if it answers your question.


Example Results

Using this as the blacklist file:
(Good/Bad phrases separated by TAB character)

you are stupid	you need educating
Incorrect word/phrase	Replacement word/phrase
oh hell	oh darn

###MACRO:   Replace Blacklist Terms on Clipboard with Expressions in File [Example]

~~~ VER: 1.0    2017-09-30 ~~~

####DOWNLOAD:
Replace Blacklist Terms on Clipboard with Expressions in File [Example].kmmacros (16 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Requires

  • KM 8.0.2+
    (but everything can be done in KM 7.3.1+)
  • macOS 10.11.6+ (El Capitan)
    (may run on Yosemite, but no guarantees)

###ReleaseNotes

Author.@JMichaelTX

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • Create a Blacklist File with this format:
    • <Bad Phrase>TAB<Good Phrase>
      • where TAB is the TAB character
        .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • "SET Clipboar for TESTING..." -- Disable for Production
    • "Check Each Line in BlackList File" -- Change File Path to your BlackList file.
    • ALL Actions that are shown in the magenta color may be changed.

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

2 Likes

Hi JMIchaelTX,
Tested here, works perfectly!

1 Like

Hi JMIchaelTX,

Thank you for your great help!

Can it be that you’ve posted a development version?

This saving capture groups to variables: how elegant and simple!

At the left-hand side your screenshot above, at the right-hand side the downloaded macro:
[problems with uploading screenshot from wifi in train]

Another try to upload the screenshot:

I've now recreated your macro myself and I noticed that I cannot select the Replace command:

Instead, I have Search and Replace:

Is this because I'm still on 7.3.1 (and you on 8.*)?

No, I've probably caught the wrong command. Hmm, let me search ...

Sorry, I should have stated that my macro requires KM 8+.
However, everything I have done can be done in KM 7.3.1.
I will update my post to so state.

Actually my Action is the Search and Replace command, I have just renamed it.
I often rename Actions to reflect better what they do.

It is not a “development” version (there is no such thing AFAIK), but it is just an example.
As I stated in my Release Notes:

Please feel free to ask more questions about my macro and/or this topic.

Thanks for the reply, JMichaelTX, I just updated my KM and reloaded your macro. Both the Regular expressions search and the Replace action is there.

It is not a “development” version (there is no such thing AFAIK), but it is just an example.

Yes, but because quite a lot was missing when I opened your macro in KM 7.3.1., I assumed that it was a previous version. Anyway, all clear now.

I’ll start testing now (and tell you how I modify/use it). Laterrrrr …

1 Like

Okay, it works now. However, it only replaces the first found string. If the clipboard contains other words that needs replacements, these words aren’t caught. I assumed that the loop would take care of this.

So, how can I make sure that the macro only breaks from the loop after the last line of the Blacklist.txt has been checked?

Just delete or disable the "Break from Loop" action:

You will probably also want to move the Display Text action out of the loop.

That simple? Thanks. I came up with adding the line:

LastSearch TAB LastReplacement

To the Blacklist.txt file and added a line to reset the clipboard after every replacement and one to break when the bad phrase was LastSearch.

So this is how I use it:

https://cafetran.freshdesk.com/support/discussions/topics/6000051161
https://www.proz.com/forum/cafetran_support/319151-seals_on_the_beach_having_fun_with_deepls_api_on_your_mac.html

Macro screenshot:




Greetings,

Just for fun I’m going to do this in pure AppleScript.

Run it from the Script Editor.app to test.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/10/16 19:08
# dMod: 2017/10/17 04:31
# Appl: The Clipboard and an AppleScript runner app such as Keyboard Maestro.
# Task: Look for a list of blacklisted text strings on the text in the Clipboard.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Clipboard, @Blacklist, @Strings
------------------------------------------------------------------------------

# Normally I'd read this from a file, but I'll just us a list for this example.
set blackList to text 2 thru -2 of "
black listed 01
Black Listed 02
black listed 03
"

# How to read from a file instead (the file must exist at the appropriate path of course):
# set blackListFile to alias ((path to downloads folder as text) & "Test_Black_List.txt")
# set blackList to read blackListFile as «class utf8»

# Remove any trailing linefeeds:
repeat while last character of blackList is linefeed
   set blackList to text 1 thru -2 of blackList
end repeat

# Convert blacklist text into an AppleScript list:
set blackListed to false
set AppleScript's text item delimiters to linefeed
set blackList to text items of blackList

# Get the Clipboard:
set clipboardText to the clipboard

# Compare the Clipboard text against the blacklist:
considering case
   repeat with listItem in blackList
      if clipboardText contains (listItem as text) then
         set contents of listItem to "[✓] " & contents of listItem
         set blackListed to true
      else
         set contents of listItem to 0
      end if
   end repeat
end considering

# Display blacklist hits:
if blackListed then
   set blackList to text of blackList
   set AppleScript's text item delimiters to linefeed
   set blackList to blackList as text
   set shCMD to "echo " & quoted form of blackList & " | open -f"
   do shell script shCMD
end if

------------------------------------------------------------------------------

-Chris

1 Like

Hey Folks,

Now I’m going to do it with the Satimage.osax.

NOTE — the Satimage.osax MUST be installed for this to work!

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/10/16 19:08
# dMod: 2017/10/17 04:31
# Appl: Satimage.osax
# Task: Look for a list of blacklisted text strings on the text in the Clipboard.
# Libs: None
# Osax: Satimage.osax -- http://tinyurl.com/satimage-osaxen
# Tags: @Applescript, @Script, @Clipboard, @Blacklist, @Strings
------------------------------------------------------------------------------

set blackList to text 2 thru -2 of "
black listed 01
Black Listed 02
black listed 03
"

# The Satimage.osax can read directly from a file if desired.

# Change the text blacklist into an AppleScript list:
set blackList to find text "^\\S+.*" in blackList with regexp, all occurrences and string result

# Turn the blacklist into a regular expression:
set blackList to join blackList using "|"

# Get the Clipboard:
set clipboardText to the clipboard

# Find any blacklisted items in the clipboardText:
set blackListedItems to find text blackList in clipboardText with regexp, all occurrences and string result

# Display the result:
if length of blackListedItems = 0 then
   set shCMD to "echo " & "No blacklisted items were found." & " | open -f"
   do shell script shCMD
else if length of blackListedItems > 0 then
   set blackListedItems to sortlist blackListedItems
   set blackListedItems to join blackListedItems using linefeed
   set shCMD to "echo " & quoted form of blackListedItems & " | open -f"
   do shell script shCMD
end if

------------------------------------------------------------------------------

You can see why I like the Satimage.osax so much — the functional code is down to 3 lines…

-Chris

1 Like

Hey Hans,

Using AppleScript and the Satimage.osax this is very simple:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/10/17 05:30
# dMod: 2017/10/17 05:51
# Appl: Satimage.osax
# Task: Use a two dimensional array of incorrect and correct values for find and replace.
# Libs: None
# Osax: Satimage.osax
# Tags: @Applescript, @Script, @array, @incorrect, @correct, @value, @find, @replace, @Satimage.osax
------------------------------------------------------------------------------

# Must be tab-delimited with 1 or more tabs:
set findReplaceList to "
WrongOne			RightOne
WrongTwo			RightTwo
WrongThree			RightThree
"

set wrongList to find text "^\\S[^\\t]+" in findReplaceList with regexp, all occurrences and string result
set rightList to find text "(?<=[\\t])[^\\t]+" in findReplaceList with regexp, all occurrences and string result

# Using the findReplaceList as ad hoc test data:
set sourceText to findReplaceList

# Make the corrections:
set newText to change wrongList into rightList in sourceText with case sensitive

------------------------------------------------------------------------------

As you can see the Satimage.osax can take an AppleScript list for FIND and another AppleScript list for REPLACE.

This works for both literal text and regular expressions, so it’s very, very handy.

-Chris

1 Like

Great timing, Chris. I was just needing to do exactly that, to replace invalid file name characters with valid ones. Your method makes it very easy. :+1:

1 Like