Fix my script (simple) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Fix my script (simple)

21 Feb 2018, 03:27

Heya can you help me fix my script please? Anything that you feel can be done better or is not done right.

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



I'm having an array of problems. Firstly the imagesearch won't work. I don't know how to write code and syntax so I'm figuring it's a problem there like with the parenthesis an additional not required return or whatever. And most importantly my monitor won't turn off when I added the If and else statement. It worked perfectly fine in itself though just not getting triggered in the first place.

Additionally:
I want a msgbox to pop up when there is an error (when errorlevel is 2) saying "Error" and then the script must immediately exit irrespective of whether I interact with anything
And a msgbox to pop up when there is no image found (when errorlevel is 1) saying "Image is not found" and the script must immediately exit irrespective of whether i interact with anything

I tried msgbox but the script never exited. This is confusing hehe.

Thanks! :superhappy:
Last edited by Nwb on 21 Feb 2018, 03:37, edited 4 times in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 03:29

The reason I put suspend on wheelup and wheeldown so that the hotkey won't trigger twice. Is that possible? I don't even know. Is there a better method?


Also can I make it so that wheelup and wheeldown hotkey lines will trigger only if I keep scrolling up or down for more than a second?
Last edited by Nwb on 21 Feb 2018, 03:38, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 03:30

I don't know how the number values etc work I just took a shot at the dark.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)  Topic is solved

21 Feb 2018, 03:55

There are probably other problems: But in this part (also the next - which never will be reached anyway)

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

If ErrorLevel = 2 
ExitApp ; this line will be executed if Errorlevel is 2
return ; this line will always be executed --> here: end of the autoexecute section
your indentation is misleading. If Errorlevel is 2, the script will exit. But the return is not part of the if-condition (without curly brackets aka "code blocks" only the next line after an if will be executed conditionally) - so, if the script continues, it will just idle and wait for hotkeys, once the return is reached. Apart from that, where should the script return to? So far, there was only linear code execution...

Also, you don't specify the coordinates for the imagesearch - parameters 3-6. You can use the WindowSpy script to determine the coordinates - to find it, right-click on the script icon in the systray while a script is running or look in the Autohotkey install directory.

What about looking at the basics concepts first? We have a beginner tutorial and a section called "Usage and Syntax" in the docs that might help you.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 04:37

So I need to put exitapp and return inside parenthesis? Gotcha.

And yea I don't know how but I apparently confused coordinates to search within for - to retrieved coordinates of the image (variable name).

Is using keywait mousenotches a good idea for the sensitivity part like I mentioned (I wanted the hotkeys for wheel down and up to not trigger with 1notch)?
Also how do I mention mouse notches in keywait if somebody can help me with that

I'm sorry if this is weird for you. I am dyslexic so it is hard for me to read, I still can but I just don't want to haha. I guess I'm lazy. But I try to read as much as I can and understand as much as I can when I can. I don't read in order and I skip a lot of stuff so sometimes I still need help. I promise I will learn one day though!
I am your average ahk newbie. Just.. a tat more cute. ;)
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Fix my script (simple)

21 Feb 2018, 05:03

If (Mod(%Number%, 2) = 0) ; if even
vs
If (Mod(Number, 2) = 0) ; if even
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 05:14

BoBo wrote:If (Mod(%Number%, 2) = 0) ; if even
vs
If (Mod(Number, 2) = 0) ; if even


Oh my god it works flawlessly! Thanks!

Also I don't need the return for If statements right? Or do I?
And what do I put in the image search for it to search the entire screen?
Last edited by Nwb on 21 Feb 2018, 05:30, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 05:29

So I need to put exitapp and return inside parenthesis? Gotcha
Well, if you want more than one conditional line - yes, use code blocks.

Here, my point was rather that your indentation was misleading and more importantly that a return at this point of the script doesn't make much sense:
* If return is not part of the code block, there is nothing to return to and there is code that is cut off from the autoexecute section.
* If it is part of the code block after ExitApp, it won't be executed.

Also I don't need the return for If statements right? Or do I?
No, don't use return with if, here where you have a linear code structure. But, in a function or ina gosub routine, it could still make sense with an if-startement. It is all a matter of code flow and logic...
Last edited by gregster on 21 Feb 2018, 05:37, edited 2 times in total.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 05:32

gregster wrote:
So I need to put exitapp and return inside parenthesis? Gotcha
Well, if you want more than one conditional line - yes, use code blocks.

Here, my point was rather that your indentation was misleading and more importantly that a return at this point of the script doesn't make much sense:
* If return is not part of the code block, there is nothing to return to and there is code that is cut off from the autoexecute section.
* If it is part of the code block after ExitApp, it won't be executed.


I don't understand what you meant by my indentation was misleading. I don't know how to indent :P I didn't know it had a purpose I thought it was just to beautify a script.

Is there a guide for indenting the right way? Even if it is just for beautifying might be cool to know I guess.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 05:34

No, indenting is only to make the code more comprehensible for the reader. I just wasn't sure how you meant it (it looked like you wanted it to be part of the if-statement), because the return didn't make sense here, neither in the if block, nor outside.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 05:36

gregster wrote:No, indenting is only to make the code more comprehensible for the reader. I just wasn't sure how you meant it, because the return didn't make sense here, neither in the if block, nor outside.


Okay that makes sense I guess I was a little confused with that concept. Also what is the default for imagesearch to make it search the entire screen?
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 05:43

Also what is the default for imagesearch to make it search the entire screen?
I haven't used it in ages, but according to the docs, that should do it:

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

CoordMode Pixel  ; Interprets the coordinates below as relative to the screen rather than the active window.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, someimage.bmp
0,0 is the top left corner and the rest is depending on screen resoultion, but there are these built-in variables starting with A_ which will give you the dimensions.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 05:46

Still having problems with image search
What is wrong with this

I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 05:53

Make sure the path to the image file is correct and that the image has a good quality: if you can, don't use lossy jpg, but rather lossless formats like bmp or png.
On the other hand, there is also the *n option to allow color variation, if needed. You could also try

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

If ErrorLevel = 2
msgbox, Image not found on screen
If ErrorLevel = 1
msgbox, There was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
to narrow down the problem.

But there are probably much bigger Imagesearch experts on the forum than I am.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 05:58

gregster wrote:Make sure the path to the image file is correct and that the image has a good quality: if you can, don't use lossy jpg, but rather lossless formats like bmp or png.
On the other hand, there is also the *n option to allow color variation, if needed. You could also try

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

If ErrorLevel = 2
msgbox, Image not found
If ErrorLevel = 1
msgbox, There was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
to narrow down the problem.

But there are probably much bigger Imagesearch experts on the forum than I am.


Okay thanks I'll play around. Will this be proper given that the image is taken care of?


It says the closing parenthesis is unexpected?
Last edited by Nwb on 21 Feb 2018, 06:03, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 06:03

Well, the path has to include the name of the file. That seems to be missing here:

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

C:\Users\coolb\Desktop\ahk images
Rather try

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

C:\Users\coolb\Desktop\ahk images\myimage.bmp
or something like that.

EDit: Ah, I see you changed the path. If the image is in the script's directory, that should work, I guess.
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 06:06

It says the closing parenthesis is unexpected?
Closing parenthesis have to be at the start of a line. With the syntax you are using, opening parenthesis should be, too!

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

Last edited by gregster on 21 Feb 2018, 06:10, edited 4 times in total.
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Fix my script (simple)

21 Feb 2018, 06:07

Ahh okay the problem was that I forgot the \image.png. I left it at filename. It works perfectly now thank you.
I am your average ahk newbie. Just.. a tat more cute. ;)
User avatar
gregster
Posts: 1382
Joined: 30 Sep 2013, 06:48

Re: Fix my script (simple)

21 Feb 2018, 06:13

Next, you could look into the use of else and else if. Also, this syntax is preferred for ifs: https://autohotkey.com/docs/commands/IfExpression.htm Only then you are allowed to have opening brackets on the same line with the if (One True Brace Style). With the "traditional" if-syntax you are using (https://autohotkey.com/docs/commands/IfEqual.htm), you have to write it like I did above to prevent mistakes (braces on the next line).

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], JoeWinograd, lefan, truekefir and 55 guests