AHK with 2 programs?

Get help with using AutoHotkey and its commands and hotkeys
densch
Posts: 46
Joined: 29 May 2018, 15:10

AHK with 2 programs?

25 Jun 2018, 14:31

Hi, my thing that i want to have is:
At the start I'll manually open 2 different programs.

I want AHK to do the following:
sometimes, at unpredictable times a popup will show up in program A
(which can't be traced by source code or stuff. Would need imagesearch or something to detect it.
the psoition where it shows up as well as how it looks is always the same.
AHK just needs to wait for the popup to show up.)

so if the popup shows up, AHK shall go to program 2 and do there certain things, mostly specific clicking.

it is really important that as little time as possible passes between the popup showing up and the AHK finishing the clicking sequence in window 2.

the clicking itself is nothing special, clicking at fixed spot a, typing some given numbers, etc.

nothing too hard.

once the lciking is done, it should go back to window 1 and simply close the popup by clicking, so that there is now space again for a new popup to come.


AND what I also want:
I want the AHK program only to do this in a certain time frame.
What I mean:
I somehow want AHK to always check the current time (perhaps by somehow recognizing the time on the clock at the bottom right in the task bar.

and only do the above "wait until popup shows up, then instantly do specified clicking in window 2" when the current time is for example between
9:30 and 11:30 am.

if the current time isn't in that interval, it shouldn't do any of the above and sit back quietly.

only checking time every now and then to make sure that it doesn't miss the start of the 2 Work hours" :-D



do you think that something like this is possible?

Cause the qustion will come anyways, it's about some trading stuff.

in window 1 I will have a popup coming up whenever certain conditions are met (the programming of the popup doesnt have anything to do with AHK, so I wont go into details).
and AHK shall place the trade in window 2 by clicking the buttons there (since I know where the buttons are, clicking fixed coordinates there will do the job.



and If I were to make one more ridiculous request, it would be great if all of that could happen in the background.

so that that autotrading via ahk stuff could happen in the background and I'm doing other stuff in the foreground.

Cause I would preferably not want the notebook occupied by the AHK working and me not being able to work in that time.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: AHK with 2 programs?

25 Jun 2018, 15:16

Here's a template, you will have to change this. Click on any links in the code to look at the documentation. Oh, it's not in the code, but check out CoordMode to make sure your coordinates are right between ImageSearch and Clicks.

Code: Select all

#Persistent
SetTimer, Routine, 30000 ; checks every 30 seconds
return

Routine:
If !((A_Hour = 9 && A_Min >= 30) || (A_Hour=10) || (A_Hour = 11 && A_Min <= 30)) ; If it is NOT between 9:30 and 11:30, don't do anything.
   return ; single-line command associated with the If statement
ImageSearch, image stuff, fill in the parameters
If (ErrorLevel) ; the image wasn't found if there's an error
    return ; single-line command associated with the If statement
WinActivate, Window2
Click X, Y ; whatever coordinates
Send abc ; whatever you need to send
WinActivate, Window1
Send {Esc} ; or whatever will dismiss the popup
return
If the popup is its own Window, you could use WinWait instead of repeatedly checking for the pop up with ImageSearch. Read up on WinTitle to figure out how to name this. You may need to use something like MyPopUpTitle ahk_class #32730 ahk_exe program1.exe or MyPopUpTitle ahk_exe program1.exe or MyPopUpTitle.

Edit: The background stuff can be done with ControlClick and ControlSend, but ImageSearch definitely won't work if the popup is not visible on the screen. The WinWait might work there.
densch
Posts: 46
Joined: 29 May 2018, 15:10

Re: AHK with 2 programs?

25 Jun 2018, 16:01

Wow, thanks!

I'll work through all that even though it might take a while :-)

Hm, technically I could minimize the windows immediately via WinMinimize as soon as the intended stuff is done (which it should be able to do in an instant.
Since I don't need any puffer time between actions.
Doesn't matter if a hypothetical click rate of 8 clicks per sec is achieved :-D)

Will really have to think that whole thing through.

As far as I understand the #persitent thingie (does the #persistent make the stuff after it run nonstop?)
is executed first.
due to the settimer thing, the Routine program/function/method/whatever-it's-called-in-ahk
is run ever 30 seconds.

the routine thing itself has 2 kinds of break conditions implemented:
the time thing, if stuff is ooutside the time frame, return; is performed and the whole routine program ends abruptly.

same thing, a few lines later, when errorlevel is found and therefor the image wasn't found.

(
by the way I am currently thinking about wether a simple color(pixel) thing would do the job.

cause it is quite limited what can show up.
so I know that at a specific coordinate of the popup
the corresponding pixel is green.

so searching for example "is pixel at 10,55 green?" would esentially be the same as asking "is the green popup present".
)

anyways winactivate then goes to 2. window. bringing it to the front and taking it out of it's minimized state.
then simple clicking, sending and stuff.
then back to window 1.
doing similar stuff there.
the end.


by the way I still don't understand the difference between functions and methods in ahk too well.
I am very used to the usual
function(input){
}
style so it deeply confuses me when literally there are no brackets at all and function like structures look the same as
a simple list of commands.

for example the if(errorlevel) above.

if I didnt know the keyword if and the next line wasnt indented I never would have guessed that there was a condition thing, let kaloine knowing where it stats or ends.

think that's a really dumb thing about ahk.
why not use brackets around parts that belong together?

How do you guys not get confused by this stuff?
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: AHK with 2 programs?

26 Jun 2018, 14:39

Reading through your post, I think you've got it.
As far as I understand the #persitent thingie (does the #persistent make the stuff after it run nonstop?)
#Persistent prevents the script from closing when it reaches the auto-execute section. Normally people start out with AHK using a Hotkey, which automatically keeps the script open. But when you are using SetTimer without any hotkeys, you want #Persistent so that the timer can even run.

About Pixel Color vs ImageSearch

Yes, you can use PixelGetColor instead of ImageSearch if you'd like, then you would use something like If (myColor != 0x0000FF) or If !(myColor = 0x0000FF) (practically equivalent here) instead of the If !(ErrorLevel).
by the way I still don't understand the difference between functions and methods in ahk too well.
I am very used to the usual
function(input){
}
style so it deeply confuses me when literally there are no brackets at all and function like structures look the same as
a simple list of commands.
Yes, AHK is different in that way. AHK v2 is closer to the function() styles throughout its code; however, AHK does support using functions. AHK blends both simple commands for non-programmers and functions() for flexibility.
if I didnt know the keyword if and the next line wasnt indented I never would have guessed that there was a condition thing, let kaloine knowing where it stats or ends.
Important to note here that indentations are purely stylistic in AHK. You don't need them to run code. But it does help clarify in the absence of brackets; those two lines could be written as

Code: Select all

If (ErrorLevel)
{
return
}
think that's a really dumb thing about ahk.
why not use brackets around parts that belong together?
I personally like it for quickly writing up code, but you can definitely feel free to use brackets as you wish in the original code. AHK is pretty flexible about superfluous characters.

How do you guys not get confused by this stuff?
Lots of practice with the language, and referring to documentation.

Return to “Ask For Help”

Who is online

Users browsing this forum: DathPower, DRocks, Google [Bot] and 154 guests