Sign in to follow this  
Followers 0
S0mbre

IE/Opera Proxy Setter (GUI)

16 posts in this topic

Here's a small GUI tool to change the proxy settings for two popular web browsers: Internet Explorer and Opera. Usually, changing these settings manually (via the browsers' inbuilt dialogs) is time-consuming and (for some users) quite a 'recondite' matter.

Workflow:

Select the browser (Radiobutton group) -> enable/disable proxy by checkbox -> change proxy server and port -> click "Apply" to apply the settings to the selected browser.

Features:

* No tweaks to the code are required: all the global file and registry paths and settings are retrieved from the Windows registry and should be the same on every Windows machine

* Reads the current settings from the browsers (from the registry for IE and from the ini file for Opera)

* The server and port edits are protected by a checkbox "Change proxy server and port" to prevent accidental change

* Option to restart browser after applying changes

* Confirmation to apply unapplied changed on closing the GUI

Plans for the future include adding more browsers (Safari, Chrome, Mozilla). Enjoy! :graduated:

The code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Local $tOperaCommand = StringSplit(RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", ""), """")
If $tOperaCommand[1] == "" Then
    $OPERA_EXE = $tOperaCommand[2]
Else
    $OPERA_EXE = $tOperaCommand[1]
EndIf

Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")

Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $ProxyEnabled_ie = $IE_PROXY_ENABLE, $Restart_op = True, $Restart_ie = True

Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")

Global $Proxy_op, $Port_op, $Proxy_ie, $Port_ie

Global $CHANGED_IE = 0, $CHANGED_OP = 0

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    $Proxy_op = $tProxyPort_op[1]
    $Port_op = $tProxyPort_op[2]

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        $PID = ProcessExists("opera.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $Enable Then
            IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
            IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
            IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
            IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
        Else
            IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
            IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
            IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
            IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
        EndIf

        If Not $ServerAndPort = "" Then
            IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
            IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
            IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
            IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            GUICtrlSetState($rb_opera, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
            GUICtrlSetData($gui_ed_Port, $Port_op)

            If $OP_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites



$USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"

I have single user installation, so this will not work.

Check out my Opera Library, there is functions to get opera install dir, settings file, and many other stuff (see the UDF's header).


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 1)
            Else
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 0)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 0)
                IniWrite($OPERA_INI, "Proxy", "Use GOPHER", 0)
            EndIf

            If Not $ServerAndPort = "" Then
                IniWrite($OPERA_INI, "Proxy", "HTTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "HTTPS server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "FTP server", $ServerAndPort)
                IniWrite($OPERA_INI, "Proxy", "Gopher server", $ServerAndPort)
            EndIf
        EndIf

    Else
        Return
    EndIf

    If $Restart Then
        StartBrowser($br)
    EndIf

EndFunc   ;==>SetProxy


;==================================================================================
;--------------------------------------------------------------
;==================================================================================
InitializeProxy()

$gui = GUICreate("Proxy Setter", 200, 200)

GUICtrlCreateGroup("Choose browser:", 10, 10, 180, 60)
$rb_ie = GUICtrlCreateRadio("Internet Explorer", 20, 25, 100, 20)
GUICtrlSetState($rb_ie, $GUI_CHECKED)
$rb_opera = GUICtrlCreateRadio("Opera", 20, 45, 60, 20)
If $bInstalled_op = False Then
    GUICtrlSetPos($rb_opera, 20, 45, 150, 20)
    GUICtrlSetData($rb_opera, "Opera [NOT INSTALLED]")
    GUICtrlSetState($rb_opera, $GUI_DISABLE)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

$gui_chb_Enable = GUICtrlCreateCheckbox("Enable proxy", 5, 75, 180, 20)
If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf
$gui_chb_Set = GUICtrlCreateCheckbox("Set proxy server and port:", 5, 95, 180, 20)
$gui_ed_Proxy = GUICtrlCreateEdit($Proxy_ie, 5, 115, 120, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_ed_Port = GUICtrlCreateEdit($Port_ie, 130, 115, 60, 20, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
$gui_chb_Restart = GUICtrlCreateCheckbox("Restart browser", 5, 140, 170, 20)
$gui_b_Apply = GUICtrlCreateButton("Apply", 10, 165, 85, 25)
$gui_b_Close = GUICtrlCreateButton("Close", 105, 165, 85, 25)


GUICtrlSetState($gui_chb_Set, $GUI_UNCHECKED)
GUICtrlSetState($gui_chb_Restart, $GUI_CHECKED)
GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)

If $IE_PROXY_ENABLE = 1 Then
    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
Else
    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
EndIf

GUISetState(@SW_SHOW)

While 1

    $msg = GUIGetMsg()

    Select
        Case $msg = $rb_ie
            ; ie
            GUICtrlSetState($rb_ie, $GUI_CHECKED)
            GUICtrlSetData($gui_ed_Proxy, $Proxy_ie)
            GUICtrlSetData($gui_ed_Port, $Port_ie)

            If $IE_PROXY_ENABLE = 1 Then
                GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
            Else
                GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
            EndIf

        Case $msg = $rb_opera
            ;  opera
            If $bInstalled_op Then
                GUICtrlSetState($rb_opera, $GUI_CHECKED)
                GUICtrlSetData($gui_ed_Proxy, $Proxy_op)
                GUICtrlSetData($gui_ed_Port, $Port_op)

                If $OP_PROXY_ENABLE = 1 Then
                    GUICtrlSetState($gui_chb_Enable, $GUI_CHECKED)
                Else
                    GUICtrlSetState($gui_chb_Enable, $GUI_UNCHECKED)
                EndIf
            EndIf

        Case $msg = $gui_chb_Enable
            $chbEnable_State = GUICtrlRead($gui_chb_Enable)
            If $chbEnable_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = True
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = True
                    $CHANGED_OP = 1
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $ProxyEnabled_ie = False
                    $CHANGED_IE = 1
                Else
                    $ProxyEnabled_op = False
                    $CHANGED_OP = 1
                EndIf
            EndIf

        Case $msg = $gui_chb_Set
            $chbSet_State = GUICtrlRead($gui_chb_Set)
            If $chbSet_State = $GUI_CHECKED Then
                GUICtrlSetState($gui_ed_Proxy, $GUI_ENABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_ENABLE)
            Else
                GUICtrlSetState($gui_ed_Proxy, $GUI_DISABLE)
                GUICtrlSetState($gui_ed_Port, $GUI_DISABLE)
            EndIf

        Case $msg = $gui_ed_Proxy
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Proxy_ie = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_IE = 1
            Else
                $Proxy_op = GUICtrlRead($gui_ed_Proxy)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_ed_Port
            If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                $Port_ie = GUICtrlRead($gui_ed_Port)
                $CHANGED_IE = 1
            Else
                $Port_op = GUICtrlRead($gui_ed_Port)
                $CHANGED_OP = 1
            EndIf

        Case $msg = $gui_chb_Restart
            $chbRestart_State = GUICtrlRead($gui_chb_Restart)
            If $chbRestart_State = $GUI_CHECKED Then
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = True
                Else
                    $Restart_op = True
                EndIf
            Else
                If GUICtrlRead($rb_ie) = $GUI_CHECKED Then
                    $Restart_ie = False
                Else
                    $Restart_op = False
                EndIf
            EndIf

        Case $msg = $gui_b_Apply
            Local $b = 1
            Local $prx_on = $ProxyEnabled_ie
            Local $rest = $Restart_ie
            Local $prx = $Proxy_ie
            Local $prt = $Port_ie

            Local $browser = GUICtrlRead($rb_opera)
            If $browser = $GUI_CHECKED Then
                $b = 2
                $prx_on = $ProxyEnabled_op
                $rest = $Restart_op
                $prx = $Proxy_op
                $prt = $Port_op
            EndIf

            SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)

        Case $msg = $gui_b_Close
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            If ($CHANGED_IE = 1) Or ($CHANGED_OP = 1) Then
                Local $id = MsgBox(4, "Confirmation", "Apply changes before quit?")
                If $id = 6 Then
                    Local $b = 1
                    Local $prx_on = $ProxyEnabled_ie
                    Local $rest = $Restart_ie
                    Local $prx = $Proxy_ie
                    Local $prt = $Port_ie

                    Local $browser = GUICtrlRead($rb_opera)
                    If $browser = $GUI_CHECKED Then
                        $b = 2
                        $prx_on = $ProxyEnabled_op
                        $rest = $Restart_op
                        $prx = $Proxy_op
                        $prt = $Port_op
                    EndIf
                    SetProxy($b, $prx_on, $rest, $prx & ":" & $prt)
                EndIf
            EndIf
            ExitLoop

    EndSelect
WEnd

Share this post


Link to post
Share on other sites

SOmbre,

The script falls over at line 21 if Opera is not installed. You might want to consider some (more) error checking in your code.

4Eyes

Hi! I've improved the code. Now Opera installation check is added and the code doesn't crash.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ProxySetter.exe
#AutoIt3Wrapper_Compression=3
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; CHANGE PROXY SETTINGS
;==================================================================================
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
;==================================================================================
Global Const $USER_APPDATA_DIR = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "AppData")
Global Const $REG_IESETTINGS = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Global $bInstalled_op = True

Global Const $IE_EXE = "iexplore.exe"
Global $OPERA_EXE

Global Const $IE_PROXYPORT = RegRead($REG_IESETTINGS, "ProxyServer")
Global Const $IE_PROXY_ENABLE = RegRead($REG_IESETTINGS, "ProxyEnable")
Global $tProxyPort_ie = StringSplit($IE_PROXYPORT, ":")
Global $Proxy_ie, $Port_ie, $Restart_ie = True, $ProxyEnabled_ie = $IE_PROXY_ENABLE
Global $CHANGED_IE = 0, $CHANGED_OP = 0

Local $OperaCommand = RegRead("HKEY_CLASSES_ROOT\Applications\Opera.exe\shell\open\command", "")
If $OperaCommand <> "" Then
    Local $tOperaCommand = StringSplit($OperaCommand, """")
    If $tOperaCommand[1] == "" Then
        $OPERA_EXE = $tOperaCommand[2]
    Else
        $OPERA_EXE = $tOperaCommand[1]
    EndIf

    Global $OPERA_INI = $USER_APPDATA_DIR & "\Opera\Opera\operaprefs.ini"
    Global Const $OP_PROXYPORT = IniRead($OPERA_INI, "Proxy", "HTTP server", $IE_PROXYPORT)
    Global Const $OP_PROXY_ENABLE = IniRead($OPERA_INI, "Proxy", "Use HTTP", $IE_PROXY_ENABLE)

    Global $ProxyEnabled_op = $IE_PROXY_ENABLE, $Restart_op = True

    Global $tProxyPort_op = StringSplit($OP_PROXYPORT, ":")
    Global $Proxy_op, $Port_op
Else
    $bInstalled_op = False
EndIf

Global $gui = 0, $rb_ie = 0, $rb_opera = 0, $gui_chb_Enable = 0, $gui_chb_Set = 0, $gui_ed_Proxy = 0, $gui_ed_Port = 0, $gui_chb_Restart = 0, _
        $gui_b_Apply = 0, $gui_b_Close = 0

;==================================================================================
Func InitializeProxy()
    ; ie
    $Proxy_ie = $tProxyPort_ie[1]
    $Port_ie = $tProxyPort_ie[2]
    ; opera
    If $bInstalled_op Then
        $Proxy_op = $tProxyPort_op[1]
        $Port_op = $tProxyPort_op[2]
    EndIf

EndFunc   ;==>InitializeProxy
;==================================================================================
Func KillBrowser($br)
    ; Close browser if it's running
    Local $PID

    If $br = 2 Then ; opera
        If $bInstalled_op Then
            $PID = ProcessExists("opera.exe")
            If $PID Then
                ProcessClose($PID)
            EndIf
        EndIf

    ElseIf $br = 1 Then ; ie
        $PID = ProcessExists("iexplore.exe")
        If $PID Then
            ProcessClose($PID)
        EndIf

    Else
        Return
    EndIf
EndFunc   ;==>KillBrowser
;==================================================================================
Func StartBrowser($br)
    If $br = 2 Then ; opera
        If $bInstalled_op Then ShellExecute($OPERA_EXE)

    ElseIf $br = 1 Then ; ie
        ShellExecute($IE_EXE)
    EndIf
EndFunc   ;==>StartBrowser
;==================================================================================
Func SetProxy($br, $Enable, $Restart, $ServerAndPort = "")

    KillBrowser($br)

    If $br = 1 Then ; ie
        If $Enable Then
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x1)
        Else
            RegWrite($REG_IESETTINGS, "ProxyEnable", "REG_DWORD", 0x0)
        EndIf

        If Not $ServerAndPort = "" Then
            RegWrite($REG_IESETTINGS, "ProxyServer", "REG_SZ", $ServerAndPort)
        EndIf

    ElseIf $br = 2 Then ; opera
        If $bInstalled_op Then
            If $Enable Then
                IniWrite($OPERA_INI, "Proxy", "Use HTTP", 1)
                IniWrite($OPERA_INI, "Proxy", "Use HTTPS", 1)
                IniWrite($OPERA_INI, "Proxy", "Use FTP", 1)
      &nbs