Jump to content

Help with my arrays and streamlining information


Recommended Posts

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.

#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
Link to comment
Share on other sites

Hey,

I'm not sure I understood exactly what you wanted since you gave so much informations so feel free to correct me if I misunderstood :)

First of all, using a 2D array is not needed I think. You use the second dimension to store site numbers ("_001", "_002, ...) but you can already guess that from the first dimension.

For example, if you use this array:

Global $aValues[3]

$aValues[0] = ""
$aValues[1] = "Google"
$aValues[2] = "Yahoo"

Since "Google" is stored in [1] position in the array, it means it is the site number one!

You can rewrite your "_AllSiteManagement" function this way:

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

for $i = 1 to ubound($aValues) step 1

$s_html &= "<tr>"
$s_html &= '<td><a href="http://www.' & $avalues[$i] & '.com" target=_blank>00' & $i & ' - ' & $avalues[$i] & '</a></td>'
$s_html &= "</tr>"

next

$s_html &= "</table>"
$s_html &= "</BODY>"
$s_html &= "</HTML>"
$o_object = _IECreate()
_IEDocWriteHTML($o_object, $s_html)
EndFunc

For your "_001", "_002"... functions, you need to use the function parameter for sending the site number that you want. For example:

Func set_env_variables($sitenumber)
Local $Value1 = $avalues[$sitenumber] & ".com"
Local $Value2 = "c:\temp\guest00" & $sitenumber &".txt"
Local $Value3 = $avalues[$sitenumber]
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

If you call "set_env_variables(1)" in your script, it will be the same as your "_001" function.

If you call "set_env_variables(2)" in your script, it will be the same as your "_002" function.

Is this what you wanted?

Edited by Neutro
Link to comment
Share on other sites

Thanks for the reply Neutro, I tried swapping my stuff out with what you suggested but I can't get my drop down box values to generate anything now. I had to change some of the other code just to get them to appear in the dropdown. Did it work for you?

Also I think I understand the concept you were trying to relay to me but I pose this question. What would I do if the array values aren't in sequential order. Meaning, 1 = google, 2 =yahoo, 7 = msn, 11 = autoit... and so on? Would your logic still place the correct "site #" values?

Link to comment
Share on other sites

In that case you would need a column to keep as a sorting index. So if it's required to have Google as 1, yahoo as 2, etc, and you don't necessarily enter them into the array in that order, then you'll have to do it the way you were (with a 2D array.)

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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