Jump to content

Check if Array is empty


Recommended Posts

Hi,

i think its very simple to solve this problem but i cannot see where it is...

Here is the code:

#include <array.au3>
;$iPid = Run("cleanmgr.exe")
;Sleep(10) ; allow notepad to create window


$aArray = _WinGetInfoByProcess("cleanmgr.exe", 1)

;If not $aArray[1][0] = "" Then (
If IsArray($aArray[1][0]) Then (
$WName = $aArray[1][0]
 ; Wait 0 seconds for the Cleanmgr window to appear.
Local $hWnd = WinWait($WName, "", 0)
; Set the state of the Cleanmgr window to "hide".
WinSetState($hWnd, "", @SW_HIDE)
)

;MsgBox(0,"Value","The Value is" & $iIndex)
;_ArrayDisplay($aArray, "by Name, only visible windows")

Func _WinGetInfoByProcess($vProcess = "cleanmgr.exe", $nShow = 2)
    ;
    ;===============================================================================
    ;
    ; Function Name:    _WinGetInfoByProcess
    ; Description::     Get Window Handle of Process
    ; Parameter(s):     $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
    ;                   $nShow = -1 "All (Visble or not)"
    ;                   $nShow = 0 "Not Visible Only"
    ;                   $nShow = 1 "Visible Only"
    ;                   $nShow = 2 "Return handle of newest  window " (Default)
    ; Requirement(s):   none
    ; Return Value(s):
    ;                   @extended = returns number of entries in the array
    ;                   @error = 0 AND $nShow = 2 (default)  returns handle of newest window
    ;                   @error = 0 returns array with processinfos
    ;                              n = 1 to @extended
    ;                              Array[n][0] shows windows title.
    ;                              Array[n][1] shows windows handle.
    ;                              Array[n][2] shows windows Pid.
    ;                   @error = 1 Process not found.
    ;                   @error = 2 Window not found.
    ; Author(s):        inspired by Smoke_N's script _WinGetHandleByPID()
    ;                   but to handle more than one process with the same name
    ;                   and to return handle of newest window ($nShow = 2).
    ; tested versions:  Autoit 3.3.0.0
    ;
    ;===============================================================================
    ;
    If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
    Local $iWinList, $aWinList = WinList()
    Local $iResult, $aResult[UBound($aWinList)][3]
    Local $iProcessList, $aProcessList = ProcessList($vProcess)
    If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0],["", $vProcess]]
    For $iWinList = 1 To $aWinList[0][0]
        For $iProcessList = 1 To $aProcessList[0][0]
            If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
                If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
                $iResult += 1
                $aResult[$iResult][0] = $aWinList[$iWinList][0]
                $aResult[$iResult][1] = $aWinList[$iWinList][1]
                $aResult[$iResult][2] = $aProcessList[$iProcessList][1]
            EndIf
        Next
    Next
    If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
    ReDim $aResult[$iResult + 1][3]
    $aResult[0][0] = $iResult
    If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
    Return SetError(0, $iResult, $aResult)
 EndFunc   ;==>_WinGetInfoByProcess

_________________________________________________________________________________________________________________________________

The Function works as expected, only this part is the problem:

If IsArray($aArray[1][0]) Then (
$WName = $aArray[1][0]
 ; Wait 0 seconds for the Cleanmgr window to appear.
Local $hWnd = WinWait($WName, "", 0)
; Set the state of the Cleanmgr window to "hide".
WinSetState($hWnd, "", @SW_HIDE)
)

If the array value exists he should execute the following commands, if not he should simply exit without error message.

At them moment i get first: Unbalanced brackets in expression

And if the Array value doesn't exist: Subscript used on non accessible variable (This error i want to workarround with the If Statement)

Thanks for your help.

Michael

 

 

Edited by Melba23
Added code tags
Link to post
Share on other sites
  • Moderators

excelsi,

Welcome to the AutoIt forums.

When you post code please use Code tags - see here how to do it.  Then you get a scrolling box and syntax colouring as you can see above now I have added the tags.

Now to look at the code you posted.....

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites
  • Moderators

excelsi,

You get the "Unbalanced brackets in expression" error because you have unbalanced brackets - you do not need these ones:

If IsArray($aArray[1][0]) Then ( ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$WName = $aArray[1][0]
 ; Wait 0 seconds for the Cleanmgr window to appear.
Local $hWnd = WinWait($WName, "", 0)
; Set the state of the Cleanmgr window to "hide".
WinSetState($hWnd, "", @SW_HIDE)
) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

But I would approach the whole thing slightly differently:

#include <MsgBoxConstants.au3>

$iPid = Run("cleanmgr.exe")

; Look for max 10 sec
$nBegin = TimerInit()
While 1
    ; See if a window has appeared - using $nShow = 1 returns an array if the window exists and an error if not
    $aArray = _WinGetInfoByProcess("cleanmgr.exe", 1)
    ; No error, so window has appeared and we continue
    If @error = 0 Then ExitLoop

    ; Check timeout has not expired
    If TimerDiff($nBegin) > 10 * 1000 Then
        ; Announce failure and exit
        MsgBox($MB_SYSTEMMODAL, "Error", "Timeout")
        Exit
    EndIf

    ; Try again
Wend
; We have found a window, so extract name   ; But it would be better to use handle
$WName = $aArray[1][0]              ; $hWnd = $aArray[1][1]
WinSetState($WName, "", @SW_HIDE)       ; WinSetState($hWnd, "", @SW_HIDE)

Func _WinGetInfoByProcess($vProcess, $nShow = 2) ; Do not force CleanMgr name here

; Rest of code

That works for me - please ask if you have any questions.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...