Search clipboard for text matches, report them along with additional statement

Evening everyone,

Wondering if anyone has a solution to this problem. I’m looking to generate medication instructions for patients before procedures, using their medication list as the reference. These are manually reviewed but having them flagged and default instruction sentences to follow would be supremely helpful. I can get the med list into the system clipboard easily as a starting point. As a disclaimer, none of my example statements below should be followed or taken as medical advice.

Goal (all non-case sensitive):

1/ Analyze a text selection in the system clipboard

2/ Find any matches (regardless of case) to a hard coded reference list (easily user modifiable)

3/ Report matches by typing them each on their own separate line in caps, with a line between

4/ Allow for assigning of a trailing phrase at the end of the reported words

I remember figuring out how to do something like this in Excel (using a cell reference rather than system clipboard), but have no idea how to implement in KM. I provided a few possible text formats.

Example:

Text to be analyzed (Format A):

Tylenol
Advil
Clopidogrel
Apixaban
Metformin

Text to be analyzed (Format B):

Tylenol, Advil, Clopidogrel, Apixaban, Metformin

Text to be analyzed (Format C):

Tylenol; Advil; Clopidogrel; Apixaban; Metformin

Reference list / Reference phrase (Note this will be MUCH longer in actual use, probably a few hundred hard coded drug names, and sentences after. A lot of these sentences will be repetitive, so these could be given a separate reference list).

APIXABAN / Do not take this on the day of the procedure, or the two days before

RIVAROXABAN / Do not take this on the day of the procedure, or the two days before

WARFARIN / Do not take this on the day of the procedure, or the five days before

CLOPIDOGREL / Do not take this on the day of the procedure, or the five days before

PLAVIX / Do not take this on the day of the procedure, or the five days before

METFORMIN / Do not take this on the morning of the procedure

Result:

CLOPIDOGREL - Do not take this on the day of the procedure, or the five days before

APIXABAN - Do not take this on the day of the procedure, or the two days before

METFORMIN Do not take this on the morning of the procedure

This looks like a basic text manipulation macro. Let me just discuss some options.

First, it looks like your input is a bunch of single words. Do medications ever have spaces in their names? Spaces may not add complexity, but I still want to know.

Second, it looks like your meds may be separated by various punctuation or newlines. Those are pretty easy to remove. not a problem.

Third, your result doesn't seem to match any of your inputs. Am I to assume that your input for the result shown was those three drug names that start with a C, A and M?

There are many possible ways to solve simple text manipulations. I can probably solve this in a single action.

Thanks for the reply @Sleepy

I'm not aware of any two-word medications, there are generic and brand names so there are often two names for the same medication (ie Apixaban is the same as Eliquis) but I'd just plan on hard coding both with the corresponding statements. Those two will never appear in the same list (either generic or Brand name, not both). So for these contexts, one word only.

Regarding results - I think I see why this wasn't clear. I only want to give instructions on the medications that need to be changed before the procedure. So in the above example drugs "C" and "A" are blood thinners, and drug "M" is a diabetic med. All need to be modified around surgery. So I guess I could call the reference list, a list of "Bad drugs" to take around surgery, or at least that need modification.

The Tylenol and Advil would not be in the reference list, as it doesn't matter if the patient takes these before the procedure or not (along with the majority of medications).

So in this fake patient that is on: Tylenol, Advil, Clopidogrel, Apixaban, Metformin

I want to tell them to:

  • Not take Clopidogrel for 5 days before the proceudre
  • Not take Apixaban 2 days before procedure
  • Not take Metformin in the morning before they come in for the procedure

Oh, I think I get it now. I need to ponder it a while now. Once I'm sure I fully understand it, I'll decide which approach to take.

My macro is written, and it should work, and it's really short, but I have some sort of syntax error that I'm still getting. Please be patient.

I have a solution that works 100% correctly, but the problem is when I run it in KM, it generates no output. It works only from the shell command line. I'm perplexed - and I don't give up easily. I'm still working in it. If I get totally stuck, I'll post what I have and ask others to tell me what my problem is. AHA! I found a solution, give me a couple of minutes.

Okay, I finally have a solution. At least, it's the solution to the problem that I interpreted from your post. If I've misinterpreted you, you have to let me know.

You have to replace the path with your own path, and the filename below needs to be replaced with the name of the file i that path where your long list of meds is stored. You also have to replace the upper half of this action with a variable that contains your values. Notice that punctuation is ignored.

Isn't it sweet how short this is?

Here're the lines you have to enter and patch up:

cd /Users/username/data
tr ";," "\n" | xargs -I {} sh -c "grep -i \"{}\" medical.txt"

I had meant to put a comma between Advil and Clopi in the example above. Punctuation is treated as newlines.

The problem I was having was not knowing I had to place "sh -c" in the command. To be honest, I'm not sure why that solved my problem. Maybe someone here can tell me.

Now I will get some dinner. I earned it.

Wow that is very concise. Thank you!

I'm a bit of a noob here and can't seem to get it working. I'm sure its on my end...

I made a plain text document medical.txt, and put a few medications, each on their own line.

So the top line I changed to the path containing plain text document medical.txt
cd /(This is the path that contains the folder contains the medical.txt file)

And the 2nd line:
All I did was copy your code, and I named my file medical.txt so it corresponded.

I ran it and it didn't do anything. Am I supposed to edit anything else? Note in the below screen captures my USERNAME is actually written correctly when I attempt to run it.


Since I have no proof that your username is "USERNAME", I can't validate that you entered the first line correctly. Based on the image you provided, the problem seems to be that you didn't use your account's username.

In order to find your username, open a Terminal window, and type: whoami

I don't need to know your username, if that contains personal information, but you need to enter that name into the action's first line. At this moment that's the only possible error that I see. But I'll keep looking while you check on this.

P.S. You didn't put a comma after the word "Advil", which I advised you to do. But that doesn't break anything, it just may not give all the right results.

P.S. I can already see one small way to improve this macro, but we'll deal with that after you get it working.

Yeah I changed the user name to my actual username, confirmed with the terminal command you provided as well.

It doesn't give me a result when I run it (change USERNAME) for my actual username.

Okay, well let me think.

I neglected to tell you to set these flags on the action. It might solve your problem. I'm not sure.

Screen Shot 2022-03-08 at 10.42.41 PM

Does your medical.txt file look anything like this: (This is what mine is set to.)

APIXABAN / Do not take this on the day of the procedure, or the two days before

RIVAROXABAN / Do not take this on the day of the procedure, or the two days before

WARFARIN / Do not take this on the day of the procedure, or the five days before

CLOPIDOGREL / Do not take this on the day of the procedure, or the five days before

PLAVIX / Do not take this on the day of the procedure, or the five days before

METFORMIN / Do not take this on the morning of the procedure

Oh. That was it. Thx! Haha wow. I just listed the meds

As I said... noob hahaha

SUPERB. I have one small change you should make. Let me put it below... in a sec...

tr -d " " | tr ";," "\n" | xargs -I {} sh -c "grep -i \"{}\" medical.txt"

This change (the extra 12 characters at the start) deletes spaces from your short list of medicines. It may be helpful or needed.

Hang on, this website removes backslashes. I have to fix that above. There. I fixed it. This website is great, but if you aren't careful it can remove backslashes.

There are lots of enhancements and changes we can make to this cool macro, but give this a try and see how it works.

I cannot thank you enough for this help - I'll try to implement it tomorrow and test on a bunch of cases. Inputs should be quite consistent so hopefully I don't find a way to break anything hahah.

Alright. I can make improvements and enhancements if needed. Keep in touch.