AutoHotkey Community

It is currently May 26th, 2012, 11:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Foreground/Focus Problem
PostPosted: November 4th, 2009, 8:31 pm 
Offline

Joined: November 3rd, 2009, 3:41 pm
Posts: 5
Location: KPTK
I'm a newbie to AHK - not a newbie to programming.
I've used the search feature and browsed the tutorials,
but not found a crisp post re: focus (foreground).

My first script works until another window grabs focus
(becomes the foreground window) .... typically a
status/upgrade/nag window of some sort.

My attempts to use SetTitleMatchMode and WinActive
are not changing focus (bringing Mozilla back to the
foreground).

Help, please.

Code:
SetTitleMatchMode, 2
Loop {
Run http://www.google.com/
Sleep, 10000
WinActivate, Mozilla
Send, {TAB}{ENTER}
Sleep, 10000
WinActivate, Mozilla
Send, !f
Sleep, 5000
WinActivate, Mozilla
Send, x
Sleep, 1000000
}


-doug

_________________
I spend 80% of my money on airplanes and
beer, the rest is wasted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 9:16 pm 
Offline

Joined: August 22nd, 2009, 11:23 pm
Posts: 294
Check out
WinSet, AlwaysOnTop
And
Last Found Window

_________________
Image
"Man's quest for knowledge is an expanding series whose limit is infinity"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 4th, 2009, 10:40 pm 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6067
Location: San Diego, California
Quote:
I'm a newbie to AHK - not a newbie to programming.

Why do you have 5 seconds delay between alt-F and x ?

Since you are a programmer you know you start with simple routines and build upon them.
So, if you'll bear with me, I have a few basic questions.

Please confirm: is the word "Mozilla" always in the browser title?
For example my current browser window has exactly:"Post a reply - Windows Internet Explorer"

Please confirm: is the word "Mozilla" EVER in the 'status/upgrade/nag ' ?


Run the following code, run your browser steps manually.
When the browser looses focus, does running this hotkey routine ALWAYS "bring it back to top/focus"?

If not 100%, what is the title of the window that has top/focus?

Code:
SetTitleMatchMode, 2
return

F1::   ;<-------------------I chose the F1 hotkey, change as desired
WinActivate, Mozilla
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 2:21 pm 
Offline

Joined: November 3rd, 2009, 3:41 pm
Posts: 5
Location: KPTK
Why do you have 5 seconds delay between alt-F and x ?

> Eases my task of monitoring AHK behavior to close Mozilla

Since you are a programmer you know you start with simple routines and build upon them.

> Yup. Which is why I began with a simple
> infinite loop with simple timers. The simple
> AHK script works ... until another window
> pops-up and steals focus.

So, if you'll bear with me, I have a few basic questions.

Please confirm: is the word "Mozilla" always in the browser title?

> Confirmed. Since I'm using "SetTitleMatchMode, 2" ... I am
> expecting that any window with "Mozilla" anywhere in the title
> should be a match ... for the subsequent; "WinSet, AlwaysOnTop, On, Mozilla"

For example my current browser window has exactly:"Post a reply - Windows Internet Explorer"

> I'm using Mozilla, not "Windows Internet Explorer"

Please confirm: is the word "Mozilla" EVER in the 'status/upgrade/nag ' ?

> To my knowledge, never. Haven't seen it yet.

Run the following code, run your browser steps manually.
When the browser looses focus, does running this hotkey routine ALWAYS "bring it back to top/focus"?

> It depends ...
>
> Since my AHK script is an infinite loop; the next
> pass thru the loop should close Mozilla ... and it
> does close Mozilla unless another has pop'd and
> stolen focus during one of the sleep's.
>
> If I kill AHK and restart it ... Mozilla will [re]grab
> focus. The loop will continue to execute until the
> next status/update/nag screen pops (and steals
> focus).

If not 100%, what is the title of the window that has top/focus?

> Pretty much any window can steal focus. I can provoke it by
> having a "Command Prompt" window open; or "GVIM"; or "Excel";
> or "bash"; or any of goofy balloon status dialogs that appear from the
> task bar. Last night it was a Sun Java updates-are-available dialog
> that stole focus.

Here is the latest edition that still has the (loss of) focus behavior:

SetTitleMatchMode, 2
WinSet, AlwaysOnTop, On, Mozilla

Loop {
Run http://www.google.com
Sleep, 1000

Send, {TAB}{ENTER}
Sleep, 10000

Send, !f
Sleep, 5000

Send, x
Sleep, 1000000
}

_________________
I spend 80% of my money on airplanes and
beer, the rest is wasted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 2:49 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
What are you actually trying to achieve?

The focusing is working fine for me as per your first script, although if focus changes between your "Alt-F" and "X" then the file menu will be closed when the focus is lost, meaning that your "X" key will be sent to the google window, not the menu. Other than that, I can wait for the first command, change focus, and the "Alt-F" command has focus brought back to the Mozilla window fine for me.

If you specify what you are actually trying to do, then we can find a more reliable way of doing it for you (because of the above problems).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 4:03 pm 
Offline

Joined: November 3rd, 2009, 3:41 pm
Posts: 5
Location: KPTK
> what are you trying to do?

Specifically; ALT-F and x, are simply the commands to politely close
Mozilla.

I *ass*umed that WinSet, AlwaysOnTop did precisely that. ALWAYS,
On-top ... and that I only needed to set it once (outside the infinite loop).

Should I be calling it after the "run http://www.google.com" that starts
Mozilla?

Should I be setting it after every call to SLEEP?

Or; perhaps I need to be calling WinActivate after the WinSet?

On a broader scope; I am attempting to script an "Agree" button on a
T&C/splash page that the netw admin has setup with an absurdly short
(15 minute) expiration.

The AHK script does that task ("Agree") ... but works only if focus is not
stolen from Mozilla.

Since I'm missing something about AHK and focus/foreground (or AHK is
broken?), I'm beginning to think about using my bag o'hammers ... and
using Run TASKKILL to bludgeon all instances of firefox.exe. Admittedly,
a truely crude approach. <g> But it also solves the problem of what-if
firefox.exe has children, or if there are multiple copies of firefox.exe
running. Hmmmh.

_________________
I spend 80% of my money on airplanes and
beer, the rest is wasted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 4:26 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
rainsux wrote:
Should I be calling it after the "run http://www.google.com" that starts
Mozilla?

Yes, as you can't set a window to always be on top until it actually exists.
Additionally, just because the window is "Always ON TOP" does not mean that it "Always HAS FOCUS" :)

rainsux wrote:
Specifically; ALT-F and x, are simply the commands to politely close Mozilla.

Indeed they are, but I was trying to establish the reason for the 5 second pause between the Alf-F and X? Have you tried just doing :
Code:
Send, !fx


Also, you can just use WinClose or WinKill instead:
Code:
WinClose, Mozilla ; less forceful
WinKill, Mozilla ; more forceful


Or if you want to be more specific, you can input the title of the specific page you want to close, and that will leave other instances of Mozilla open (rather than using the 'catch-all' word of 'Mozilla' in your window title).

I'm not sure if the below works any better for you?

Code:
SetTitleMatchMode, 2

Loop {
  Run http://www.google.com
  WinWait, Google
  WinSet, AlwaysOnTop, On, Google
  WinActivate

  Sleep, 10000

  WinActivate
  Send, {TAB}{ENTER}
  Sleep, 10000

  WinKill, Google

  Sleep, 1000000
}


EDIT: Just added the extra WinActivate before you send the TAB and ENTER (forgot that initially, sorry)...


Last edited by OceanMachine on November 5th, 2009, 8:32 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 5:09 pm 
Offline

Joined: November 3rd, 2009, 3:41 pm
Posts: 5
Location: KPTK
> Have you tried just doing: Send, !fx

Yes. Works okay. The delay was simply an artifact from my initial
attempts ... it allowed me to more easily watch what was happening.

I'll take your suggestion to use WinKill ... it solves all the problems of
other possible Mozilla instances and/or their children.

_________________
I spend 80% of my money on airplanes and
beer, the rest is wasted.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 8:34 pm 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
Just added the extra WinActivate before the TAB and ENTER line, as of course during the previous 10 seconds sleep the focus may have been lost! Sorry, forgot to add that initially.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 5th, 2009, 9:22 pm 
Offline

Joined: November 3rd, 2009, 3:41 pm
Posts: 5
Location: KPTK
Thanks ... I'd guessed that was appropriate. Nice to know my hunch
was correct.

_________________
I spend 80% of my money on airplanes and
beer, the rest is wasted.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: joetazz, JSLover, rbrtryn, Tipsy3000, WillTroll and 58 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group