Irregular Distribution of Items to Run Macro On

I've written hundreds of KM macros. But I'm a little stuck on this one. Maybe someone has some suggestions?

I have to go through a long list of 2000 items on a website. On some of them I want to run a macro. This macro works great. I've fine tuned everything. I manually go through the list and for the ones I have to correct, I hit control-mac-B and it does its thing. The trouble it, it has 25 steps, and takes 30 seconds and I have thousands of them to do.

All I can think of is to create another macro that allows me to manually go through the list and pick "Y" or "N" and send it to an Excel sheet ....

1 Y
2 Y
3 N
4 Y
5 N
6 N
7 N
8 Y
9 Y

... then create a wrapper for my macro that goes through the Excel sheet and runs the macro when it sees a Y, and skips to the next one when it see an N.

Can anyone think of a less clunky way to do this?

What if I went through the list using KM macro record? It would record ...

Type the up arrow
Type the Y
Type the up arrow
Type the Y
Type the up arrow
Type the N

... etc. I thought I could just search and replace, but it doesn't look like there is a 'search and replace' function in KM.


Let's see if I understand your question. What you'd like to do is go through all the items manually, and determine which ones you want to run your macro against. Then you want to go back through the items, and have it automatically run the macros on the items you've selected, so you don't have to sit there and wait for it. Right?

Well, you could make an empty variable, then append either Y or N followed by a newline. This would give you a list, which you could then use "For each item in a list", while going back through the web items.

So you wouldn't be playing back keystrokes - your macro that goes through the web items would just use the answers in the list, instead of asking you what to do.

Let me know if this makes any sense.

Yes, your first paragraph is exactly right. That's what I want to do.

Are you saying I could manually type out the Y/N list into a multi-line variable? Interesting.

I'll most likely have to kill and restart this macro at some point when the internet burps or whatever. Actually, now that you make me think of it, there is no numbering the web page, so I suppose I should probably save off the name if the item too because if I have to restart it will be much easier to find the spot to restart.

So anyway, I really need to just go through them on the web page because there are thousands, and even at 1 second each it's going to take an hour or more.

Okay, so let's say I have this list all done and it's sitting in a text editor or Excel.

1 Y Amber shoes
2 Y sandals
3 N green trainers
4 Y tennis shoes
5 N gold shoes
6 N running shoes
7 N high heels
8 Y nurse shoes
9 Y surfing shoes
999 Y brown dress shoes

I'm looking for the most professional way to run this macro on the Y items?

Should I just tell the macro to go to excel, down arrow, get the cell value, if Variable = 'Y' then run the macro? Actually, I remember doing a macro like this before. I did use Excel to give me values to paste in. Pretty similar, and I remember last time too, thinking "this is really kind of a hack, there's got to be something in KM that can do this better."

No, I'm sayin that you have a macro (with a hotkey) build this variable, but let's hold on a second:

So you're saying that each item has a name? If so, it gets even easier. And forget about a spreadsheet - you can do all this in KM.

So right now, when you're on an item you want to process, you hit a hotkey to run your macro. You'll do the same thing, except you'll launch a different macro which appends the item name to a variable. So this variable will only have the names of the items you want to process, each name on a new line within the variable.

Then you have two options, depending on which works best for you:

  1. You could walk through the item names in the variable, and for each item name, go to that item on the web page and process it.

  2. If it's better to walk through all the items on the web page, then do that, and for each item name, see if it's in the variable and if it is, process the item.

Does that make sense? If so, I can write something to show you exactly how I was thinking it would work.

Hey Cliff,

It's not super clear to me what you're doing, but from the gist I think I'd:

Use BBEdit to create a table in a file saved to disk. (Remember BBEdit is commercial but reverts to a free β€œLite” version after the demo period that is still muy powerful and scriptable.)

1	Amber shoes			Y
2	sandals				Y
3	green trainers		N
4	tennis shoes		Y
5	gold shoes			N
6	running shoes		N
7	high heels			N
8	nurse shoes			Y
9	surfing shoes		Y

You can set up the table in advance, so you don't have to do anything but βŒƒNto move down to the next line and then type y or n.

Placing your yes/no choice in the last column makes it easier and faster to edit.

When you run your macro it can read the file.

Reuse and editing is easy.


What would be the best way for KM to read that info, what commands? Like change focus to BBEdit, and parse.

Maybe the problem is I don't know an easy way to read data from a text file.

So what I have been doing is:

change focus to excel
keystroke down arrow

Is there a more direct way to read data out of a text file?

Unless I'm misunderstanding your use case, you don't need a spreadsheet or BBEdit file.

See if this is anything close to what you're trying to do:

Test.kmmacros (10.0 KB)

"for each item in a collection execute actions"

I've never used this one before.
This is interesting. I'll have to study it a little.


Okay, I think I get it. it asks me X times what the decision is and stores it in a X-line variable. Then it goes through the lines of the variable and executes or doesn't execute based on yes or no.


thanks Dan, I'll give it a whirl

By the way, I'm not saying that you should run your macro in counter-based loop like this. Maybe it's the right way for you, maybe not. But I just wanted to show you how the variable could be used.

Another thought. If there's a unique way to identify each item on your web page, like a unique ProductID or something like that, then instead of storing Yes and No lines, you could store only the ProductID:

So you'd do what you do now - go through each line, and press a hotkey when it's something you want to process. The hotkey would launch a macro that appends the ProductID to the variable. So you'd end up with a variable that has ProductIDs on separate lines.

Then when you do the ForEach on the variable, you could use the ProductID to position yourself on the right product line, then do your 30 second process.

Just another thought. Good luck! :smile:

Hey Cliff,

Dan's method works, but I personally wouldn't want to play with dialogs when I could very quickly populate a table in BBEdit.

It's easier to work with and easier to change and maintain in my opinion.

It's easy to read the file and then work with the lines in it:


1 Like


First off, Dan, your script was very helpful in advancing this project. Thanks again.

So, it looks like the number of items is going to be more like 20,000 - 50,000 instead of 2000 - 3000. And this website with the products on it is flaky. I've been using your script, set at 100. Occasionally the script goes off the rails because one of the responses, like a 'save' will take 15 seconds instead of the usual 2, or the page auto-reloads ever few hours ... and several other traps. I tried setting the pause timers higher and neutralized the keystrokes when possible to prevent catastrophes, but basically I need to sit here and watch it, or it goes off the rails and the macro is basically throwing random keystrokes at the site.

In all my hours of watching it. I realize I really do need feedback. The KM macro needs to know when the page is done saving or when new items have loaded. I'm trying out the 'Find an Image' command. There's no 'loading complete' image or anything though, so it's tricky.

Got any suggestions? Is this a good application of KM, or should I be using Ansible or something else for this?

Glad I could help. Honestly, I don't know how to check for all those things. I don't know what Ansible is, but you probably need some sort of commercial-grade web scraper for what you're trying to do.

Sorry, I meant Selenium, not Ansible. It seems to be a browser automation tool used for testing. I really don't want to jump to a new platform when all I really need is for KM to wait until something is done happening instead of using a high timer for the worst case. Like when I make the changes and hit save. It usually takes about 2 seconds. Once in a while it takes 10 to 15 seconds. If I set the timer at 18, I'm wasting 15 seconds every time, but it fails only very rarely. If I set the timer to wait for 3 seconds only, it will be fine for probably 50 items and then go berserk because the save wasn't done.

I need to figure out how to make KM wait until something happens, like the 'save' finishing, so I don't spend 45 seconds on every item when most of the time it only takes 15 - 20 seconds.

You've got your work cut out for you...

Take a look at this:

Macro: Pause Until Web Page Element Exists [Example]


1 Like