Jump to content
Sign in to follow this  
bmitchell

Winactivate - Bringing window to focus - task scheduler

Recommended Posts

bmitchell

Hi - I'm extremely new to all this, but can't believe I haven't found it before! :mellow:

I seem to be having an issue with Firefox being brought into focus in some cases. which in turn means the rest of my script cannot run.

Basically I want a simple script to do the following:

open firefox

go to my routers status page

login (firefox fills out the password box)

press disconnect on that page.

What I have below works OK on both 32 and 64 bit when clicked directly from the .exe the problem is when I use Windows Task scheduler to run it on the 64bit system (Win7). It opens firefox (I can see it on the taskbar) but doesn't then bring it into focus. When i click on Firefox on the taskbar the script continues.

I have tried various methods below (commented out at the moment) but none makes any difference.

I have triple checked the Task scheduler settings and all are exactly the same. It seems to be related to 64bit windows.

I have tried compiling for both 32 and 64 and although both work from a double clicking the .exe neither work from scheduled.

Any help would be greatly appreciated!

;-----Configuration-----

Dim $ConfigWindowTitle = "Mozilla Firefox Start Page - Mozilla Firefox"
Dim $appName = "Mozilla Firefox Start Page"
$appHandle = WinGetHandle($appName, "")

;-----End of configuration----

Run("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") ;Run firefox remove include x86 for 64bit system

WinActivate($appHandle);firefox in focus

;WinSetOnTop($appHandle, "", 1)

;WinSetState($appHandle, "", @SW_SHOW)

WinWaitActive($ConfigWindowTitle) ;Wait for it to be active before sending keystrokes.

Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}") ;focus address bar

Send("http://192.168.1.1/setup.cgi?next_file=Status.htm{ENTER}") ;send url

WinWaitActive("Authentication Required") ; wait for password box

Send("{ENTER}")

WinWaitActive("Modem Router Status") ;wait for modem window

;Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{ENTER}") ;select disconnect

;WinClose($ConfigWindowTitle)

Share this post


Link to post
Share on other sites
bmitchell

Hi bmitchell :mellow:

Try the WinActivate function, this seems to suit your needs perfectly.

Thanks for the reply - That is what I currently have in the code above - and it does suit my needs when running it straight from the .exe but when scheduling to run in Win7x64 It doesn't bring the window into focus. (this is my problem)

Share this post


Link to post
Share on other sites
dani

You use WinActivate on the handle, but if the documentation is complete this is not possible :mellow: Use it on the window title instead. In your code, that is $ConfigWindowTitle.

Instead of $ConfigWindowTitle you can also use more advanced title definitions

When running from the .exe WinActive($appHandle) probably returns false just like when you run it from the service but it gets focus already anyway because this is the default behavior for newly launched apps. I'm not sure why the window doesn't get focus when launched from the scheduler but WinActive($ConfigWindowTitle) should do the trick.

For debugging purposes, you could save the result of WinActivate and print it to verify it worked. It returns the handle of the window if it was successful and 0 otherwise.

$var = WinActivate($ConfigWindowTitle)
ConsoleWrite($var)
; or
; MsgBox(0, "", $var)
Edited by dani

Share this post


Link to post
Share on other sites
bmitchell

This is what I tried to use first time round. It has the same outcome unfortunately.

I've just tried again - no luck

I also tried just putting winactivate("Mozilla Firefox - start page") - still the same..

Share this post


Link to post
Share on other sites
dani

What about the result of WinActivate? Did you print it -- was it 0 or a handle? Perhaps it simply doesn't complete successfully which would explain why the window does not become active.

Aside from that, is the title exactly "Mozilla Firefox - start page"? Anyway, just try printing the result of WinActivate first, I'm guessing its 0.

Edited by dani

Share this post


Link to post
Share on other sites
bmitchell

What about the result of WinActivate? Did you print it -- was it 0 or a handle? Perhaps it simply doesn't complete successfully which would explain why the window does not become active.

Aside from that, is the title exactly "Mozilla Firefox - start page"? Anyway, just try printing the result of WinActivate first, I'm guessing its 0.

Thanks again for your help on this :mellow: - I put this into my code after where winactive is used and sure enough I get a 0 pop up in a msg box - have I got the code right? -i've not used this bit before?

$var = WinActivate($ConfigWindowTitle)
ConsoleWrite($var)
; or
MsgBox(0, "", $var)

The actual text in the title bar is "Mozilla Firefox Start Page - Mozilla Firefox" without quotes. EDIT: I see why you ask now - When I entered it in the code directly it was correct. I just couldn't remeber the actual text when writing the post!

How do I resolve?

Edited by bmitchell

Share this post


Link to post
Share on other sites
dani

Well it kinda depends on the matching mode you use for the title (see here). But as you use the default, you can just use:

$ConfigWindowTitle = "[CLASS:MozillaUIWindowClass]"
; or
; $ConfigWindowTitle = "Mozilla Firefox"

$var = WinActivate($ConfigWindowTitle)
ConsoleWrite($var)
; or
; MsgBox(0, "", $var)

Both ways work fine on my machine.

Edited by dani

Share this post


Link to post
Share on other sites
bmitchell

Well it kinda depends on the matching mode you use for the title (see here). But as you use the default, you can just use:

$ConfigWindowTitle = "[CLASS:MozillaUIWindowClass]"
; or
; $ConfigWindowTitle = "Mozilla Firefox"

$var = WinActivate($ConfigWindowTitle)
ConsoleWrite($var)
; or
; MsgBox(0, "", $var)

Both ways work fine on my machine.

:s hmmm neither of those work on mine..

How do you know the CLASS? where do I find this info?

Maybe I'll have to try setting one of the other matching modes to see how that goes?

Share this post


Link to post
Share on other sites
dani

You can use the AU3Info.exe file located in your AutoIt installation folder, and probably in the start menu folder it created. I think I gave you all the information you need so you should be able to fix the problem :mellow: When I manually open Firefox and run the script I gave you $var is 0x..... (random hex) which indicates a window handle. I don't know why it doesn't work for you. Can you post your whole script again with correct use of WinActivate?

Share this post


Link to post
Share on other sites
jacobraccuia

This totally isn't addressing your problem, but I was very enthused when I found out that instead of sendind {tab}{tab}{tab} you can send it in one command.

Send("{tab}{tab}{tab}{tab}")

=

Send("{tab 4}")

Share this post


Link to post
Share on other sites
bmitchell

Thanks so much Dani! :mellow: with your help it's fixed! I just started from scratch with your pointers so was left with the code below..

For some strange reason it doesn't like WinWaitActive($ConfigWindowTitle) and continued without waiting. Swapping for the actual title worked though. :(

@jacobraccuia - Great tip - Thanks it looks neater at least!!

$ConfigWindowTitle = "[CLASS:MozillaUIWindowClass]"
; or
; $ConfigWindowTitle = "Mozilla Firefox"

Run("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") ;Run firefox remove include x86 for 64bit system

$var = WinActivate($ConfigWindowTitle)
;ConsoleWrite($var)
; or
;MsgBox(0, "", $var)

WinWaitActive("Mozilla Firefox Start Page - Mozilla Firefox") ; Wait for it to be active before sending keystrokes.

Send("{TAB 7}") ;focus address bar

Send("http://192.168.1.1/setup.cgi?next_file=Status.htm{ENTER}") ;send url

WinWaitActive("Authentication Required") ; wait for password box

Send("{ENTER}")

WinWaitActive("Modem Router Status") ;wait for modem window

Send("{TAB 11}{ENTER}") ;select disconnect

;WinClose($ConfigWindowTitle)

Share this post


Link to post
Share on other sites
dani

WinWaitActive should also work with your $ConfigWindowTitle, strange. It does work with the task scheduler now?

Share this post


Link to post
Share on other sites
bmitchell

Hmmm - it didn't run as scheduled last night?! even though I am sure it worked when I last posted! (I scheduled it to run whilst I was watching it, and ran it manually in task scheduler and i'm sure it worked!) now for some reason it just doesn't - its back to only working off a double click of the .exe :mellow:

Share this post


Link to post
Share on other sites
bmitchell

Aha! I added a sleep(5000) in after the Run firefox and it seemed to work!

I was then also able to change back to WinWaitActive($ConfigWindowTitle)and run successfully. Maybe when I was fiddling first time it just so happened that my machine was fast enough to get the process up and running before winactivate kicked in? When I scheduled it to run my machine would have been busy doing other things too.

Does this sound like a logical explanation? what kind of Sleep time is generally used? i'm guessing 5000 should be enough before I think about changing my machine! :mellow:

Share this post


Link to post
Share on other sites
bmitchell

I agree it seems to leave room for error! So do you mean use WaitWinActive($ConfigWindowTitle) before WinActivate($ConfigWindowTitle)? Do I then need it again after?

So

WinWaitActive($ConfigWindowTitle)

WinActivate($ConfigWindowTitle)

Then WinWaitActive($ConfigWindowTitle) again?

Share this post


Link to post
Share on other sites
bmitchell

OK so i've tried this.. arrangement - no luck

WinWaitActive($ConfigWindowTitle)

WinActivate($ConfigWindowTitle)

Then WinWaitActive($ConfigWindowTitle)

also tried WinWaitExists - also doesn't work as expected. i'm just guessing now though!

Sleep may have to do for now ? - although not ideal

Share this post


Link to post
Share on other sites
bogQ

its

WinActivate($ConfigWindowTitle)

WinWaitActive($ConfigWindowTitle)

not

WinWaitActive($ConfigWindowTitle)

WinActivate($ConfigWindowTitle)

Fitst you try to active it than wait for it to active

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
bmitchell

its

WinActivate($ConfigWindowTitle)

WinWaitActive($ConfigWindowTitle)

not

WinWaitActive($ConfigWindowTitle)

WinActivate($ConfigWindowTitle)

Fitst you try to active it than wait for it to active

Thanks for the reply, This is what I had to begin with:

Run("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
WinActivate($ConfigWindowTitle)
WinWaitActive($ConfigWindowTitle)

but it doesn't work 100% because sometimes the Run has not loaded firefox up quickly enough so the script misses the WinActivate line out (it can't find the window because its not fully loaded yet)

It works without fail if I do the following

Run("C:\Program Files (x86)\Mozilla Firefox\firefox.exe")
Sleep(5000)
WinActivate($ConfigWindowTitle)
WinWaitActive($ConfigWindowTitle)

but as pointed out above this isn't ideal really.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.