Keyboard Maestro 8.2.4 “Getting Only the Top Line of a List” Macro

I'm trying to get ONLY the 1st line of a list from the clipboard and put it to a variable.

I'm not familiar with all the Regular Expressions, but I have an idea putting in the right one, will make it work.

Ideally i'd like to try to avoid the For Each action.

Keyboard Maestro 8.2.4 “Getting top line only of list” Macro

Getting top line only of list.kmmacros (3.4 KB)

This is how I'd do it:

Test get first line.kmmacros (3.8 KB)

Hope that gives you some ideas. (BTW you don't have to use the system clipboard, I just used it for convenience..)

I've managed to figure it out, without using For each.

My bad - I missed that you wanted to avoid the very thing I was recommending :face_with_hand_over_mouth:

I'm not sure why you're using the search and replace action. Here's another approach with an overall simpler regex:

The (.*) works because of the way KM only returns the first match it finds.

1 Like

For the sake of completeness (not to mention keeping up my reputation), I'll offer another alternative.

If you wanted to use a shell script, you could filter the clipboard through the shell command

head -1

Usually head will give you the top 10 lines, but if you add -1 it will limit that to just the 1st line.

1 Like

Awesome thanks. Many ways to skin a cat, they say :slight_smile:

Hey @gideonmurray,

Just to throw in a little variety...  :sunglasses:


Extract First Line From Text v1.00.kmmacros (4.8 KB)

Extract First Line From Text v2.00.kmmacros (5.5 KB)

Extract First Line From Text v3.00.kmmacros (4.8 KB)

Extract First Line From Text v4.00.kmmacros (4.8 KB)

Aren’t the lines an array? And therefore doesn’t taking the zeroth element work?

Or maybe Keyboard Maestro isn’t that sophisticated.

Afraid not, here's a KM array of 3 elements:

<elem 1>,<elem 2>,<elem 3>

The same array as a list:

<elem 1>
<elem 2>
<elem 3>

I'm just the messenger...

1 Like

I think the underlying data type is a string, but there is a quasi-JSON-syntax indexed access, though not exactly zero-based.

if jsArray is a KM variable containing a JSON array (or just comma-delimited data – opening and closing square brackets turn out to be optional), then

%Variable%jsArray[0]% references the array length


%Variable%jsArray[1]% references the first item.

The OP mentioned avoidance of For Each, so might not be keen on a general lines -> JSON array conversion like that below : -)

Array from lines.kmmacros (20.8 KB)

so many great options, thanks :slight_smile: