How to click on two or more identical images on the screen?

I want to create a macro that can press two or more identical icons (red box) sequentially on the screen using the “Click at Found Image” action.

If this is possible, I also want to select an area on the screen first, so that it only clicks the icons within the area I selected.

Is this something KM can achieve?

I don't see why not. In fact, it can be done in a macro with one action, at least in a basic implementation. Just create a macro that triggers on a hotkey that clicks the mouse on the image. You didn't show what the image looks like when it is "selected" so I can't show you how I would do it. Hopefully the image looks different when it is selected. Can you check into that and report back here?

Using a second action, you can have it click on all those images/icons, by using a Found Image Loop.

There might also be even better ways to do this using Javascript or Press Button actions in KM. That would require a little more investigation. I'm just giving you a quick/simple solution.

This is what happens when I “select“ the image. A sort of dropdown menu pops up, I would select “download” and the dropdown menu will disappear afterward. And the image will look the same as before I select the image (I suppose that would make it harder to create such macro?)

Yes I tried using For loop to do this, but I think because the image does not change after clicking on it, so it kept repeating itself over and over until I canceled the macro manually. Also, it doesn’t click on all the identical images somehow

You've got the idea. But worse, I think you were saying that if you selected two icons, only one menu shows up. This is even a bigger problem. Maybe we can't use the Find Image method then, at least no in conjunction with selected text. If you want to abandon the selected text idea, it's still possible. You For Loop has a bug. You are searching for the image a second time (in your Move action) after the first search (in the For Each action). Even worse, you are using different fuzz factors in your search. The correct way to do it is for the Move action to not repeat the search, but instead use the result of the For search by using the contents of the %FoundImage% token. The first action inside your loop should be a Set Variable to Text action and you should set the variable to %FoundImage%. Once you fix these issues, that may fix your other problems, but in any case fix them and then report back.

If we still can't fix the Find Image approach, we can always consider the Press Button approach or the Javascript approach.

Yes, only one menu shows up at a time

Unfortunately, I must select the text (“an item in the drop down menu“) after clicking on the image.

What should come after the “set variable to text“ action? I need an action that clicks on the image and the only way I know how is the “click at found image” action, but you said I shouldn’t use that action a second time in the for loop.

For the “press button” approach, it requires a name. And that image / button does not have a name so i can’t use this method?

Maybe this is my only hope, don’t know anything about javascript tho :smiling_face_with_tear:

Your statements are correct. Once you have saved the token into a variable (as you did, using the variable "icon") then you should use the following action:

It doesn't sounds like you've gone through the proper steps of finding the name for a button on the screen. The instructions for finding the names of buttons are on this page. Read this page, and see if you can follow the instructions. Then ask further questions.

There are people on this site who know and love Javascript and will help you with it. I'm not able to help much with it. It may be we will be forced to go that route. But try each of my ideas above first.

Even so, if you click on the download button in that menu while having multiple icons selected, does it download each of the selected items, or just one? If the former, then that's half the problem solved right there.

OK got it! This actually worked! No need for the other methods! Thank you!

OK I definitely have some questions about it! I will ask on a separate post in the future if I can’t figure it out after some more research.

I think I misled you. I cannot “select” multiple icons at the same time. When I click on the icon, a drop-down menu pops up immediately below that icon. If I press another icon, the previous drop-down menu disappear and a new drop-down menu pops up below the icon I just pressed next…If that makes sense. Anyway, it worked! So it’s all good!

If I may, I have two further enhancements I would like to make.

  1. Right now, It clicks on the images (icons) in a random order. But I would like it to do it sequentially in a top-to-bottom fashion. Is that possible to do?

  2. I want the option to select an area on the screen first, so that it only clicks the icons within the area I selected. I vaguely remember there’s an action that captures an area on the screen, but I cannot remember what action that is. I suppose maybe I could use that action to capture the area and save it as a variable and then somehow incorporate that variable into the macro I have right now? I’m not sure! How should I go about it?

Thx!

Yes! But I can’t explain how while I’m sitting in a restaurant.

No worries! Enjoy your meal :wink:

Sure, this is possible. I use two different ways to solve this. The first way is to use a single action, like this one:

Notice carefully the option "top to bottom" in that loop. Inside the loop, the value LocalImageLocation will be the string equivalent to the result of a single %FoundImage% token value, which includes the x and y positions, among other things. So inside that loop you can move the mouse to the location specified in that variable.

Bear in mind that if the screen changes while this loop is running, you may get unpredictable or invalid results. So another way to solve this, slightly more reliable, would be like this. Notice the use of Topmost in the first action. This will find the topmost icon, process it, and then assuming that the icon has disappeared, the next run of the loop will find the next one that is "topmost" which hopefully means "second from the top" prior to the first one being clicked on. If the icon has NOT disappeared, then we can use the previous solution above.

There are also more solutions, which are more powerful, but I don't think you need them at this time. One of the above solutions will probably suffice.

You asked an additional question, which I will probably answer in a separate Reply.

1 Like

There are probably a dozen ways to solve this. I'm not sure which way you will like the most. KM does not have an action that specifically addresses this problem - perhaps it should. This would be a good thing to request. I would really like that added to KM.

I believe that for you the simplest/best way is to define a hotkey that records the location of the mouse, like this, using a key to save the top left and bottom right corners of the box. You could use two separate macros to make this a bit simpler, but since the two macros are so closely related, I think it makes sense to put them into a single macro.

EDIT: Oops, the tooltip is covering the string "Page Up". Sorry about that.

Once you have this macro installed, you can set the corners of the search area using the two keys indicated above. Is it self explanatory? You still need another action to make use of the data in these variables.

1 Like

Silly me! Should have looked more carefully there’s such an option!

Yup works like a charm. Don’t even need to “set variable to text(%FoundImage%)“ in the first place then!

I tried to replicate what you did here, but it kept looping (clicking on icons from top to bottom then repeat these actions again and again) until I pressed cancel macro manually. Did I do something wrong here?

Perhaps I made the wrong assumption. I assumed that when you clicked on the arrow and chose a menu item that the icon would go away. Now that you mention it's falling, I will re-assess my assumptions and offer a correction. But I won't be able to do that until tomorrow.

I take the blame, but that's what happens when you write code for someone. You make assumptions that may turn out to be incorrect.

Sorry I am stupid and need some guidance on the next step :smiling_face_with_tear:. I am assuming I have to use the “Find Image on Screen” action to save the image as a variable first. Then use an If clause, where if the variables “UpperLeft“ and “LowerRight“ contain the image (variable name of the image), then click on it? The problem is I don’t think it is possible to input two variables into one field (“UpperLeft“ and “LowerRight“) and it’s likely that’s not the way to do it either. So please! Enlighten me.

Really? I thought there’s an action that brings out the crosshair thingy (like the screenshot thing when you press CMD+Shift+4) that allow you to record an area on the screen or something? But for the life of me I can’t find it again or remember the name of that action! If I can find this action, I think it will be a better method for me since pressing two keys and having to move the mouse to two places to record the location is a bit of a hassle. (as oppose to just click→hold & drag → release to record the location)

Nah I should have explain what I wanted more clearly in the first place! The one you showed me earlier works perfectly well, so don’t bother yourself coming up with another way, I took too much of your time already!

This macro and the infinite while loop thingy to monitor the screen 24/7 you taught me previously are a real time safer! Thank you so much!

I don't recall there being a native action in KM that can record a click/drag/release. There are ways to detect mouse clicks in KM, but the problem with that is that the mouse clicks STILL CLICK on the screen, even when they are detected by KM. That's why I'm trying to steer you away from that solution, because I'm fairly sure that you don't want that. KM cannot INTERCEPT mouse clicks, it can only DETECT them. That's a big difference, and that's why I'm trying to turn your rudder to stop going in that direction. [You know what? It would probably be a good idea to ask The Architect for a new action that actually INTERCEPTS mouse action. But that would be difficult for noobs to handle safely.]

There's always a 10% chance I'm wrong here. For example, KM has a very powerful feature called Custom HTML, and it may be possible for that action to INTERCEPT mouse clicks using Javascript.

I'm happy to enlighten you. (Of course you're not stupid.) It's always difficult to know when enlightening becomes insulting, so sometimes I don't enlighten to the correct amount. (And I also like to make my students do a little work themselves, to be honest.)

If you used my macro above to collect data for both variables (UpperLeft and LowerRight) then the following code will find an image within that rectangle:

Personally, as a person who likes to make my code bulletproof, I would add an IF statement around that action, to first validate if the variables "LowerRight" and "UpperLeft" actually exist, and that the UpperLeft variables actually is "Upper" and "Left" of the other variable. Or instead of an IF statement, you could insert an ASSERT action before the Find Image action that looks like this:

Thanks. Positive feedback is always appreciated, and motivates me.

Fair Enough. That makes sense. Also upon further digging, I think I thought such action existed because of the “click” action, where if you choose “Area” then “Get“, it would bring out the crosshair and you can get the 4 ‘coordinates’ that way. And somehow I got it all mixed up. Let’s forget that!

Why do you need the +1 in the third and fourth lines? (LowerRight.x-UpperLeft.x+1 and LowerRight.y-UpperLeft.y+1)

Ah good idea!

Ok here is the next problem. It doesn’t wait for me to press both keys to initiate the actions. I pressed PageUp, then it already jump into action, which means it didn’t click on the images only in a specific area, it clicked on all the images on the screen. I tried to insert a “pause until the key PageDown is down” action but that didn’t work (not shown below). What do I do now??:smiling_face_with_tear:

I'm glad you asked. It's just one pixel, so it's not a big deal, but the reason is that the number of pixels between two fixed values, say 500 and 600, is not 100 but 101, because you count both ends. For another example, the number of pixels from 0 to 10 would be 11. For the same reason, we are living in the 21st century right now even though it's barely the year 2000.

That's because I didn't want you to add your code to the end of this macro. When I wrote earlier, "You still need another action to make use of the data in these variables" what I meant to say was "You need another macro" not "You need another action." Sorry.

Also, and this is a big one, your loop logic is incorrect. The loop has "in all screens" set for the found image search area. That's the place you are supposed to change to "in area". You don't need the Find Image action at all. So (a) delete your Find Image action, and (b) fix the For Loop to use "in area" which is where you put your four expressions. And don't forget to put the Assert and the For Each into its own macro with its own trigger.

Took me a minute to get what you are saying. But I get it now haha thx!

Let me say I’m sorry before I keep going. I tried following the instructions you gave me. But I must have misunderstood something or messed up some basic stuff because I still couldn’t get it to work :smiling_face_with_tear:

Here are the corrections I did:

  • Separate them into two macros; each has its own trigger
  • Put the Assert action before the for loop
  • Delete the Find image action in the For loop
  • Add the Find Image on Screen action in the for loop and use “in area“ (I assume this is what you mean when you say “ fix the For Loop to use in area which is where you put your four expressions“)

Also, do I have to add something to the “New Collection” in the for loop? (since I deleted the Find image action)