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.
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.
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.
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.
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
Appreciate you chiming in and put everything in detail.
I went ahead and tried running the macro and nothing happened
Let me share what changes I made to the macro you shared.
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.
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
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.
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:
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