Bert Posted July 11, 2006 Share Posted July 11, 2006 In building a GUI that would allow for me to add and delete printers, I had assistance from several people. I like to say that these people are very skilled, and are far better at AutoIt than me. In saying this, I came to the conclusion that my coding skills need work, and put simply, I need help. To give an example: Mine, which is done with many repetive items, and is quite messy: CODE#include <GuiConstants.au3> #include <Process.au3> #include <Array.au3> AutoItSetOption("TrayIconDebug", 1) ;debug is on DIM $hService, $sPrinter, $sPrinterList, $data GuiCreate("", 600, 290,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS)) GuiCtrlCreateLabel("//", 20, 52, 10, 20) $iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "") $iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "") $Input_1 = GuiCtrlCreateInput($iniread_1, 35, 50, 100, 20) $Combo_2 = GuiCtrlCreateCombo("", 20, 75, 170, 21) $Combo_3 = GuiCtrlCreateCombo("", 20, 100, 170, 21) $Combo_4 = GuiCtrlCreateCombo("", 20, 125, 170, 21) $Combo_5 = GuiCtrlCreateCombo("", 20, 150, 170, 21) $Combo_6 = GuiCtrlCreateCombo("", 20, 175, 170, 21) $Combo_7 = GuiCtrlCreateCombo("", 20, 200, 170, 21) $radio_1 = GUICtrlCreateRadio("", 210, 75) $radio_2 = GUICtrlCreateRadio("", 210, 100) $radio_3 = GUICtrlCreateRadio("", 210, 125) $radio_4 = GUICtrlCreateRadio("", 210, 150) $radio_5 = GUICtrlCreateRadio("", 210, 175) $radio_6 = GUICtrlCreateRadio("", 210, 200, 18, 18) $input_2 = GuiCtrlCreateInput("", 270, 25, 270, 21) $input_3 = GuiCtrlCreateInput("", 270, 50, 270, 21) $input_4 = GuiCtrlCreateInput("", 270, 75, 270, 21) $input_5 = GuiCtrlCreateInput("", 270, 100, 270, 21) $input_6 = GuiCtrlCreateInput("", 270, 125, 270, 21) $input_7 = GuiCtrlCreateInput("", 270, 150, 270, 21) $input_8 = GuiCtrlCreateInput("", 270, 175, 270, 21) $input_9 = GuiCtrlCreateInput("", 270, 200, 270, 21) $input_10 = GuiCtrlCreateInput("", 270, 225, 270, 21) $input_11 = GuiCtrlCreateInput("", 270, 250, 270, 21) $check_1 = GUICtrlCreateCheckbox("", 550, 25, 20, 20) $check_2 = GUICtrlCreateCheckbox("", 550, 50, 20, 20) $check_3 = GUICtrlCreateCheckbox("", 550, 75, 20, 20) $check_4 = GUICtrlCreateCheckbox("", 550, 100, 20, 20) $check_5 = GUICtrlCreateCheckbox("", 550, 125, 20, 20) $check_6 = GUICtrlCreateCheckbox("", 550, 150, 20, 20) $check_7 = GUICtrlCreateCheckbox("", 550, 175, 20, 20) $check_8 = GUICtrlCreateCheckbox("", 550, 200, 20, 20) $check_9 = GUICtrlCreateCheckbox("", 550, 225, 20, 20) $check_10 = GUICtrlCreateCheckbox("", 550, 250, 20, 20) GUICtrlCreateLabel("Currently install printers", 270, 3, 200, 21) GUICtrlCreateLabel("Delete", 550, 7, 100, 21) GUICtrlCreateGroup("Default", 200, 60, 50, 162) guictrlsetdata($Combo_2, $iniread_2) guictrlsetdata($Combo_3, $iniread_2) guictrlsetdata($Combo_4, $iniread_2) guictrlsetdata($Combo_5, $iniread_2) guictrlsetdata($Combo_6, $iniread_2) guictrlsetdata($Combo_7, $iniread_2) $Button_11 = GuiCtrlCreateButton("Run", 20, 225, 80, 30) $Button_12 = GuiCtrlCreateButton("Exit", 117, 225, 73, 30) $button_14a = GuiCtrlCreateButton("Find", 146, 50, 43, 20) $Label_15 = GuiCtrlCreateLabel(" Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 240, 50) $font="Comic Sans MS" GUICtrlSetFont($Label_15, 10, 400, "", $font) $Label_16 = GuiCtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25) $_dummy = GUICtrlCreateDummy() GuiSetState() While 1 $msg = GuiGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case $msg = $Button_11 ;MsgBox(0, "", GUICtrlRead($check_1)) _Rprinter() _instprint() ;~ Beep(500, 500) ;~ sleep(500) ;~ Beep(500, 500) ;~ sleep(500) ;~ Beep(500, 500) ;~ sleep(500) MsgBox(0, "", "Done!", 3) Case $msg = $button_14a $data = GUICtrlRead($Input_1, 0) MsgBox(0, "", "This may take a moment...Please wait until the search for printer share is complete.", 2) _netprint1() Case $msg = $Button_12 Exit Case Else ;;; EndSelect WEnd Exit big_daddy's code. Shorter, and much cleaner: CODE#include <GuiConstants.au3> #include <Process.au3> #include <Array.au3> AutoItSetOption("TrayIconDebug", 1) ;debug is on Dim $hService, _ $data, _ $sPrinter, _ $sPrinterList, _ $font = "Comic Sans MS", _ $Input[12], $Combo[7], $radio[7], _ $check[11], $Button[4] Global Enum Step + 25 _ $CON_POS_1 = 25, _ $CON_POS_2, _ $CON_POS_3, _ $CON_POS_4, _ $CON_POS_5, _ $CON_POS_6, _ $CON_POS_7, _ $CON_POS_8, _ $CON_POS_9, _ $CON_POS_10, _ $CON_POS_11 GUICreate("", 600, 290, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS)) GUICtrlCreateLabel("\\", 20, 54, 10, 20) $iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "") $iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "") $Input[1] = GUICtrlCreateInput($iniread_1, 35, 50, 100, 20) For $i = 2 To 11 $Input[$i] = GUICtrlCreateInput("", 270, Eval("CON_POS_" & $i - 1), 270, 21) Next For $i = 1 To 6 $Combo[$i] = GUICtrlCreateCombo("", 20, Eval("CON_POS_" & $i + 2), 170, 21) GUICtrlSetData($Combo[$i], $iniread_2) $radio[$i] = GUICtrlCreateRadio("", 210, Eval("CON_POS_" & $i + 2), 15, 21) Next For $i = 1 To 10 $check[$i] = GUICtrlCreateCheckbox("", 550, Eval("CON_POS_" & $i), 20, 20) Next GUICtrlCreateLabel("Currently install printers", 270, 3, 200, 21) GUICtrlCreateLabel("Delete", 550, 7, 100, 21) GUICtrlCreateGroup("Default", 200, 62, 50, 160) $Button[1] = GUICtrlCreateButton("Run", 20, 225, 80, 30) $Button[2] = GUICtrlCreateButton("Exit", 117, 225, 73, 30) $Button[3] = GUICtrlCreateButton("Find", 146, 50, 43, 20) $Label_1 = GUICtrlCreateLabel(" Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 240, 50) GUICtrlSetFont(-1, 10, 400, "", $font) $Label_2 = GUICtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25) $_dummy = GUICtrlCreateDummy() ;_getinstalledPrinters() GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $Button[1] ;_Rprinter() ;_instprint() MsgBox(0, "", "Done!", 3) Case $Button[3] $data = GUICtrlRead($Input[1], 0) MsgBox(0, "", "This may take a moment...Please wait until the search for printer share is complete.", 2) _netprint1() Case $Button[2] ExitLoop Case Else ;;; EndSwitch WEnd Exit You will notice how big_daddy used arrays to make the GUI. I was blown away by this. It never occurred to me that a GUI could be built this way. I made a test to quiz ones knowledge on coding, but hindsight being 20-20, what I did provide to educate others is woefully inadequate. What would be nice would be a sticky that provides links to resources that taught coding, advanced tricks, like what big_daddy demonstrated above, and so forth. My question is this: Can anyone recommend resources that would be good in understanding programming in Basic? I know you can Google, but Google can't disseminate what is a good site, and what is a waste of time. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Bert Posted July 12, 2006 Author Share Posted July 12, 2006 In learning something new, here is the finished program. I hope it is bug free, but if it isn't, hopefully it can be fixed. Again, Thanks to everyone who help with this. Note: If someone could figure out how to get this to work with a Novell servers, that would be great. I tried suggestions from both Valuater and GaFrost, but neither worked. CODE#cs Network Printer Utility 1.0 beta Designed by the CTS Script team July 2006 A big THANK YOU goes to big_daddy. His help was invaluable in fixing some issues. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ History July 2006: Initial release. --------------- Known issues: Will not work on Novell servers. #ce#include <GuiConstants.au3>#include <Process.au3>#include <Array.au3>Opt("TrayIconDebug", 1) ;debug is onDim $hService, _ $data, _ $sPrinter, _ $sPrinterList, _ $font = "Comic Sans MS", _ $Input[12], $Combo[7], $radio[7], _ $check[11], $Button[6]Global Enum Step + 25 _ $CON_POS_1 = 25, _ $CON_POS_2, _ $CON_POS_3, _ $CON_POS_4, _ $CON_POS_5, _ $CON_POS_6, _ $CON_POS_7, _ $CON_POS_8, _ $CON_POS_9, _ $CON_POS_10, _ $CON_POS_11GUICreate("", 600, 290, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))GUICtrlCreateLabel("\\", 20, 54, 10, 20)$iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "")$iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")iniwrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")$iniread_3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")$LocalPrinters = StringSplit($iniread_3, "|")If $LocalPrinters[0] > 10 Then ReDim $LocalPrinters[11]EndIf$Input[1] = GUICtrlCreateInput($iniread_1, 35, 50, 100, 20)For $i = 2 To 11 $Input[$i] = GUICtrlCreateInput("", 270, Eval("CON_POS_" & $i - 1), 270, 21) If ($i - 1) <= $LocalPrinters[0]Then GUICtrlSetData(-1, $LocalPrinters[$i - 1]) EndIfNextFor $i = 1 To 6 $Combo[$i] = GUICtrlCreateCombo("", 20, Eval("CON_POS_" & $i + 2), 170, 21) GUICtrlSetData(-1, $iniread_2) $radio[$i] = GUICtrlCreateRadio("", 210, Eval("CON_POS_" & $i + 2), 15, 21)NextFor $i = 1 To 10 $check[$i] = GUICtrlCreateCheckbox("", 550, Eval("CON_POS_" & $i), 20, 20)NextGUICtrlCreateLabel("Currently install printers", 270, 3, 200, 21)GUICtrlCreateLabel("Delete", 550, 7, 100, 21)GUICtrlCreateGroup("Default", 200, 62, 50, 160)$Button[1] = GUICtrlCreateButton("Run", 20, 225, 35, 30)$Button[2] = GUICtrlCreateButton("Exit", 65, 225, 35, 30)$Button[3] = GUICtrlCreateButton("Find", 146, 50, 43, 20)$Button[4] = GUICtrlCreateButton("Find", 498, 3, 43, 20)$Button[5] = GUICtrlCreateButton("Restart Spooler", 110, 225, 80, 30)$Label_1 = GUICtrlCreateLabel(" Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 240, 50)GUICtrlSetFont(-1, 10, 400, "", $font)$Label_2 = GUICtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25)$_dummy = GUICtrlCreateDummy()GUISetState()While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE ExitLoop Case $Button[1] _Rprinter() _instprint() MsgBox(0, "", "Done!", 3) Case $Button[2] ExitLoop Case $Button[3] $data = GUICtrlRead($Input[1], 0) _netprint1() Case $Button[4] _getinstalledPrinters() Case $Button[5] _restart() Case Else ;;; EndSwitchWEndExitFunc _netprint1() $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & $data & "\root\cimv2") If @error Then MsgBox(48, "ERROR", "No Printers Found. Possible issues: " & @CRLF _ & "" & @CRLF _ & " 1. The Windows Print Server name has been entered in incorrectly." & @CRLF _ & " 2. You are trying to access a Novell Server. This utility does not support Novell Print Servers." & @CRLF _ & " 3. There are no printers shared on the Windows Print Server you selected.") Else _np2() EndIfEndFunc ;==>_netprint1Func _np2() Local $i = 0 Local $sPrinters = "" Local $aArray[1] SplashTextOn("", "Gathering printers, please wait...", 150, 75) For $y = 1 To 6 GUICtrlSetData($Combo[$y], "") Next IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "") $oPrinterList = $hService.ExecQuery ("Select * From Win32_Printer") For $oPrinter In $oPrinterList ReDim $aArray[$i + 1] $aArray[$i] = StringUpper($oPrinter.name) $i += 1 Next _ArraySort($aArray) For $i = 0 To UBound($aArray)-1 For $y = 1 To 6 GUICtrlSetData($Combo[$y], $aArray[$i]) Next Next $iElements = UBound($aArray) - 1 For $x = 0 To $iElements If $x = $iElements Then $sPrinters &= $aArray[$x] Else $sPrinters &= $aArray[$x] & "|" EndIf Next IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $sPrinters) $dataget2 = GUICtrlRead($Input[1], 0) IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2) SplashTextOn("", "Select what printers you wish to install", 150, 75) Sleep(2000) SplashOff()EndFunc ;==>_np2Func _instprint() Local $ComboGet[7], $RadioGet[7] SplashTextOn("", "Installing printer, please wait...", 150, 75) $InputGet = GUICtrlRead($Input[1], 0) For $i = 1 To 6 $ComboGet[$i] = GUICtrlRead($Combo[$i], 0) $RadioGet[$i] = GUICtrlRead($radio[$i], 0) Next For $i = 1 To 6 If $ComboGet[$i] <> "" Then RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /ga /c\\" & @ComputerName & " /n\\" & $InputGet & "\" & $ComboGet[$i]) EndIf Next _RunDOS("NET STOP spooler") _RunDOS("NET START spooler") sleep(10000) For $i = 1 To 6 If $RadioGet[$i] = 1 Then If $ComboGet[$i] <> "" Then RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n\\"&$iniread_1&"\"& $ComboGet[$i]) endif EndIf GUICtrlSetData($Combo[$i],"") GUICtrlSetState($radio[$i], $GUI_UNCHECKED) sleep(50) GUICtrlSetData($Combo[$i],$iniread_2) Next SplashOff()EndFunc ;==>_instprintfunc _restart() SplashTextOn("", "Stopping Print Spooler, please wait...", 150, 75) _RunDOS("NET STOP spooler") SplashOff() SplashTextOn("", "Starting Print Spooler, please wait...", 150, 75) _RunDOS("NET START spooler") SplashOff()EndFunc Func _getinstalledPrinters() SplashTextOn("", "Gathering installed printers, please wait...", 200, 75) IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "") $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2") $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer") ConsoleWrite('$sPrinterList.count = ' & $sPrinterList.count & @LF) For $sPrinter In $sPrinterList ConsoleWrite('$sPrinter.name = ' & $sPrinter.name & @LF) $rd3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "") IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", $rd3 & $sPrinter.name & "|") Next $local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|") If UBound($local) < 11 Then ReDim $local[11] For $i = 1 To 10 GUICtrlSetData($Input[$i + 1], $local[$i]) Next SplashOff()EndFunc ;==>_getinstalledPrintersFunc _Rprinter() Local $InputGet[11] For $i = 2 To 11 $InputGet[$i - 1] = GUICtrlRead($Input[$i]) Next For $i = 1 To 10 If GUICtrlRead($check[$i]) = 1 Then SplashTextOn("", "Removing printer, please wait...", 150, 50) GUICtrlSetState($check[$i], $GUI_UNCHECKED) sleep(1000) If $InputGet[$i] <> "" Then $str = StringLeft($InputGet[$i], 2) If $str = "\\" Then RunWait(@SystemDir & "\RUNDLL32 PRINTUI.DLL,PrintUIEntry /gd /q /c\\" & @ComputerName & " /n" & $InputGet[$i]) GUICtrlSetData($InputGet[$i], "") Else RunWait(@SystemDir & '\RUNDLL32 PRINTUI.DLL,PrintUIEntry /dl /q /n "' & $InputGet[$i] & '" /c\\' & @ComputerName) EndIf EndIf EndIf Next EndFunc ;==>_Rprinter The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now