Delete First Line of a Variable

Hi Guys,

I need help with the following task. I understand how to display delete the variables so you can see the lines of the variables which aren't used in the display text.

My Problem is that I use the Macro every day and every day and because I've set I=0 it always starts with the same URL then.

Anyone has an idea how I repeatedly start the macro every day and it always starts where it ended at the day before?

Remove First Line.kmmacros (2.4 KB)

Macro Image


There are quite a few examples at the end of this thread.

Do you also reset your variable to the entire URL list every time you run the macro, like you do in your example? That might be the actual problem. Otherwise, after you've deleted the first line from your Global variable is should stay deleted, and the next day you'll have a new first line.

No. I think that's the solution.

How do you reset the variable, so I have a new first line in the same variable?

Explain what you actually want to do. Do you want to use, then delete, the first line of a variable every time the macro runs? And what happens when you run out of lines? Or do you want to use the first line and then move it to the end, so you eventually cycle back to the beginning?

Where is this list of URLs coming from? Do you want to be able to edit it, add to it from within KM or from outside, use it in other places?

There are (at least) two different ways of achieving what you want -- keep a record of the last line used so you can go to the next line on the next run, or move/remove lines as you use them. Which you choose will depend on what you are doing.

I'm a newbie. I don't think that far :smiley:

I'm adding the URLs manually. When the list is almost finished I would add new URLs to the variable.

The URLs I used should be deleted completely.

At the moment I have to manually delete the used URLs in the variable and I just need a solution for the moment which deletes the used URLs automatically.

OK, so something like this. Set up a variable in KM's File->Settings->Variables pane called "Global_theURLList" and paste your list into it. Because it is a Global it will persist so each run of the macro will see the list the last one left behind after deleting the top line.

(You could also keep the list as a text file on your disk, updating it with a text editor, and using KM's "Read File" and "Write File" actions to read the data in and then save it out.)

I've been a bit lazy and done the "get and delete the first line" in one operation:


...where the ?s modifier at the start means "let . match new line characters as well". Then it is "From the start of the string Global_theURLList, match every character up to (but not including) the first new line and put that into Local_theURL. Then match everything after the first new line to the end and put that back into Global_theURLList".

The "Trim" removes any white space they may have been added around the URL when you added it to the list, just in case, then we open the URL in your default browser. The last few bits are just reminders if your list is getting short or is empty.

One URL at a Time.kmmacros (6.0 KB)



Oh wow, thanks for your effort! I appreciate that. Let me take a closer look on it.

Just academically, if you wanted to get and delete the first, say, 10 lines of a list, would you repeat that process 10 times or is there a more efficient way you're aware of?

Previously, I've used a for-each with a counter.

  • While the counter is < 10, each line of var1 is appended to var2.
  • Once it's > 10, subsequent lines are appended to var3.
  • Do something with var2.
  • Finally, set var1 to var3.

Not very efficient for long lists, as you can imagine.

Short memory, Neil? Have a look back here…

1 Like

Ahhh... Yes, of course!

Sorry, I forgot that your version used capture groups. Perfect!

I think the "traditional" way would be "repeat x times, copying/appending line 1 to a variable then deleting it" -- so yes, you could use the above in a loop. In KM it might be more efficient to use a loop to grab the first x lines but do the deletion with an "Execute Shell Script" and tail -- you'd be balancing processing time within KM vs the "expense" of shelling, passing a large variable back and forth, etc.

But since I know you like your RegExs :wink: , how about this pattern?


Similar to the last, but matching the first Local_lines lines. Example:

Process and Delete First n Lines.kmmacros|attachment (4.5 KB)


Edit to add:
Should'a remembered that @tiffle had already been there, done that, and made a better job of it!


I tried it and it works! Thank you very much! :slight_smile:

I just have one last question. Im working with Variables i=0 and calc i+1 at the moment to open the URLs in a Loop.

It seems that i dont need the Variable i, even when im not using the for each Loop. Whats the reason behind this?

Never mind my question. Basically its only using the first line every loop and not the whole Variable

1 Like

In order to return both chunks without getting into JSON arrays, I've edited it slightly:

SPLIT into Array at Line Number.kmmacros (19 KB)

Macro screenshot

...and an example caller:

Split at Line Number TEST.kmmacros (21 KB)

Macro screenshot

So, if you wanted to open 10 URLs from a list and then remove them from that list....

Open 10 URLs and Remove from List.kmmacros (23 KB)

Macro screenshot

1 Like

I'd use something more likely to be unique than , as the output separator -- unless you are sure your input won't contain any commas (and I'd still add a "test and error" section for when I forgot that limitation).

Ok in that case, if I'm going to have a subroutine that works for absolutely any input variable, then maybe JSON is called for after all...?

Here's the subroutine:

SPLIT into Array at Line Number.kmmacros (20 KB)

Macro screenshot

...and the URL opener:

Open 10 URLs (JSON Array).kmmacros (21 KB)

Macro screenshot

1 Like

Well, now you've started poking at browser internals you're going to have to get comfortable with JSON -- here's a good place to start :wink:

1 Like

Or maybe not :wink: Instance variables to the rescue!

I'd stayed away from them because making sure we use the same instance names in our main and sub isn't in the spirit of sub-routines. But how about if we pass in the names of the instance variables as parameters, maintaining the "blackbox-iness" of a proper sub-routine?

[SUB] tiffle's REMOVE Top Line(s), modded for instance vars.kmmacros (9.1 KB)

SUB Image

Chopping using Modified Sub.kmmacros (4.6 KB)

Main Image

I've tried to keep to @tiffle's "double underscore" variable naming convention in both macros, for clarity, but may have messed up


Ooh. Can't decide which one I like best. Comes down to which one's easier to recall how to use in the moment. I'll keep both and see how it goes. Thanks!