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?
A match is found IF the clipboard CONTAINS the entire phrase on a line in the Blacklist.txt file
Check for match is case insensitive
You must spell the words correctly , 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 Library Check Clipboard for Expressions in File [Example]
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/2/20b6c2e2ee46a3ffaa3461d1c243cd5b85e8f483.kmmacros">Check Clipboard for Expressions in File [Example].kmmacros</a> (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. 😉
.
* 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.
---
<img src="/uploads/default/original/2X/c/cd678a8b669828b5cedd003963d24cdd240b5917.jpg" width="478" height="2000">
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.
------------------------------------------------
# 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 Library Replace Blacklist Terms on Clipboard with Expressions in File [Example]
####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/2/2767ea1d007c59f9213d48665ec5acea516434fb.kmmacros">Replace Blacklist Terms on Clipboard with Expressions in File [Example].kmmacros</a> (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. 😉
.
* 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.
---
<img src="/uploads/default/original/2X/b/b5e4db8f4dbec39fcb0e3d65ee6ca520a40f6496.jpg" width="491" height="2000">
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 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
------------------------------------------------------------------------------
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…
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.