I have a script I recently created. My script is currently working great and thanks to other examples across this forum and the help file I was able to build it. HOWEVER, I feel like I could make this a lot cleaner and easier. To me it feels like I am entering the same data multiple times within the script.
I have one combo box that uses an array. I was hoping that all the other parts of my script could pull the information from the array and use it but this is where my hang up is. I am not that familiar with arrays and writing code in general, still learning =). Was wondering if I could get some input and or help on this.
Here is a little background on what my script does. I am trying to streamline a workflow I use at work. We deploy software to various hospitals and from time to time we are asked to install the same software from that hospital to our own computer for testing purposes. All that changes on my side in order to connect to a given facility is an environment change to point to the correct facilities. My utility currently creates and sets 3 environment variables appropriately. But like I stated before, I just feel like when I add new facilities to my script that I am adding it several times when it could probably all be done just once.
I currently enter the same information in the following section $aValues (this is for the combo box drop down and what the array is used for), _AllSiteManagement(for access to the sites webpage), Func _001(which is each site information individually listed).
What I would like to be able to do is have all the information in the combo box and have one "general" Func _### section. Right now, every new site I have to create a new Func _### section. Is there a way to not have to do this? Here is my current code with dummy information in its place.
Let me know if you have any suggestions or examples I may be able to try to clean this up. Any help would be greatly appreciated.
I have one combo box that uses an array. I was hoping that all the other parts of my script could pull the information from the array and use it but this is where my hang up is. I am not that familiar with arrays and writing code in general, still learning =). Was wondering if I could get some input and or help on this.
Here is a little background on what my script does. I am trying to streamline a workflow I use at work. We deploy software to various hospitals and from time to time we are asked to install the same software from that hospital to our own computer for testing purposes. All that changes on my side in order to connect to a given facility is an environment change to point to the correct facilities. My utility currently creates and sets 3 environment variables appropriately. But like I stated before, I just feel like when I add new facilities to my script that I am adding it several times when it could probably all be done just once.
I currently enter the same information in the following section $aValues (this is for the combo box drop down and what the array is used for), _AllSiteManagement(for access to the sites webpage), Func _001(which is each site information individually listed).
What I would like to be able to do is have all the information in the combo box and have one "general" Func _### section. Right now, every new site I have to create a new Func _### section. Is there a way to not have to do this? Here is my current code with dummy information in its place.
Let me know if you have any suggestions or examples I may be able to try to clean this up. Any help would be greatly appreciated.
[ autoit ]
#include <GUIConstantsEx.au3> #include <GuiButton.au3> #include <IE.au3> #include <Array.au3> Opt("GUIOnEventMode", 1) #Region ***GUI Information*** ;GUI Interface $GUI = GUICreate("Environment Variable Change", 400, 300, (@DesktopWidth - 400) / 2, (@DesktopHeight - 300) / 2) GUISetOnEvent($GUI_EVENT_CLOSE, "_Events") ;GUI Combo Box & Step 1 Label which will set your environment variables GUICtrlCreateLabel("Step 1 - Choose a site", 200, 20, 150, 25) $COMBO = GUICtrlCreateCombo("", 200, 42, 175) GUICtrlSetOnEvent($COMBO, "_Events") ;GUI Label "Other Options" GUICtrlCreateLabel("Other Options", 30, 100, 80, 25) ;GUI Button for opening the environment variables $ButtonOpenEV = GUICtrlCreateButton("Open Environment Variables", 30, 120, 160, 25) GUICtrlSetOnEvent($ButtonOpenEV, "_EnvironmentVariables") ;GUI Button for displaying current environment variables $ButtonCurrentEV = GUICtrlCreateButton("Current Set Variables", 200, 120, 160, 25) GUICtrlSetOnEvent($ButtonCurrentEV, "_CurrentSettings") ;GUI Label "Global Options" GUICtrlCreateLabel("Global Options", 30, 220, 80, 25) ;GUI Button for launching the All Site Management Link List $ButtonOpenAllSiteManagement = GUICtrlCreateButton("All Site Management Links", 30, 240, 160, 25) GUICtrlSetOnEvent($ButtonOpenAllSiteManagement, "_AllSiteManagement") ;GUI Button for clearing the environment variables $Buttonreset = GUICtrlCreateButton("Clear Variables", 200, 240, 160, 25) GUICtrlSetOnEvent($Buttonreset, "_ResetAll") GUISetState() #EndRegion #Region ***Global Environment Variables*** ;Global Environment variables Global $SystemRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" Global $UserRegKey = "HKEY_CURRENT_USER\Environment" Global $HTTPADDRESS = "_HTTPADDRESS" Global $TEXT = "_TEXT" Global $NAME = "_NAME" Global $Replace = 1 #EndRegion Global Environment Variables #Region Site List Defined ;Global setting to fill the combo with the values: Global $sComboValues, $Label_1 ;Declare the array that hold the controlID and the function to execute: ; This is the list of all Sites. If a site is added then the first $aValues within the [] will need to be increased. Currently set to 3 Global $aValues[3][2] = [["", ""], _ ["001 - Google", "_001()"], _ ["002 - Yahoo", "_002()"]];, _ For $i = 0 To UBound($aValues) -1 $sComboValues &= $aValues[$i][0] & "|" Next GUICtrlSetData($COMBO, $sComboValues, $aValues[0][0]) #EndRegion CS Site List Defined While 1 Sleep(250) WEnd Func _Events() Switch @GUI_CtrlId Case $GUI_EVENT_CLOSE Exit Case $COMBO ;read the value in the combo: Local $ValueSelected = GUICtrlRead($COMBO) ;Search the value in the array: For $i = 0 To UBound($aValues) -1 If $aValues[$i][0] = $ValueSelected Then ;Found then execute the function Return Execute($aValues[$i][1]) EndIf Next EndSwitch EndFunc Func _AllSiteManagement() ;Opens a web page list of all the Sites. Press "All Site Management Page" button. Local $s_html = "", $o_object $s_html &= "<HTML>" & @CR $s_html &= "<HEAD>" $s_html &= "<TITLE>Site Management List</TITLE>" $s_html &= "<STYLE>body {font-family: Arial}</STYLE>" $s_html &= "</HEAD>" $s_html &= "<BODY>" $s_html &= "<table border=0 id='tableOne' cellspacing=10>" $s_html &= "<tr>" $s_html &= " <td align=center><b>Site Management Page</b></td>" $s_html &= "</tr>" $s_html &= "<tr>" $s_html &= " <td><a href='http://www.google.com' target=_blank>001 - Google</a></td>" $s_html &= "</tr>" $s_html &= "<tr>" $s_html &= " <td><a href='http://www.yahoo.com' target=_blank>002 - Yahoo</a></td>" $s_html &= "</tr>" $s_html &= "</table>" $s_html &= "</BODY>" $s_html &= "</HTML>" $o_object = _IECreate() _IEDocWriteHTML($o_object, $s_html) EndFunc Func _CurrentSettings() ;Displays the Current Environment Variable settings within a msg box. Press the "Current Set Variables" button. Local $msg Local $var1 = RegRead($SystemRegKey,$HTTPADDRESS) Local $var2 = RegRead($SystemRegKey,$TEXT) Local $var3 = RegRead($SystemRegKey,$NAME) $Label_1 = MsgBox(4096,"Current Settings", $NAME & " = " & $var3 & @CRLF & "" & @CRLF & $HTTPADDRESS & " = " & $var1 & @CRLF & "" & @CRLF & $TEXT & " = " & $var2) EndFunc Func _ResetAll() ;Clears all defined Environment Variables. Press the "Clear Variables" button. RegDelete($SystemRegKey, $TEXT) ; Deletes TEXT System Variable and Value. RegDelete($SystemRegKey, $HTTPADDRESS) ; Deletes HTTPADDRESS System Variable and Value. RegDelete($SystemRegKey, $NAME) ; Deletes NAME System Variable and Value. RegDelete($UserRegKey, $TEXT) ; Deletes TEXT User Variable and Value. RegDelete($UserRegKey, $HTTPADDRESS) ; Deletes HTTPADDRESS User Variable and Value. RegDelete($UserRegKey, $NAME) ; Deletes NAME User Variable and Value. Sleep(100) EnvUpdate() MsgBox(0, "Environment", "Environment Variables cleared") EndFunc Func _EnvironmentVariables() ;Opens up System Environment Variables. Press the "Open Environment Variables" button. Run("C:\Windows\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables") EndFunc Func _Env() ;Silently opens up System Environment Variables Box and clicks OK button to double check that settings are applied. Sleep(500) EnvUpdate() Sleep(500) Run("C:\Windows\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables", @SW_HIDE) Sleep(500) Send("{ENTER}") EndFunc Func _001() Local $Value1 = "google.com" ;<<<< if this could be placed as another array variable I am ok with that i.e. ["001 - Google", "google.com", "_001()"]];, _ Local $Value2 = "c:\temp\guest001.txt" ; <<<<would like to be able to use ### in array to input after "guest", so I don't have to type again Local $Value3 = "Google" ;<<<< This is also already in the array, is there a way to auto gather this information? If $Replace = 1 Then RegWrite($SystemRegKey, $HTTPADDRESS, "REG_SZ", $Value1) RegWrite($SystemRegKey, $TEXT, "REG_SZ", $Value2) RegWrite($SystemRegKey, $NAME, "REG_SZ", $Value3) RegWrite($UserRegKey, $HTTPADDRESS, "REG_SZ", $Value1) RegWrite($UserRegKey, $TEXT, "REG_SZ", $Value2) RegWrite($UserRegKey, $NAME, "REG_SZ", $Value3) _Env() EndIf MsgBox(0, "Environment", "Environment Variables set for "&$Value3&"") EndFunc Func _002() Local $Value1 = "yahoo.com" ;<<<< if this could be placed as another array variable I am ok with that i.e. ["002 - Yahoo", "yahoo.com", "_002()"]];, _ Local $Value2 = "c:\temp\guest002.txt" ; <<<<would like to be able to use ### in array to input after "guest", so I don't have to type again Local $Value3 = "Yahoo" ;<<<< This is also already in the array, is there a way to auto gather this information? If $Replace = 1 Then RegWrite($SystemRegKey, $HTTPADDRESS, "REG_SZ", $Value1) RegWrite($SystemRegKey, $TEXT, "REG_SZ", $Value2) RegWrite($SystemRegKey, $NAME, "REG_SZ", $Value3) RegWrite($UserRegKey, $HTTPADDRESS, "REG_SZ", $Value1) RegWrite($UserRegKey, $TEXT, "REG_SZ", $Value2) RegWrite($UserRegKey, $NAME, "REG_SZ", $Value3) _Env() EndIf MsgBox(0, "Environment", "Environment Variables set for "&$Value3&"") EndFunc