So, are you asking for an output that is a fixed phrase + trigger word:
One of Johns Sedan's is <trigger word> .
Yes Exactly. I want an output that is a fixed phrase + trigger word or words. My biggest issues is how to get KM to search through my document to find keywords based on a dictionary I give it.
I already have an AppleScript that I use in Automator. I'm trying to translate it to KM as KM is far superior and not use Automator at all. I figure I can just drop this script in KM, but I dont know how to pass the clipboard into the script. In Automator, whatever I highlight is automatically passed into the script once I activate it.
Here is my Script:
on run {input, parameters}
set theDocument to input
set Dictionary1 to {"Red", "Green", "Blue"}
set Dictionary2 to {"Apples", "Oranges", "Duck"}
set Dictionary3 to {"John", "Mary", "Joe"}
set WordFilter to Dictionary1 & Dictionary2 & Dictionary3
set ResultList to containsItems from WordFilter against theDocument
return input
end run
on containsItems from qList against theText
set foundWords to {}
set positiveHit to false
repeat with aText in theText
repeat with aWord in qList
if every word of aText contains aWord then set end of foundWords to contents of aWord & " "
if every word of aText contains aWord then set positiveHit to true
end repeat
end repeat
if positiveHit then
say "Warning! I Found a positive hit"
display dialog "The Document contains the following words: " & foundWords
else
say "This Document does not contain any of the words listed in the dictionary."
end if
return foundWords
end containsItems
If I gave this script the following input:
Hello everyone. The Cat named Clyde has a Yellow Mustang. However the Duck named John has a Red Hummer. This is just an example of a random paragraph.
The Script would output the following:
"Warning! I found a positive hit."
The Document contains the following words: Red Duck John
OK, this can definitely be done in KM.
Here's an example macro to get you started.
You will need to modify, or use as an example, to develop your own macro.
Let us know if this works for you.
UPDATED: 2020-01-25 22:08 GMT-6 See my below post for a small update to ensure word boundaries.
Note: This differs slightly from your desired output:
But there are two keywords in the Source String: "Red" and "Blue"
So it is not clear how you want to choose which KW to use if there are multiple KWs found. For now, I just took the easy solution of using the first KW found.
Hi JMichaelTX, I have read your many posts and your contributions are invaluable.
This macro works perfectly. The 2nd Version does exactly what I need. Even the formatting details like adding the commas and cleaning up the last one by removing the comma and adding a period are impeccable.
While playing with it, I noticed that the "For Each" Loop was set to check "Each Line" in the Variable.
After playing with it more. I discovered that the macro doesn't not distinguish between whole words and parts of words. If it finds a match, then it pulls out parts of words regardless of context.
In my apple script you could ask it to parse whole words.
Is there a way to do this with KM? I went through all of the conditional options and only two worked "Matches" and "Contains". But they would not do whole words.
To see this, goto the following loop and remove the "Sedan" qualifier.
Then I get the following results based on the given input:
It picked out the word "Red" form "Blurred" and "Blue" from "Blueberry".
Now if I try changing the condition to "Variable Matches"... then It only distinguishes based on Uppercase / Lowecase. But it will not consider the whole word.
Easy fix.
Replace the IF/THEN in the macro with this one:
Two changes to the IF/THEN:
Change "contains" to "matches"
Change the match field to: (?i)\b%Local__KW% Sedan\b
This uses the Regex metacharacter \b to ensure the enclosed string in on a word boundary.
The (?i) makes the comparison case insensitive. Remove it if you want a case sensitive match.
Also, thank you for explaining the Regex syntax to me. <--- You made me aware that there is something else I need to study. Now to search the forum to learn more about Regex and KM
Regular expressions (RegEx or RegExp) are extremely powerful, but have an initial steep learning curve that is often intimidating. But once you get over that initial hump, and you continue to write new RegExp, it will become much easier.
I do all of my RegEx development at this free website: