How to Fetch Dynamic Data of URLs From Multiple Text Files and Perform Actions on Each?

I have instagram reel URLs in 5 different text files. And I want to perform action on each URL. Either by copying URL to the clipboard or setting it as some variable value. (I would be downloading these posts and upload to telegram group)

After the action is performed (or the URL is saved to the clipboard/variable), the URL should be deleted from the file. So, at any given time, some files might be empty and some files might have URLs as I would be constantly appending these files with new URLs.

Once I start the macro, it should automatically check if the file has data and perform action on each URL inside it. When finished, it should move to the next file and so on, until all the URLs from all the files is exhausted. End result of this macro should be, all files are empty.

P.S. - I am using text files (instead of KM variables or dictionary) to store the URLs because I can append URLs to the file from Mac as well as iPhone. And I have 5 text files because each file represents a separate category of instagram reels.

Here's how the text files looks like:

How do I create such a macro?

  • Read your file into a local variable.
  • Use a For Each action with the Lines-in-a-Variable condition.
  • Write over the existing file with text (nothing in the text field) to zero it out.
  • Read a File action
  • For Each action
  • Write to a File action

I've done this kind of thing in the past so I can offer you an approach that hopefully you'll find useful.

There are two different macros involved.

The first is a general purpose subroutine that simply reads lines from files. If you look at it, it contains usage information in the command. You need to download this and install it as you normally would for any macro; there is no need to modify this subroutine at all.

Download Macro(s): [SUB] Get Chunks From List.kmmacros (52 KB)


  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

The second macro is the one that does the hard work. Here it is:

Download Macro(s): Test Process Files.kmmacros (17 KB)


  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Once you've installed this macro you'll need to make some changes to it in order to get it to actually do what you want. All the places you need to make changes are coloured red.

  1. The first red action sets a KM variable called Local__TextFiles You need to put the paths to your text files into this variable. As it stands, I've set it to 5 text files in a folder on my Desktop for testing purposes.
  2. There are 5 other places you need to make changes. At each one you need to change "contains File 1" or "contains File 2" etc to reflect the file names you have chosen for your text files; you might use a simple naming convention like I have or it might be more complicated. IMHO - simple is better!! Also at each one, you need to add some KM macro actions in order to perform whatever you need to do to those URLs - the text of which is held in the variable Local__LineFromFile.

If you need more than 5 text files you can expand the Switch action accordingly. Similarly, if you need lest than 5, just deleted the ones you don't need.

Finally, I haven't set a trigger for this macro - I'll leave that for you to decide.

As usual the @tiffle :clap:
Appreciate you chiming in and put everything in detail.
I went ahead and tried running the macro and nothing happened :thinking:
Let me share what changes I made to the macro you shared.

I updated the file paths

Created this subroutine as a part of a test. Where it'd take URL and merely open in browser.

Here's your macro with changes that I made:
Test Process Files (version2).kmmacros (20.6 KB)

This is how I added my subroutine in the macro.

Here's my subroutine macro:
Action steps subroutine.kmmacros (18.8 KB)

I did not make any changes to the subroutine macro you shared.

Also, when I tried looking up the variable in the macro that you shared, they don't show up in the variable list in KM preferences. I guess it has something to do with them being non-global. I bumped into this answer but if you can further elaborate why they don't show up and what is the difference between global and non-global variable, it would be great. Because I see lot of people here use variables starting with 'Local' when they share a macro. I wonder why.

and it is to do with the difference between local and global variables as you suspected. Coincidentally I just wrote a post about that which you can find here: Get Mouse Position, Run Other Actions, Then Return Mouse Pointer to Same Position - #25 by tiffle which I recommend you read, but the definitive text about variables can of course be found in the KM wiki (aka user manual). Have a look here manual:Variables [Keyboard Maestro Wiki] and specifically in the section titles “Scope”.

Your use of subroutines is the best way of achieving what you’re trying to do, but because my macro uses the local variable Local__LineFromFile its value will only be available inside my macro and not in any other macro, subroutine or submacro. To get round this you need to supply your subroutine with an input parameter that is my local variable. I’m afraid I’m away from my Mac so I can’t show you the approach explicitly and instead I’ll try to describe it: at the top of your subroutine where it says “Executed as a subroutine with parameters” click the little green + button and fill in the field that appears with the name of the local variable Local__LineFromFile . Literally just that.

Then, in the main macro (that’s the one I provided and modified by you) you’ll see a new field has appeared into which you’ll need to literally type this: %Variable%Local__LineFromFile%. Do that for each of the calls to the subroutine.

By doing this you’re effectively passing the value of the local variable “into” the subroutine where it can then be used in your Open URL action.

If you’ve followed these instructions (literally) everything should now work as intended plus you’d have gotten a slightly better understanding of KM variables and subroutines. If it doesn’t work then I won’t now be around until next week :man_shrugging:

As @tiffle says, you need to add a parameter to your sub-routine so you can pass the data (your line of text) to it from your calling action:

When you add the parameter, the "Execute Sub" actions in your calling macro will update to allow you include it there:


Notice that you don't have to use the same variable name in the caller and the sub! This is useful because it's good practice to use meaningful names, but what is meaningful in the caller isn't always in the sub -- this way it's obvious when looking at the caller's actions that you are handling a "line from a file", but when you look at the sub it's clear that it expects to receive "a URL to open".

Note that, unless you are going to be using it in other macros as well, using a sub-routine here is probably an unnecessary expense -- when you've different "Cases" calling the same sub-routine but with different parameters you can usually refactor your code to do everything more efficiently in the one macro.

You elucidate in an amazing way. I wish all the teachers I ever had were as great explainers as you.
I did that and it is now working fine :+1::blush:

That's so kind of you to be so considerate and inform me. But thankfully all is working now :+1::blush:

I did as per the screenshots you sent but got the following errors. then I went ahead and tried what @tiffle said, and it worked.

I must me missing something. I'll try it again tomorrow as it is my bed time now.

Apologies, that was me being stupid. The parameter field is a "text field" so I should, of course, have used a Variable Token and not the plain variable name:


1 Like

That’s very kind of you. I have much experience of imparting knowledge and understanding in my professional life, but not as a teacher so I’m glad I still have that ability :wink:

1 Like