Jump to content

Wrote Script and GUI: How to get them to work together?


kmatte
 Share

Recommended Posts

I'm extremely new to AutoIt; I've only been aware of its existence for a couple of days now. I've been asked to create a script that can disable/enable a network card on multiple machines running Windows XP (maybe Vista/Windows 7 too, but for now, XP). I've been reading the Help files and I checked out a couple of scripts posted to the forums for ideas on how to get started. The script I have is working on my current machine, and hopefully works on others as well.

Current script:

$bEnable = true ;set to false to disable the network

If @OSType <> "WIN32_NT" Then
    MsgBox(0, "", "This script requires Windows 2000 or higher.")
    Exit
EndIf

$ShellApp = ObjCreate("Shell.Application")
$oNetConnections = $ShellApp.Namespace(0x31)

For $LanItem In $oNetConnections.Items
    If StringLower($LanItem.Name) = StringLower("Local Area Connection") Then
        $oLanConnection = $LanItem
        ExitLoop
    EndIf
Next

$oEnableVerb = ""
$oDisableVerb = ""

For $Verb In $oLanConnection.Verbs
    If $Verb.Name = "En&able" Then
        $oEnableVerb = $Verb
    EndIf
    If $Verb.Name = "Disa&ble" Then
        $oDisableVerb = $Verb
    EndIf
Next

If $bEnable Then
    If IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card
EndIf

If Not $bEnable Then
    If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
EndIf

Sleep(1000)

Since the people using this script are severely technologically-challenged, they are going to need a GUI with ON/OFF buttons that can be clicked, rather than changing the script to change what happens. I've also figured out how to write a GUI:

#NoTrayIcon
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$MainGUI = GUICreate("Network Connection Switch", 280, 80, -1, -1)
$Label = GUICtrlCreateLabel("Toggle network connection:", 35, 10, 210, 35)
$Off = GUICtrlCreateButton("OFF", 80, 40, 60, 25, 0)
$On = GUICtrlCreateButton("ON", 150, 40, 60, 25, 0)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Off

        Case $On

    EndSwitch
WEnd

I'm not positive I've got the correct includes for the GUI, but it also seems to work.

The part I need help with is getting the script and the GUI to communicate with each other. I get the feeling this is something rather obvious and I've probably been spending too much time staring at all of this all day. Still, I'd really appreciate any feedback on the code I've got so far, and any pointers in the right direction.

Ideally what will happen is the OFF button on the GUI will turn the network off, and the ON button will turn the network on. I'm just having difficulty understanding how to accomplish this.

This may be part of the problem, but I've also just noticed something in the Wiki about Global and Local variables and declaring them. How do I know which variables should be declared as which type? Where do I do this in the code that I have?

Edited by kmatte
Link to comment
Share on other sites

This will do what you want but I think it can be improved on by quite a bit.

#Include <ButtonConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <StaticConstants.au3>
#Include <WindowsConstants.au3>

If @OSType <> "WIN32_NT" Then
    MsgBox(0, "", "This script requires Windows 2000 or higher.")
    Exit
EndIf

Global $bEnable = True
$ShellApp = ObjCreate("Shell.Application")

$MainGUI = GUICreate("Network Connection Switch", 280, 80, -1, -1)
$Label = GUICtrlCreateLabel("Toggle network connection:", 35, 10, 210, 35)
$Off = GUICtrlCreateButton("OFF", 80, 40, 60, 25, 0)
$On = GUICtrlCreateButton("ON", 150, 40, 60, 25, 0)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Off
            _Connection_Toggle(False)
        Case $On
            _Connection_Toggle()
    EndSwitch
WEnd

Func _Connection_Toggle($bEnable = True)
    $oNetConnections = $ShellApp.Namespace(0x31)

    FOR $LanItem In $oNetConnections.Items
        If StringLower($LanItem.Name) = StringLower("Local Area Connection") Then
            $oLanConnection = $LanItem
            ExitLoop
        EndIf
    Next

    Local $oEnableVerb = "", $oDisableVerb = ""


    FOR $Verb In $oLanConnection.Verbs
        If $Verb.Name = "En&able" Then
            $oEnableVerb = $Verb
        EndIf
        If $Verb.Name = "Disa&ble" Then
            $oDisableVerb = $Verb
        EndIf
    Next

    If $bEnable Then
        If IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card
    EndIf

    If NOT $bEnable Then
        If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
    EndIf

    Sleep(1000)


EndFunc   ;==>_Connection_Toggle

The way I read it this can all be done easily with a single button but before I get into that is there any way to get the current status of the card back using your object(s)?

Here is an example using the single button method I've commented out the If IsObj() lines so you can try it without actually changing the status of your adapter. By the way, the StringLower()s are not required since AutoIt is not case sensitive.

#Include <ButtonConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <StaticConstants.au3>
#Include <WindowsConstants.au3>

If @OSType <> "WIN32_NT" Then
    MsgBox(0, "", "This script requires Windows 2000 or higher.")
    Exit
EndIf

Global $bEnable = True;;  I would like to see some way of getting the current status to set this
$ShellApp = ObjCreate("Shell.Application")

$MainGUI = GUICreate("Network Connection Switch", 280, 80, -1, -1)
$Label = GUICtrlCreateLabel("", 35, 10, 210, 35)
;$Off = GUICtrlCreateButton("OFF", 80, 40, 60, 25, 0)
$On = GUICtrlCreateButton("ON", 110, 40, 60, 25, 0)
;; If we had the current status then this would make sense
If $bEnable Then
    GUICtrlSetBkColor($On, 0x00FF00) ;; Color indicates current status
    GUICtrlSetData($On, "Off")
    GUICtrlSetData($Label, "Toggle network connection: Off")
Else
    GUICtrlSetBkColor($On, 0xFF0000)
    GUICtrlSetData($On, "On")
    GUICtrlSetData($Label, "Toggle network connection: On")
EndIf

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

            ;Case $Off
            ;    _Connection_Toggle(False)
        Case $On
            $bEnable = NOT $bEnable
            _Connection_Toggle($bEnable)
    EndSwitch
WEnd

Func _Connection_Toggle($bEnable = True)
    $oNetConnections = $ShellApp.Namespace(0x31)

    FOR $LanItem In $oNetConnections.Items
        If StringLower($LanItem.Name) = StringLower("Local Area Connection") Then
            $oLanConnection = $LanItem
            ExitLoop
        EndIf
    Next

    Local $oEnableVerb = "", $oDisableVerb = ""


    FOR $Verb In $oLanConnection.Verbs
        If $Verb.Name = "En&able" Then
            $oEnableVerb = $Verb
        EndIf
        If $Verb.Name = "Disa&ble" Then
            $oDisableVerb = $Verb
        EndIf
    Next

    If $bEnable Then
        GUICtrlSetBkColor($On, 0x00FF00)
        GUICtrlSetData($On, "Off")
        GUICtrlSetData($Label, "Toggle network connection: Off")
        ;If IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card
    EndIf

    If NOT $bEnable Then
        GUICtrlSetBkColor($On, 0xFF0000)
        GUICtrlSetData($On, "On")
        GUICtrlSetData($Label, "Toggle network connection: On")
        ;If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
    EndIf

    Sleep(1000)


EndFunc   ;==>_Connection_Toggle
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thank you for the help! I like the idea of using one button to accomplish this. I'll have a chance this afternoon to look at the code you've posted and add in a way to test the status of the connection to what I started with. I didn't realize you could integrate the GUI with the functions like that, it makes things so much easier.

Link to comment
Share on other sites

Pretty much the only way to do it is using functions. Also see what you can find out about getting the current connection status of the adaptor, it will make things much simpler.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

If my line of thinking is correct, then the

If IsObj($oEnableVerb)/If IsObj($oDisableVerb)
is checking the status of the connection, since if "Enable" is there the card is off, and if "Disable" is there the card is on. Now I just need to turn this into something useful.

I'm learning; in the meantime I feel rather slow at all of this.

Link to comment
Share on other sites

It all takes time but the learning curve is short compared to most languages and in no time at all you will be writing code with little difficulty. I'm not sure that your assumption about Enable/Disable is correct though. I'll check that theory later. In the meantime your question about integrating the code has been answered so I'll drop out for now and check your theory later. I'll come back and post the results when I get a chance to do the test.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just little optimized code from post #2

created function SetButtonStatus() for repetitive task

#Include <ButtonConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <StaticConstants.au3>
#Include <WindowsConstants.au3>

If @OSType <> "WIN32_NT" Then
    MsgBox(0, "", "This script requires Windows 2000 or higher.")
    Exit
EndIf

Global $bEnable = True;;  I would like to see some way of getting the current status to set this
Global $oEnableVerb, $oDisableVerb
$ShellApp = ObjCreate("Shell.Application")

$MainGUI = GUICreate("Network Connection Switch", 280, 80, -1, -1)
$Label = GUICtrlCreateLabel("", 35, 10, 210, 35)
;$Off = GUICtrlCreateButton("OFF", 80, 40, 60, 25, 0)
$On = GUICtrlCreateButton("ON", 110, 40, 60, 25, 0)
SetButtonStatus()
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

            ;Case $Off
            ;   _Connection_Toggle(False)
        Case $On
            $bEnable = NOT $bEnable
            _Connection_Toggle($bEnable)
    EndSwitch
WEnd

Func _Connection_Toggle($bEnable = True)
    $oNetConnections = $ShellApp.Namespace(0x31)

    FOR $LanItem In $oNetConnections.Items
        If StringLower($LanItem.Name) = StringLower("Local Area Connection") Then
            $oLanConnection = $LanItem
            ExitLoop
        EndIf
    Next

    Local $oEnableVerb = "", $oDisableVerb = ""


    FOR $Verb In $oLanConnection.Verbs
        If $Verb.Name = "En&able" Then
            $oEnableVerb = $Verb
        EndIf
        If $Verb.Name = "Disa&ble" Then
            $oDisableVerb = $Verb
        EndIf
    Next

    SetButtonStatus(True)
    Sleep(1000)
EndFunc   ;==>_Connection_Toggle

Func SetButtonStatus($doit = False)
    ;; If we had the current status then this would make sense
    If $bEnable Then
        GUICtrlSetBkColor($On, 0x00FF00) ;; Color indicates current status
        GUICtrlSetData($On, "Off")
        GUICtrlSetData($Label, "Toggle network connection: Off")
;~      If $doit And IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card
    Else
        GUICtrlSetBkColor($On, 0xFF0000)
        GUICtrlSetData($On, "On")
        GUICtrlSetData($Label, "Toggle network connection: On")
;~      If $doit And IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
    EndIf
EndFunc
Edited by Zedna
Link to comment
Share on other sites

So far what I've done is set the GUI to display a message with which action has been performed when either the on or off button has been pressed, in the code for both cases. It's not the prettiest thing in the world, but it works. Thank you again for your help. I think I've got a handle on how this works. Now to find out from the higher powers whether they prefer one button or two, and if they need this thing to do anything else.

In the Connection Toggle function:

If $bEnable Then
        GUICtrlSetBkColor($On, 0x00FF00)
        GUICtrlSetData($On, "Off")
        If IsObj($oEnableVerb) Then $oEnableVerb.DoIt ; Enable network card
        WinMove ("Network Connection Switch", "", Default, Default, Default, 100)
        GUICtrlSetData($Label2, "The network connection has been enabled.")
    EndIf

    If NOT $bEnable Then
        GUICtrlSetBkColor($On, 0xFF0000)
        GUICtrlSetData($On, "On")
        If IsObj($oDisableVerb) Then $oDisableVerb.DoIt; Disable network card
        WinMove ("Network Connection Switch", "", Default, Default, Default, 100)
        GUICtrlSetData($Label2, "The network connection has been disabled.")
    EndIf
Edited by kmatte
Link to comment
Share on other sites

Suggestion;

Change

GUICtrlSetData($Label2, "The network connection has been disabled.")

to something like

GUICtrlSetData($Label2, "The network connection is disabled.")

Also do the same for the enabled line.

Remember, whatever you have in there will continue to display until the next toggle operation so it's important that it read properly.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I wrote a tool very similar to this years ago. I haven't used it in a while but maybe you'll find some use to it or get some ideas on improving. It runs in the system tray.

Link to comment
Share on other sites

I wrote a tool very similar to this years ago. I haven't used it in a while but maybe you'll find some use to it or get some ideas on improving. It runs in the system tray.

Thanks! I'll take a look at it. At the very least it will give me a chance to understand more of how AutoIT works, and will likely give me alternative implementation solutions.

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...