How to filter out specific lines/rows of text in a clipboard?

I am trying to figure out how to filter out specific lines/rows of a text from a list, in order to turn them into individual variables. I will need to do this for each line. The list (which is copied in a named clipboard) contains multiple lines/rows. Each line contains a ‘title’ followed by a colon and then some specific info after the colon. This is how the text is formatted:

Line 1: Text 1
Line 2: Text 2
Line 3: Text 3
Line 4: Text 4

So, for example, I would like to filter the list above to end up with just this for a variable:

Line 2: Text 2

However, I know how to filter words but i can’t seem to find a way to filter LINES of text. If possible, I would like to do the filtering in Keyboard Maestro without having to use a 3rd party program. I am able to use the filter action to count the number of lines, but I can’t figure out how to actually address them individually. The ‘substring of variable or clipboard’ action also seemed promising but I can’t figure out how to select specific lines of a paragraph. Unfortunately my applescript skills are basically non existent, but I’m guessing there is a way?

If anyone could point me in the right direction to filtering out specific lines of a list/paragraph, it would be much appreciated!

I don't know if this is what you had in mind, or if it will work for you, but here is one solution using Dictionaries (KM Wiki)

MACRO: Create Dictionary from Lines of Text [Example]

Please let us know if this works for you and if you have any questions.

Wow, Thank you so much JMichaelTX! I am studying the macro you created (and learning a lot!) but some of it is a little over my head. Specifically, the RegEx portion which I am not at all familiar with. Forgive the redundant question, but is there a simpler method of just separating lines of text using something like the ‘filter’ or ‘search and replace’ action?

What I am trying to accomplish is this:

  1. Separate each line of a multi-line list I receive in an email.

  2. Turn each separated line’s text into it’s own variable

  3. Use those variables to update a webform I use for work.

The problem step is #1, separating each line, which is a necessary step for my application. Currently, I copy/paste the emailed text into an excel sheet, which automatically separates the lines into individual cells…and then I have a Keyboard Maestro macro that copies each of those cells to its own variable. However, I am trying to simplify the process and cut the Excel step completely out. I would love to be able to just copy the text to my system clipboard, and have Keyboard Maestro separate each line. Does this seem possible?

I believe the example macro I provided above is the simplest method to achieve your stated objectives.

1. Separate each line of a multi-line list I receive in an email.

Using the KM For Each action (KM Wiki) with the lines in your multi-line list is the simplest method to access each line separately.

image

2. Turn each separated line’s text into it’s own variable

I have consistently found that RegEx is the simplest (although not necessarily the easiest) method to extract specific data from text, unless the text is in some well-known format, like CSV. From your OP:

Use RegEx

This is a great use case for RegEx, which I used in the above macro:

image

where
Local__Key corresponds to your title
Local__Value corresponds to your "some specific info after the colon"

RegEx:
(?m)^(.+):[ \t]+(.+)

For explanation of this specific RegEx, see regex101: build, test, and debug regex

RegEx is a very powerful and very compact tool, but it does have a significant learning curve. Developing the above RegEx was fairly easy and quick for me, but I've been actively developing RegEx for well over two years now.
Big Tip: I use https://regex101.com to develop and test all of my RegEx.
To learn more about RegEx, see Regular Expressions (KM Wiki)

Use KM Dictionary

Rather than create dynamically named KM Variables (possible, but complex), the easiest method is to assign the data from each line to a KM Dictionary (KM Wiki). You can then access the Dictionary Keys as easy as you can KM Variables.

image

3. Use those variables to update a webform I use for work.

You can use the KM Dictionary Key to get its value to set a web form field, or paste into one. Here's an example of pasting into one:

%Dictionary[myDictTest,EMail]%

image

where
myDictTest is the Dictionary name
EMail is the Key

OR, using the KM Set Browser Form Field (KM Wiki)

image


Questions?

1 Like

Wow, thank you so much JMichaelTX! I really appreciate you walking me through this, it made things much clearer for me. It’s really quite amazing. You just saved me hours of work and a big headache :slight_smile:

1 Like