AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WinActivate not working...

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
triwebb1



Joined: 06 Mar 2008
Posts: 20

PostPosted: Fri Apr 18, 2008 12:55 am    Post subject: WinActivate not working... Reply with quote

Hi. I have a pretty long and complicated macro that I am working on, and I have a silly problem. Here is a set of example scripts to show how I have things structured:

Main.ahk:
Code:
#Include Functions.ahk
SetTitleMatchMode 1

Loop
 {
  IfWinNotExist Notepad
   {
     Run notepad.exe
     WinGet pidvar,PID,A
   }
  Activate()
 }


Functions.ahk:
Code:
Activate()
 {
  global pidvar
  WinActivate ahk_pid %pidvar%
  Return
 }


So this example works fine. Why would something like this not work in a more complicated script? I have narrowed my problem down to this. When the command in the equivalent of functions.ahk calls the WinActivate %pidvar%, it seems to activate the desktop instead of the pidvar window - the pidvar window was already activated, but when it becomes de-activated after this call. I have verified that pidvar contains the correct pid.

What is really wierd is that the first time or two that the Activate() function is called, it works fine, but later on it stops working.

I can't figure this out - it seems so simple, and this test example works fine. There most be something else, some special circumstance that is making this not work. Does anyone have any ideas?


Last edited by triwebb1 on Fri Apr 18, 2008 1:38 am; edited 2 times in total
Back to top
View user's profile Send private message
pockinator



Joined: 06 Dec 2007
Posts: 33

PostPosted: Fri Apr 18, 2008 1:34 am    Post subject: Reply with quote

I am learning AHK myself, but seeing as no one has answered you yet, I'll just toss out an idea here.

In your posted code, refer to the two red lines:
Code:
Loop
 {
  IfWinNotExist Notepad
   {
     Run notepad.exe
     WinGet pidvar,PID,A

   }
  Activate()
 }

It might be a possibility that when the WinGet command is called upon, the active window (A) is still the desktop because notepad.exe hasn't had a chance to start yet. (I know that your example code works, but I have nothing else to refer to here.) Maybe you can insert a WinWaitActive command before the WinGet command? Although now that I'm thinking about it, that might be a problem considering you don't know the PID yet. Of course, in this particular example, you could use:
Code:
SetTitleMatchMode, 2
WinWaitActive, - Notepad

But I guess the problem is that we don't know the actual code you are using. Maybe you can post the actual code instead of a shortened example? It could be that the error lies in an overlooked detail elsewhere, which I know sometimes happens to me. Very Happy
Back to top
View user's profile Send private message
triwebb1



Joined: 06 Mar 2008
Posts: 20

PostPosted: Fri Apr 18, 2008 1:48 am    Post subject: Reply with quote

It may very well be that the problem is in my real code, but I have about 5000 lines of code in it, and this part of it is intertwined with the message passing part, and an excerpt of that will not make any sense without seeing all of it.

In my real code the program isn't actually notepad, and the program is already running when I activate it. I edited my original post just after you replied and I pointed out that this activation part works fine for the first two times that the Activate() function is called, but then, for no apparent reason, it doesn't work after that. I have monitored the %pidvar% the whole time, and it is always what it should be.
Back to top
View user's profile Send private message
triwebb1



Joined: 06 Mar 2008
Posts: 20

PostPosted: Fri Apr 18, 2008 1:58 am    Post subject: Reply with quote

Ok, well here is the actual function (ReturnTokenWait) that is having the problem. It should also be noted that I have another function which also activates pidvar, and it does not work either, after about the second time.

I don't think this will help at all, but maybe...

Code:
ReturnTokenWait(time=0)
 {
  global pidvar
  sleeptimer:=A_TickCount
  SetTitleMatchMode 2
  PostMessage 5000,,,,Macro Manager  ;Give token back to manager
  Loop
   {
    GetToken()  ;Wait for manager to give me token
    If ((A_TickCount-sleeptimer)>time)
     Break
    Sleep 100
    ReturnToken()  ;Give back token to manager if it is not time yet
   }
  IfWinExist ahk_pid %pidvar%  ;Check if pidvar window exists
   WinActivate ahk_pid %pidvar%  ;When I have token and it is time, activate this window if it exists
  Sleep 100
  Return
 }


Note that the only way the WinActivate command can be called is if the pidvar window exists. So the fact that it is trying to activate the window means that it did indeed find that the pidvar window exists, but for some reason when it tries to activate it, it activates the desktop instead.
Back to top
View user's profile Send private message
triwebb1



Joined: 06 Mar 2008
Posts: 20

PostPosted: Sat Apr 19, 2008 9:05 am    Post subject: Reply with quote

So I changed all instances of "pid" to "id" in my scripts, and now it all works great. I don't know why it doesn't work with pids, but it does with ids. I guess this may be a bug... Dunno, but I'm happy that I found a work around!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group