Using if while multiple conditions need to be met Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
autopuff
Posts: 13
Joined: 29 Jun 2017, 18:55

Using if while multiple conditions need to be met

19 Nov 2017, 03:29

My code works fine but it's super redundant. I need to meet two conditions (A = Window AND B = Image to File) to take a screenshot, or it will do several clicks to meet the 2 conditions. The following is a simplified version of my code:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Is there a way to simplify this? Two conditions are already pretty time-consuming for me to write the code in that fashion, but I might run into situations requiring more than that. I really appreciate anyone who can help.
User avatar
Exaskryz
Posts: 2690
Joined: 17 Oct 2015, 20:28

Re: Using if while multiple conditions need to be met  Topic is solved

19 Nov 2017, 08:29

The only thing I'm seeing is "factor out" the Take the screenshot step. It's common to both second-level if's.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



But isn't that curious? We have two very similar blocks. All of this is common to each other:

Code: [Select all] [Download] GeSHi © Codebox Plus

    {
If B!=Image To File ; this is != or not-equals
{
Several clicks to make B the image to file
Sleep, 100
}
Take the screenshot ; saves you a line here, and an else
}


The only difference is making A = Window, so, what if we just do this?

Code: [Select all] [Download] GeSHi © Codebox Plus

If A != Window
Several clicks to make A = Window
If B!=Image To File ; this is != or not-equals
{
Several clicks to make B the image to file
Sleep, 100
}
Take the screenshot ; saves you a line here, and an else


If the Window and Image to File are correct, then it'll just take the screenshot. If either or both condition(s) is/are not met, it'll complete actions to meet that/those respective condition.
autopuff
Posts: 13
Joined: 29 Jun 2017, 18:55

Re: Using if while multiple conditions need to be met

20 Nov 2017, 15:57

Exaskryz wrote:The only thing I'm seeing is "factor out" the Take the screenshot step. It's common to both second-level if's.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



But isn't that curious? We have two very similar blocks. All of this is common to each other:

Code: [Select all] [Download] GeSHi © Codebox Plus

    {
If B!=Image To File ; this is != or not-equals
{
Several clicks to make B the image to file
Sleep, 100
}
Take the screenshot ; saves you a line here, and an else
}


The only difference is making A = Window, so, what if we just do this?

Code: [Select all] [Download] GeSHi © Codebox Plus

If A != Window
Several clicks to make A = Window
If B!=Image To File ; this is != or not-equals
{
Several clicks to make B the image to file
Sleep, 100
}
Take the screenshot ; saves you a line here, and an else


If the Window and Image to File are correct, then it'll just take the screenshot. If either or both condition(s) is/are not met, it'll complete actions to meet that/those respective condition.

Thank you for your response! It's very easy to follow. When I run the code, I can see the conditions are changed if they are not met, but it doesn't take the screenshot. I use the ControlClick to do that and it works fine in the original code. Is there a reason for that?
User avatar
Exaskryz
Posts: 2690
Joined: 17 Oct 2015, 20:28

Re: Using if while multiple conditions need to be met

20 Nov 2017, 16:35

We may need to see the final code to make sure nothing odd is going on. Do you use return in either of the If blocks? Do you use use an else statement? The final code that I simplified down to didn't use any elses.
Osprey
Posts: 222
Joined: 18 Nov 2017, 05:50

Re: Using if while multiple conditions need to be met

20 Nov 2017, 16:49

The simplest way to simplify is simply (heh) to move the redundant parts out into subroutines, like so...

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


That should do exactly what your old code did, minus the ExitApp that I added so that it doesn't continue on and process BImage unless called. You can replace it with Return or other code that you have.
autopuff
Posts: 13
Joined: 29 Jun 2017, 18:55

Re: Using if while multiple conditions need to be met

20 Nov 2017, 16:50

Exaskryz wrote:We may need to see the final code to make sure nothing odd is going on. Do you use return in either of the If blocks? Do you use use an else statement? The final code that I simplified down to didn't use any elses.

Nevermind. It works just fine now. This thing happened last time when I used ControlClick as well. Thank you very much!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], evilC, Google [Bot], gregster, Odlanir and 38 guests