Jump to content

abberration

Active Members
  • Posts

    557
  • Joined

  • Last visited

Community Answers

  1. abberration's post in autoit wiki guideline clarification was marked as the answer   
    Hello, 
    I am not the best person to answer your questions, but I have been using AutoIt for years. I have read over the best practices guidelines, but that has only been recently (in the past 6 months or so). I have posted many scripts in the past and no one has ever mentioned that I wasn't adhering to the best practices. They are good rules to follow and can save some headaches, because the rules have pointed out some things that can break a script and leave you scratching your head. 
    Your questions about variables are taken from Best Coding Practices and UDF Specific are talking about different things. In Best Coding Practices, I think it simply means not to initialize all variables at the beginning of your script, meaning if you have functions that will have local variables that won't be used anywhere else, declare them inside those functions. As for the UDF Specific, it is talking about the best consistency for writing a UDF so other people will not get confused. I think UDF guidelines are important because other people may need to modify your UDF for their own needs or may continue or fix a UDF if it is abandoned and never updated. The UDF guide simply states the order and structure in which things should be listed. The best example of a UDF is at the bottom of that page called Template UDF. 
    As for your question about the +, | and - in UDF headers, the Template UDF shows examples of the - and | being used in the tables for Parameters and Return values:
    ; Parameters ....: $avAnArray - [byref] An array of anything. The value of anything is changed and passed out using this ; parameter. The array should only have one dimension ; $iAnInt - An integer that does very little. ; $hAHandle - [optional] A handle. Default is zero. ; $nSomeNumber - [optional] A number of some kind. Default is 42. ; Return values .: Success - A MYSTRUCT structure. ; Failure - Returns zero and sets the @error flag: ; |1 - The $avAnArray is invalid. As for your third question about the order of things listed in an UDF, look at the Template UDF again and it is consistent with the statements that #Index# comes first, then other sections, which includes the listing of functions and structures immediately after #Index#. The guide probably should have put that section at the top of of the Other Sections, but they did get it correct in the Template UDF. 
    Personally, I wouldn't worry about the UDF stuff until you are good at coding and are ready to write and share UDFs with other people. 
    The best practices are great if you plan to share your code on the forum or if you plan to use your scripts professionally (i.e., at your job), but if you are just scripting for your own use, as Jos said, just code for yourself. For myself, I think the best advice I can give is to write code that isn't sloppy and to make good comments to remind yourself why you did something or what a piece of code does. I have spent a lot of time trying to figure out what my old code did. Just like the help file is an incredible piece of documentation, your comments are also great documentation. 
    I hope this helps. If anything is still unclear, I will try to explain further.
    Cheers!
  2. abberration's post in Running program (cleanmgr) works differently in autoit than command prompt or otherwise was marked as the answer   
    I think this has to do with #RequireAdmin, which will run the script as an administrator. Try creating the Sageset configuration the exact same way that you run the command. I have created a small example where you can create the command and run it exactly the same way. Maybe it will help? (oh, I prefer shellexecute over run).
    #RequireAdmin #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 270, 179) $Label1 = GUICtrlCreateLabel("Pick a task:", 16, 16, 60, 17) $Radio1 = GUICtrlCreateRadio("Set Sageset task", 16, 40, 113, 17) $Label2 = GUICtrlCreateLabel("Task #", 48, 64, 38, 17) GUICtrlSetState($Label2, $GUI_DISABLE) $Input1 = GUICtrlCreateInput("", 96, 64, 49, 21) GUICtrlSetState($Input1, $GUI_DISABLE) $Radio2 = GUICtrlCreateRadio("Run Sagetask:", 16, 104, 113, 17) GUICtrlSetState($Radio2, $GUI_CHECKED) $Label3 = GUICtrlCreateLabel("Task # ", 48, 128, 41, 17) $Input2 = GUICtrlCreateInput("", 96, 128, 49, 21) GUICtrlSetState($Input2, $GUI_FOCUS) $Button1 = GUICtrlCreateButton("Go!", 176, 104, 75, 49, $WS_GROUP) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Radio1 GUICtrlSetState($Label2, $GUI_ENABLE) GUICtrlSetState($Input1, $GUI_ENABLE) GUICtrlSetState($Label3, $GUI_DISABLE) GUICtrlSetState($Input2, $GUI_DISABLE) GUICtrlSetState($Input1, $GUI_FOCUS) Case $Radio2 GUICtrlSetState($Label3, $GUI_ENABLE) GUICtrlSetState($Input2, $GUI_ENABLE) GUICtrlSetState($Label2, $GUI_DISABLE) GUICtrlSetState($Input1, $GUI_DISABLE) GUICtrlSetState($Input2, $GUI_FOCUS) Case $Button1 $iRun = GUICtrlRead($Radio1) If $iRun = 4 Then ShellExecute("cleanmgr", "/SAGERUN:1") Else ShellExecute("cleanmgr", "/sageset:1") EndIf Exit EndSwitch WEnd  
  3. abberration's post in Compile from command line - no /language switch? was marked as the answer   
    Thanks, argumentum!
    AutoIt3Wrapper.au3 compiles it silently and uses the wrappers. Perfect solution. All I need is to shellexecute it and run the specify the script. 
  4. abberration's post in StringFormat, to one decimal place, unless there is a 0 after the decimal point was marked as the answer   
    Local $double = 10.5, $fDouble $fDouble = StringFormat(IsFloat ($double)?"%05.1f":"%05i", $double) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $fDouble = ' & $fDouble & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console  
    Ternary operators
  5. abberration's post in Ticking checkboxes with specific names. was marked as the answer   
    Hi, I'm assuming that you are blanking out portions of your screenshots because you think they may compromise your security. Not knowing what application you are trying to install, and it does not appear that you are trying to do anything malicious, I can offer what I think would click the register button for you. 
    In the following code, replace "BLANK" with what the AutoIt Window Info's title box says.
    ControlClick ("BLANK", "Whitelist Creation", "[CLASS:Button; INSTANCE:2]") If this works, you should be able to figure out the rest for yourself.
  6. abberration's post in Problem with GUICtrlRead in second GUI was marked as the answer   
    OK, I think I understand the situation a little better. GUI1 should be your main GUI and GUI2 should be the radio selection (I think). Therefore you might want to structure your GUI2 to do all the reading file locations, making the radios and writing to the INI. Just for an example, I had the following code to write your selection in an INI in the script directory (and a little sample code (_WriteToINI) if you want to put INI writing off to another function). I might be wrong about all this, but here's an idea of how this might work...
    #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 400, 278, 192, 124) $Button1 = GUICtrlCreateButton("Choose", 264, 208, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _CreateRadios() EndSwitch WEnd Func _CreateRadios() $Form2 = GUICreate("Form2", 300, 293, 700, 218) $top = 10 $aArray = _FileListToArrayRec(@ScriptDir, "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT) For $ar = 1 To $aArray[0] Assign("Radio" & $ar, GUICtrlCreateRadio($aArray[$ar], 10, $top, 350, 20)) $top += 30 Next $Button2 = GUICtrlCreateButton("Select", 136, 136, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($Form2) ExitLoop Case $Button2 For $ar = 1 To $aArray[0] If GUICtrlRead(Eval("Radio" & $ar)) = $GUI_CHECKED Then IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", GUICtrlRead(Eval("Radio" & $ar), 1)) _WriteToINI($aArray, $ar) MsgBox(0, "", "You chose: " & GUICtrlRead(Eval("Radio" & $ar), 1) & @CRLF & "It has been written to the INI @ script folder") EndIf Next GUIDelete($Form2) ExitLoop EndSwitch WEnd EndFunc ;==>_CreateRadios Func _WriteToINI($aArray, $ar) ; Other stuff you may want to write to an INI $verzeichnis = "scripts" & $aArray[$ar] IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis) $verzeichnis = @ScriptDir & "\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "") IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis) EndFunc ;==>_WriteToINI  
  7. abberration's post in Need little help please :) was marked as the answer   
    I saw where you updated the code. Here's an updated version with your newer code:
    #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> $Form1 = GUICreate("Form1", 800, 408, 192, 124) $check1 = GUICtrlCreateCheckbox("Dance / Electro Pop",281,135,150,20,-1,-1) $check2 = GUICtrlCreateCheckbox("Bass House",567,216,103,20,-1,-1) $check3 = GUICtrlCreateCheckbox("Funky House",445,216,97,20,-1,-1) $check4 = GUICtrlCreateCheckbox("Mainstage | Big Room",281,162,150,20,-1,-1) $check5 = GUICtrlCreateCheckbox("Disco Polo Dance",281,188,112,20,-1,-1) $check6 = GUICtrlCreateCheckbox("Trance",281,216,112,20,-1,-1) $check7 = GUICtrlCreateCheckbox("Hardstyle",445,135,112,20,-1,-1) $check8 = GUICtrlCreateCheckbox("Nu Disco",445,162,112,20,-1,-1) GUISetState(@SW_SHOW) For $j = 1 To 4 Check() Sleep(1000) Uncheck() Sleep(1000) Next While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func Check() For $i = 1 To 15 GUICtrlSetState(Eval("check" & $i), 1) Next EndFunc Func Uncheck() For $i = 1 To 15 GUICtrlSetState(Eval("check" & $i), 4) Next EndFunc Using a similar method with just Eval, this code works. I'm not sure why you are adding the checkboxes to an array after they have been created. But give this a try. It cycles between checking and unchecking all the checkboxes four times.
  8. abberration's post in unique random function was marked as the answer   
    Here's a way to create a random sequence of items:
    #include <array.au3> Dim $array[5] = ["excel.exe", "word.exe", "notepad.exe", "calc.exe", "vlc.exe"] $randomString = "" For $i = 1 To UBound($array) Step 1     $rand = Random(0, UBound($array) - 1, 1)     $randomString = $array[$rand] & "|" & $randomString     _ArrayDelete($array, $rand) Next $randomString = StringTrimRight($randomString, 1) ;~ MsgBox(0, "", $randomString) $randomArray = StringSplit($randomString, "|", 2) _ArrayDisplay($randomArray) To put it to use:
    #include <array.au3> Dim $array[5] = ["excel.exe", "word.exe", "notepad.exe", "calc.exe", "vlc.exe"] For $i = 1 To UBound($array) Step 1     $rand = Random(0, UBound($array) - 1, 1)     Run($array[$rand])     _ArrayDelete($array, $rand) Next
  9. abberration's post in trying to launch IE with no address bar and no menubar was marked as the answer   
    This seems to work:
    $objIE = ObjCreate ("InternetExplorer.Application") $objIE.Toolbar = 0 $objIE.Navigate ("www.google.com") $objIE.Visible = 1
  10. abberration's post in Remove text from saved variable? was marked as the answer   
    If the format of that titlebar never changes, you could use StringLeft & StringRight to trim portions of the statement. Example:

    $test = "Space left is: 50gb!" $test = StringRight($test, 5) $test = StringLeft($test, 4) MsgBox(0, "", $test)
  11. abberration's post in Function in Loop is it possible? was marked as the answer   
    Functions should reside outside of loops. I re-arranged your code below. AutoIt will take each line of code in sequential order, so I arranged it to:
    1. open the files
    2. run the loop. inside the loop, it will call those 6 functions, one after another, on the line that writes to the csv file.
    3. it closes the files
    4. it opens the file it wrote to
    I assume that your functions work correctly. I have not tested this code. I hope it helps point you in the right direction.

    #include <Excel.au3> Local $UserName = "Administrator" $Password = "Password" Local $csv = FileOpen(@ScriptDir & "\SystemInfo.csv", 1) Local $strName = FileOpen(@ScriptDir & "\Computers.txt", 0) DriveMapAdd("Z:", "\\" & $strComputerName & "\C$", 8, $strComputerName & "\" & $UserName, $Password) ; pulled this outside the loop because you only need to map it once While 1     Local $strComputerName = FileReadLine($strName)     If @error = -1 Then ExitLoop ;~     MsgBox(0, "Computer Name", "This is the computer name: " & $strComputerName)          ; Writes information to the Excel Spreadsheet in Script Director.     FileWrite($csv, "ComputerName" & "," & "Vendor" & "," & "Model" & "," & "OS" & "," & "Vergence Version" & "," & @CRLF & _Get_Name() & "," & _Get_SystemVendor() & "," & _Get_SystemVersion() & "," & _Get_OS_Name() & "," & _Get_Vergence_Ver() & @CRLF) WEnd  FileClose($csv) ; pulled this outside the loop because you only need to close once the loop is finished  FileClose($strName) ; same for this one   ; Opens file once the script has finished running. $oExcelExist = FileExists($csv) If $oExcelExist = 1 Then     _ExcelBookOpen($csv) EndIf If @error = 1 Then     MsgBox(0, "Error!", "Unable to Open the Excel Object")     Exit ElseIf @error = 2 Then     MsgBox(0, "Error!", "Cannot Open " $oExcelExist)     Exit EndIf DriveMapDel("Z:") ;;;;;;;;;;;;;;;  Begin Functions  ;;;;;;;;;;;;;;;; Func _Get_Vergence_Ver()     $Vergence = FileGetVersion("Z:\Program Files\Sentillion\Vergence Authenticator\Authenticator.exe")     Return $Vergence EndFunc   ;==>_Get_Vergence_Ver Func _Get_Name()     Local $s_Text = ''     $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")     $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")     $colItems = $objWMIService.ExecQuery("SELECT Name FROM Win32_ComputerSystem", "WQL", 0x30)     If (IsObj($objWMIService)) Then         For $objItem In $colItems             Local $s_Text = $objItem.name         Next         Return String($s_Text)     Else         Return 0     EndIf EndFunc   ;==>_Get_Name Func _Get_SystemVendor()     Local $s_Text = ''     $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")     $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)     If (IsObj($objWMIService)) Then         For $objItem In $colItems             Local $s_Text = $objItem.Vendor         Next         Return String($s_Text)     Else         Return 0     EndIf EndFunc   ;==>_Get_SystemVendor Func _Get_SystemVersion()     Local $s_Text = ''     $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")     $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)     If (IsObj($objWMIService)) Then         For $objItem In $colItems             Local $s_Text = $objItem.Name         Next         Return String($s_Text)     Else         Return 0     EndIf EndFunc   ;==>_Get_SystemVersion Func _Get_SystemVModel()     Local $s_Text = ''     $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")     $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", 0x30)     If (IsObj($objWMIService)) Then         For $objItem In $colItems             Local $s_Text = $objItem.Version         Next         Return String($s_Text)     Else         Return 0     EndIf EndFunc   ;==>_Get_SystemVModel Func _Get_OS_Name()     Local $s_Text = ''     $objWMILocator = ObjCreate("WbemScripting.SWbemLocator")     $objWMIService = $objWMILocator.ConnectServer($strComputerName, "\root\cimv2", $strComputerName & "\" & $UserName, $Password, "", "", "&H80")     $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", 0x30)     If (IsObj($objWMIService)) Then         For $objItem In $colItems             Local $s_Text = $objItem.Caption         Next         Return String($s_Text)     Else         Return 0     EndIf EndFunc   ;==>_Get_OS_Name
  12. abberration's post in FileWriteLog unable to append log at beginning of file was marked as the answer   
    Welcome to AutoIt!
    The statement "$iFlag <> -1" shouldn't be used in that manner. The help file is only using $iFlag to let you know how it will process a -1 in that location or anything other than a -1. Basically, it is a way of telling your script to write your newest  log entry to the top of the file or the bottom. If you put in a -1 (which is default), it will write it to the bottom. If you put anything else, it will write it to the top. So, the question you have to ask yourself is if you always want your newest entry on top or in chronological order.
    Examples that should work:
    _FileWriteLog($hFile, 'I:\ drive not found', -1); writes to the end of the file _FileWriteLog($hFile, 'I:\ drive not found', 1); writes to the beginning of the file
  13. abberration's post in create UDT was marked as the answer   
    I think what you are looking for is DllStructCreate. That is one of the things on my "to do" list of things to learn more about.
  14. abberration's post in Need help with simple debugging code was marked as the answer   
    $test = 13 ;~ Func Hotkey_F1()     $Debug = InputBox("Debugger","Input var name","test","",200, 135 ) ; must leave off the $ for the Eval statement to work     $value = Eval($Debug)     Msgbox(0,"Debugger", $value) ;~ EndFunc
  15. abberration's post in Get Select item text in listbox was marked as the answer   
    I have played with your code until I have it working how you want. At least this is what I think you want. Look at the comments and they should help you.

    #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <GUIListBox.au3> #include <WindowsConstants.au3> #include <array.au3> $Form1 = GUICreate("Form1", 254, 233, 516, 270) $List1 = GUICtrlCreateList("", 16, 8, 217, 149, BitOR($LBS_STANDARD, $LBS_EXTENDEDSEL)) ; BitOR style taken from help file $sItems = "1|2|3|4|5" ; To simplify, no txt list. Just this one for testing purposes. GUICtrlSetData($List1, $sItems) _GUICtrlListBox_SetSel($List1, 1) ; Set position 1 (which is the 2nd in list) as default $Button1 = GUICtrlCreateButton("Button1", 80, 176, 75, 25) GUISetState(@SW_SHOW) Local $aaa While 1     $aaa = GUIGetMsg()     Select         Case $aaa = $GUI_EVENT_CLOSE             ExitLoop         Case $aaa = $Button1             $selItems = _GUICtrlListBox_GetSelItemsText($List1) ; Read the selected item characteristics to an array             MsgBox(4160, "Information", "Item Selected: " & $selItems[1]) ; $selItems[1] cooresponds to the the selected text value in this array     EndSelect WEnd
  16. abberration's post in If value goes down was marked as the answer   
    I'm giving an example using random numbers between 1-100. Hopefully, it will set you on the right track. If you have questions, feel free to ask.

    #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 298, 75) $Label1 = GUICtrlCreateLabel("Random Number:", 8, 8, 87, 17) $Label2 = GUICtrlCreateLabel("0", 96, 8, 36, 17) ; The random number - start with zero $Label3 = GUICtrlCreateLabel("Status:", 8, 40, 37, 17) $Label4 = GUICtrlCreateLabel("No Change", 48, 40, 96, 17) ; The status $Button1 = GUICtrlCreateButton("Hit Me", 192, 24, 75, 25) GUISetState(@SW_SHOW) While 1     $nMsg = GUIGetMsg()     Switch $nMsg         Case $GUI_EVENT_CLOSE             Exit         Case $Button1             $read = GUICtrlRead($Label2) ; read current number             $newNum = Random(1, 100, 1) ; generate a new random number             GUICtrlSetData($Label2, $newNum) ; set the new random number on the program             If $newNum < $read Then                 GUICtrlSetData($Label4, "Lowered")             ElseIf $newNum > $read Then                 GUICtrlSetData($Label4, "Increased")             Else                 GUICtrlSetData($Label4, "No Change") ; if not lower or higher than previous, the only possibility left is the same number was generated             EndIf     EndSwitch WEnd
×
×
  • Create New...