TJ123 Posted May 16, 2012 Share Posted May 16, 2012 (edited) I am having trouble getting a Func to work properly in my script. I have tried several things, including searching for answers on my own, but I have not been able to solve this. Any help is greatly appreciated. I have two windows with the word "WINDOW" in the title. I want to be able to toggle between these two windows using a hotkey, regardless of which window is currently active. The code below works fine about 90% of the time. The rest of the time it crashes and I get the following error message: "Error:Array variable has incorrect number of subscripts or subscript dimesion range exceeded" Func NEXTWINDOW() $active = WinGetTitle( "WINDOW", "" ) <----Gets the active window title. $wlist = WinList ( "WINDOW", "" ) <----Creates an array with all windows that have "WINDOW" in the title. Sleep (350) If $active = $wlist[1][0] Then <----Checks to see if the first window in the array is active. WinActivate ( $wlist[2][0] ) <----If that window is active, it activates the second window. Sleep (100) EndIf If $active = $wlist[2][0] Then <----Checks to see if the second window in the array is active. WinActivate ( $wlist[1][0] ) <----If the second window is active it activates the first window Sleep (100) EndIf EndFunc I originally used IF / ELSE statements rather than two IF's. I seem to have the same problem either way. In case this matters I am running 64bit Windows 7. Many thanks to anyone who can offer suggestions or advice. TJ. Edited May 16, 2012 by TJ123 Link to comment Share on other sites More sharing options...
abberration Posted May 16, 2012 Share Posted May 16, 2012 How about this? I changed your two if statements into an if/elseif. At the very end, I displayed the array of returned values. I ran it several times with no errors on a 32 bit XP system. #include <array.au3> $active = WinGetTitle( "WINDOW", "" ) $wlist = WinList ( "WINDOW", "" ) Sleep (350) If $active = $wlist[1][0] Then WinActivate ( $wlist[2][0] ) Sleep (100) ElseIf $active = $wlist[2][0] Then WinActivate ( $wlist[1][0] ) Sleep (100) EndIf _ArrayDisplay($wlist) Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
shanet Posted May 16, 2012 Share Posted May 16, 2012 Next time you post, please make sure you make use of the autoit code tags (the button to the left of the red paw). The reason you are getting that error is because your program is trying to access a part of the array that does not exist: Global $array[4] ; has 4 elements $myVar = $array[10] ; get the 10th index As I am sure you will know, that will not work. Your code looks fine, so maybe you would like to look into $wlist[0][0] to make sure you have 2 windows returned? [font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS: %programfiles%/AutoIt3/autoit3.chm Link to comment Share on other sites More sharing options...
TJ123 Posted May 16, 2012 Author Share Posted May 16, 2012 Thanks for the suggestions abberration and shanet, I really appreciate it!Abberration, I will try changing the second statement to an ElseIf to see if that helps any.I am unsure what the purpose of adding _ArrayDisplay($wlist) at the end is. Did you just add that for testing to ensure there are two variables in the array, or does it actually do something to solve the problem?Do you think I might need to add a longer delay after running the WinList command? How long do you think it takes to enumerate the windows and create the array?Thanks again for any advice,TJ. Link to comment Share on other sites More sharing options...
abberration Posted May 16, 2012 Share Posted May 16, 2012 Thanks for the suggestions abberration and shanet, I really appreciate it!Abberration, I will try changing the second statement to an ElseIf to see if that helps any.I am unsure what the purpose of adding _ArrayDisplay($wlist) at the end is. Did you just add that for testing to ensure there are two variables in the array, or does it actually do something to solve the problem?Do you think I might need to add a longer delay after running the WinList command? How long do you think it takes to enumerate the windows and create the array?Thanks again for any advice,TJ._ArrayDisplay was not added to fix the problem. I was having trouble with your script due to capitalization of the word "WINDOW". I added it for myself to see what windows were being detected. Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
TJ123 Posted May 17, 2012 Author Share Posted May 17, 2012 Ok, thanks for clarifying that abberration. I still seem to be getting this error message, but only about 10-15% of the time. I am not sure why. I am wondering if it takes a while for WinList to enumerate all the windows, or if it sometimes misses one window. Thinking of adding some kind of error catching loop that would run WinList again if it does not return the right number the first time. Thanks again, TJ. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now