Jump to content

Subscript used on non accessible variable


Recommended Posts

So I have this script that takes care of everything I need to do when rebooting my PC while I'm away. It worked like a charm for about a month and is now starting to throw "Error: Subscript used on non accessible variable" in an unconsistent manner - sometimes it works, sometimes it doesn't. Here's my script, the Error box says that it's on line 22:

 

#RequireAdmin


Sleep(30000)

;Switch to TRIXX, open Settings
WinActivate("Sapphire TRIXX")
Sleep(1000)
MouseMove(0,0)
ControlClick("Sapphire TRIXX", "", "[CLASS:SkinButton; INSTANCE:6]")
Sleep(1000)

$i = 0
While $i <= 5
   set_GPU($i)
   set_Profile()
   $i += 1
WEnd

Sleep(10000)
ShellExecute("C:\Users\username\PathToAShortcutLinkedToA.batFile") ;there's a real path in the actual code obviously (this is where the error is reported)

Func set_GPU($GPU)
   ;calculate teh offset for gpu selection
   $GPUOffset = 32 + 25*$GPU

   ;open dropdown
   ControlClick("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")

   ;get position of windows and the dropdown
   $dialogPos = WinGetPos("Dialog")
   $dropdownPos = ControlGetPos("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")

   ;select GPU
   MouseClick("left", $dialogPos[0]+$dropdownPos[0], $dialogPos[1]+$dropdownPos[1]+$GPUOffset)

   EndFunc


Func set_Profile()
   ;move Mouse to 0,0
   MouseMove(0,0)

   ;click Profile 1
   ControlClick("Sapphire TRIXX", "", "[CLASS:SkinButton; INSTANCE:11]")
   Sleep(100)

   ;load profile
   MouseClick("left",10,10)
   Sleep(500)

   ;apply
   ControlClick("Sapphire TRIXX", "", "[CLASS:SkinButton; INSTANCE:1]")
   Sleep(1000)
   EndFunc

So what really throws me off is that the error doesn't come up every time - it's not reproducable in any way so I have a really hard time to debug this as I have to wait several days to find out if the fix actually fixed it or it decided to run despite being fixed. I also haven't coded anything in AutoIt for more than half a decade now.

 

Help is much appreciated.

Link to comment
Share on other sites

  • Developers

You probably are running the script compiled? Line 22 is not that line in that case but I expect it to be:

MouseClick("left", $dialogPos[0] + $dropdownPos[0], $dialogPos[1] + $dropdownPos[1] + $GPUOffset)

You aren't testing for success on the lines above it so can't be sure an Array is returned in these lines:

$dialogPos = WinGetPos("Dialog")
    $dropdownPos = ControlGetPos("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")

So put in some test for @error on both and act upon it.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

11 minutes ago, Jos said:

You probably are running the script compiled? Line 22 is not that line in that case but I expect it to be:

MouseClick("left", $dialogPos[0] + $dropdownPos[0], $dialogPos[1] + $dropdownPos[1] + $GPUOffset)

You aren't testing for success on the lines above it so can't be sure an Array is returned in these lines:

$dialogPos = WinGetPos("Dialog")
    $dropdownPos = ControlGetPos("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")

So put in some test for @error on both and act upon it.

Jos

Yes, I am running the script compiled. Now it makes sense that it says line 22, thanks.

Could I also check if it's an array like this:

Func set_GPU($GPU)
    ;I now also declare variables as some old posts brought that up
    Local $GPUOffset, $dialogPos, $dropdownPos

   ;calculate teh offset for gpu selection
   $GPUOffset = 32 + 25*$GPU

   ;open dropdown
   ControlClick("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")

   ;get position of windows and the dropdown
   $dialogPos = WinGetPos("Dialog")
   $dropdownPos = ControlGetPos("Dialog", "", "[CLASS:SkinStatic; INSTANCE:1]")
   
   ;check if the variables are arrays in the first place
   If IsArray($dialogPos) And IsArray($dropdownPos) Then
        ;select GPU
        MouseClick("left", $dialogPos[0]+$dropdownPos[0], $dialogPos[1]+$dropdownPos[1]+$GPUOffset)
   Else
   ;do something to fix it
   Endif

   

   EndFunc

So now, I can't have the script return "error" and just stop because that'd result in the script not running properly if something happens still. Would it make sense to simple put set_GPU($i) into the Else statement to "retry"? Why is WinGetPos and ControlGetPos returning something else than an array in the first place?

Link to comment
Share on other sites

  • Developers
33 minutes ago, hinterlufer said:

Why is WinGetPos and ControlGetPos returning something else than an array in the first place?

The first one is unlikely to fail but the ControlGetPos() will fail when the control can't be found.

By the way, the reason the line number changes in the compiled script is that the empty and comment lines get stripped from the code at compilation time.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Ok, I think I have found the issue:

When the program I want to manipulate opens, the settings dialog is sometimes open, sometimes closed. Now, if I click the "Settings" button while the settings dialog is open already, it'll close it and thus WinGetPos and ControlGetPos can't find the requested item -> error.

I tried to solve it by checking if IsArray(WinGetPos(handle of the dialog)) and clicking the settings button only, if it doesn't return an array/isn't open already.

Thanks Jos for the useful input.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...