Button at the end of array in AutoIT - windows

I am working on a program that pulls data from AD via a linked table in SQL and lets the user copy an email address to the clipboard. I am using an array to dynamically display a button beside each row. The problem is that, when I try and put labels or buttons inside the for loop, they don't show up. Is it just that I'm doing it wrong. My code is as follows:
#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
global $title = "E-Mail address lookup"
global $sqlCon = _MSSQL_Con("server", "user", "Directory3=", "password")
global $name = InputBox($title,"Please type the name of the person you wish to find")
global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
if StringLen(StringStripWS($name,3)) < 1 then
MsgBox(0, $title, "Name cannot be empty")
Else
Global $ControlID = GUICreate($title, 530, 500)
GUISetState(#SW_SHOW)
Local $iOldOpt = Opt("GUICoordMode", 2)
GUICtrlCreateLabel(" ", 0, 0, 80)
GUICtrlCreateLabel("E-Mail Address", 20, -1, 100)
GUICtrlCreateLabel("Name", 20, -1, 50)
GUICtrlCreateLabel("Department", 20, -1, 100)
GUICtrlCreateLabel("Telephone Number", 20, -1, 200)
for $i = 1 To UBound($result) Step 1
GUICtrlCreateButton("Copy", 0, $i, 30, 20)
Next
GUISetState()
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
EndIf
I would have expected one button to show up on a new line on every iteration of the loop

I recommend to use another mode:
#include <GUIConstantsEx.au3>
;~ #include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
Global $title = "E-Mail address lookup"
;~ global $sqlCon = _MSSQL_Con("server", "user", "Directory3=", "password")
;~ global $name = InputBox($title,"Please type the name of the person you wish to find")
;~ global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
Global $result = StringSplit('1,2,3,4,5,6,7,8', ',')
;~ if StringLen(StringStripWS($name,3)) < 1 then
;~ MsgBox(0, $title, "Name cannot be empty")
;~ Else
;~ _ArrayDisplay($result)
Global $ControlID = GUICreate($title, 530, 500)
;~ Local $iOldOpt = Opt("GUICoordMode", 2)
GUICtrlCreateLabel(" ", 0, 0, 80)
GUICtrlCreateLabel("E-Mail Address", 20, -1, 100)
GUICtrlCreateLabel("Name", 20, -1, 50)
GUICtrlCreateLabel("Department", 20, -1, 100)
GUICtrlCreateLabel("Telephone Number", 20, -1, 200)
For $i = 1 To UBound($result) - 1
GUICtrlCreateButton("Copy", 20, $i * 20, 350, 20)
Next
GUISetState()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd

Related

How to read value from slider control?

I used simplespy.au3 to target a slider control in the Windows settings window using _UIA_action(). Asking the user to input a value I send left and right keys for the slider to move.
But I don't know how to get the current value of my slider. I tried GUICtrlRead() but it doesn't work. How do I get the current value of a slider control?
My code:
#RequireAdmin
#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <GuiSlider.au3>
#include <GUIConstants.au3>
#include "UISpy\UIAWrappers\UIAWrappers.au3"
Slider()
Func Slider()
;User Input
$Default = ""
$input = ""
While 1
$input = InputBox("Brightness", "Set Brightness to:", "", " M", -1, -1)
If #error Then ExitLoop
If $input < 0 Or $input > 100 Then
MsgBox(48, "Error!", "Minimum value for brightness is 0 and the Maximum brightness is 100")
$Default = $input
ContinueLoop
ElseIf StringLen($input) > 0 Or $input < 100 Then
ExitLoop
EndIf
If #error = 1 Then
Exit
EndIf
WEnd
;Start automation
Local $iTimeout = 1
Local $hWnd = WinWait("[Class:Shell_TrayWnd]", "", "[CLASS:Button; INSTANCE:1]")
ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]") ;open the start menu
Send("Display Settings", 10) ;Type the Display Settings in the start menu
Sleep(1000)
Send("{ENTER}")
AutoItSetOption("MustDeclareVars", 1)
Local $oP2 = _UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Settings;controltype:=UIA_WindowControlTypeId;class:=ApplicationFrameWindow", $treescope_children)
_UIA_Action($oP2, "setfocus")
Local $oP1 = _UIA_getObjectByFindAll($oP2, "Title:=Settings;controltype:=UIA_WindowControlTypeId;class:=Windows.UI.Core.CoreWindow", $treescope_children)
_UIA_Action($oP1, "setfocus")
Local $oP0 = _UIA_getObjectByFindAll($oP1, "Title:=;controltype:=UIA_PaneControlTypeId;class:=ScrollViewer", $treescope_children)
;First find the object in the parent before you can do something
; $oUIElement=_UIA_getObjectByFindAll("Changebrightness.mainwindow", "title:=Change brightness;ControlType:=UIA_SliderControlTypeId", $treescope_subtree)
Local $oUIElement = _UIA_getObjectByFindAll($oP0, "title:=Change brightness;ControlType:=UIA_SliderControlTypeId", $treescope_subtree)
_UIA_action($oUIElement, "setfocus")
Send("{LEFT $input}"`enter code here`)
Sleep(1000)
Local $value = GUICtrlRead($oUIElement)
MsgBox($MB_SYSTEMMODAL, "", "Brightness is at: " & $value, $iTimeout)
Local $setValue = GUICtrlSetData($oUIElement, 50)
;UI for verification
MsgBox(0, "Change Brightness", "Brightness Changed.", $iTimeout)
Sleep(1000)
WinClose("Settings") ;Close the active window
EndFunc ;==>Slider
Don't manipulate the slider. Use the WinAPI instead: _WinAPI_SetDeviceGammaRamp.

Autoit - limiting the number of execution

I don't know if it's possible cuz i couldn't find anywhere.
I have created a GUI with some buttons to start things.
I'm just wondering if it is possible to:
limit the number of openings of the GUI
limit the number of executions with the buttons
limit the time range so that after certain point, u can't use it
Global $explain = "help~~"
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $explain = "hmmm"
Global $Form1 = GUICreate("Yay", 328, 157)
Global $Label1 = GUICtrlCreateLabel("ID", 12, 14, 67, 20)
Global $Label2 = GUICtrlCreateLabel("Password", 12, 44, 67, 20)
Global $Label3 = GUICtrlCreateLabel("hello world", 225, 14, 90, 20)
Global $Input1 = GUICtrlCreateInput("", 76, 10, 105, 24)
Global $Input2 = GUICtrlCreateInput("", 76, 40, 105, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
Global $Input3 = GUICtrlCreateInput("", 228, 40, 70, 24)
Global $Button1 = GUICtrlCreateButton("Log In", 76, 69, 105, 30)
Global $Button2 = GUICtrlCreateButton("Connect", 212, 69, 100, 30)
Global $Checkbox1 = GUICtrlCreateCheckbox("hmm", 240, 113, 97, 17)
Global $Checkbox2 = GUICtrlCreateCheckbox("hmm2", 240, 134, 97, 17)
Global $Group1 = GUICtrlCreateGroup("", 5, -5, 190, 110)
Global $Edit1 = GUICtrlCreateEdit("", 5, 110, 228, 100)
GUICtrlSetData(-1, $explain)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
If (GUICtrlRead($Checkbox1) = $GUI_CHECKED) Then
Global $hmm = 1
EndIf
Case $Checkbox2
If (GUICtrlRead($Checkbox2) = $GUI_CHECKED) Then
Global $hmm2 = 0
EndIf
Case $Button1
Global $id = GUICtrlRead($Input1)
Global $pass = GUICtrlRead($Input2)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","possible to limit #of Execution?")
Case $Button2
Global $exnum = GUICtrlRead($Input3)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","time limit would be nice too! thnx!")
EndSwitch
WEnd
Has anyone tried this?
Will it require intense coding?
Could you provide a sample if it isn't too bad
Good afternoon Pita,
Yup! It's all definitely possible to do! There's multiple ways to do so, I'll try to name a few.
A good way for you to manage these requests, would be to make a properties file to manage everything. Take a look at my example below!
Global $explain = "help~~"
#include <IE.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
; =================================================================================================================================================
#include <File.au3>
#Include <Date.au3>
; This is the directory that the temporary file will be stored.
$fileHandle = #AppDataDir & "\TestProgram\properties.txt"
; Checks to see if the properties file exists
if FileExists($fileHandle) Then
; START Check Run Limit
; Reads the first value in the properties file. In this example, it's the limit to how many times the program can be launched.
$runLimit = FileReadLine($fileHandle, 1)
; MsgBox(0,"Run Limit", $runLimit)
; If the run limit reaches zero (or less than by some glitch), do not launch the program.
if $runLimit <= 0 Then
MsgBox(16,"Run Limit Reached", "You have reached the maximum number of launches for this program!")
Exit
EndIf
; Subtract one from the launch limit.
$newLimit = $runLimit - 1
MsgBox(0,"New Run Limit", "You may launch this program " & $newLimit & " more times!", 3)
; Update the properties file.
_FileWriteToLine($fileHandle, 1, $newLimit, True)
; END Check Run Limit
; Start CHECK Expiration Date
$expDate = FileReadLine($fileHandle, 2)
; MsgBox(0,"Expiration Date", "This program expires on " & $expDate)
; Check to see if the expiration date has already passed
if $expDate < _NowDate() Then
MsgBox(16,"Program Expired","Your trial period has expired!")
Exit
EndIf
; END Check expiration date
Else
; If the file does not exists, create it and set it up
$propFile = FileOpen($fileHandle, 10)
; Sets the launch limit to 10. Change this value to set the launch limit
FileWrite($fileHandle, "10" & #CRLF)
; Sets the expiration date to a week (7 days) from the initial launch date.
FileWrite($fileHandle, #MON & "/" & (#MDAY + 7) & "/" & #YEAR & #CRLF)
; Sets the button limit for the login button to 3.
FileWrite($fileHandle, "3" & #CRLF)
FileClose($fileHandle)
#CS
NOTE: THIS IS JUST FOR DEMONSTRATION PURPOSES! THE USER CAN SIMPLY DELETE THE PROPERTIES FILE
IN ORDER TO RESET THE VALUES AND CONTINUE USING THE PROGRAM. THIS WAS DESIGNED SIMPLY TO
GET YOU ON THE RIGHT TRACK.
#CE
EndIf
; =================================================================================================================================================
Global $explain = "hmmm"
Global $Form1 = GUICreate("Yay", 328, 157)
Global $Label1 = GUICtrlCreateLabel("ID", 12, 14, 67, 20)
Global $Label2 = GUICtrlCreateLabel("Password", 12, 44, 67, 20)
Global $Label3 = GUICtrlCreateLabel("hello world", 225, 14, 90, 20)
Global $Input1 = GUICtrlCreateInput("", 76, 10, 105, 24)
Global $Input2 = GUICtrlCreateInput("", 76, 40, 105, 24, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
Global $Input3 = GUICtrlCreateInput("", 228, 40, 70, 24)
Global $Button1 = GUICtrlCreateButton("Log In", 76, 69, 105, 30)
Global $Button2 = GUICtrlCreateButton("Connect", 212, 69, 100, 30)
Global $Checkbox1 = GUICtrlCreateCheckbox("hmm", 240, 113, 97, 17)
Global $Checkbox2 = GUICtrlCreateCheckbox("hmm2", 240, 134, 97, 17)
Global $Group1 = GUICtrlCreateGroup("", 5, -5, 190, 110)
Global $Edit1 = GUICtrlCreateEdit("", 5, 110, 228, 100)
GUICtrlSetData(-1, $explain)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Checkbox1
If (GUICtrlRead($Checkbox1) = $GUI_CHECKED) Then
Global $hmm = 1
EndIf
Case $Checkbox2
If (GUICtrlRead($Checkbox2) = $GUI_CHECKED) Then
Global $hmm2 = 0
EndIf
Case $Button1
; START Button Limit ================================================================================================================
if FileExists($fileHandle) Then
$buttonLimit = FileReadLine($fileHandle, 3)
if $buttonLimit <= 0 Then
MsgBox(16,"Button Limit Reached", "You have reached the maximum number of times you can hit this button!")
ELSE
$newButtonLimit = $buttonLimit - 1
MsgBox(0,"New Run Limit", "You may press this button " & $newButtonLimit & " more times!", 3)
_FileWriteToLine($fileHandle, 3, $newButtonLimit, True)
; START OLD CODE
Global $id = GUICtrlRead($Input1)
Global $pass = GUICtrlRead($Input2)
; WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","possible to limit #of Execution?")
; END OLD CODE ================================================================================================================
EndIf
EndIf
; END Button Limit
Case $Button2
Global $exnum = GUICtrlRead($Input3)
WinSetState("Yay", "", #SW_MINIMIZE)
MsgBox(0,"","time limit would be nice too! thnx!")
EndSwitch
WEnd
Upon launching the program, a property file is generated and stored in the appdata on the users computer.
On windows: Press Win+R and type %appdata%. If you left the example the same, there will be a folder called "TestProgram", if you changed the name, it will be whatever you called it. Inside of this folder, there will be the properties.txt file (Again, unless you changed the name).
I have made some comments in the example code to help explain what I did, but I'm not fantastic at explaining things so please let me know if you need some more help.
P.S. If you do use this method, I strongly suggest encrypting (Using Crypt maybe?) the properties file to make it a little less editable and designing some way to tell if the user deleted the file.
I hope this helps!,
Tim

Replicate text from AutoIt form to Notepad

I am using this AutoIt code to send text to Notepad on the press of a button:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(#SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
Example(GUICtrlRead($Input))
EndSwitch
WEnd
Func Example($text)
Run("notepad.exe")
Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)
ControlSend($hWnd, "", "Edit1", $text)
EndFunc
It is working great. But now I want to send keystrokes as soon as I press them. Is there something like OnKeyDown in AutoIt? So I don't have to press the send button to send it to Notepad every time I type a character.
This is how I would do it.
Work pretty neat!
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Run("notepad.exe")
Global $hWnd = WinWait("[CLASS:Notepad]", "", 10)
$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(#SW_SHOW)
$OldText = ""
While 1
$nMsg = GUIGetMsg()
$NewText = GUICtrlRead($Input)
If $OldText <> $NewText Then
$OldText = $NewText
Example($NewText)
EndIf
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
Example(GUICtrlRead($Input))
EndSwitch
WEnd
Func Example($text)
ControlSetText($hWnd, "", "Edit1", $text)
EndFunc

rdp session terminal server sendKeys

I don't know how can I send "windows + r" in order to open the small window (execute ...) to the terminal serveur.. Anybody can help me ? I tried Send("#r") but it doesn't work
$host = "" ; <---- IP
$hGUI = GUICreate("Terminal Serveur", 952, 675, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$oRDP = ObjCreate("MsTscAx.MsTscAx.2")
$oRDP_Ctrl = GUICtrlCreateObj($oRDP, 64, 44, 800, 600)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetStyle($oRDP_Ctrl , $WS_VISIBLE)
$oRDP.DesktopWidth = 800
$oRDP.DesktopHeight = 600
$oRDP.Fullscreen = False
$oRDP.ColorDepth = 16
$oRDP.AdvancedSettings3.SmartSizing = True
$oRDP.Server = $host
$oRDP.UserName = "" ; <--- Username
$oRDP.Domain = ""
$oRDP.AdvancedSettings2.ClearTextPassword = "" ; <--- Password
$oRDP.ConnectingText = "Connecting to " & $host
$oRDP.DisconnectedText = "Disconnected from " & $host
$oRDP.StartConnected = True
$oRDP.Connect()
$oShel = ObjCreate("shell.application")
$oShel_Ctrl = GUICtrlCreateObj($oShel, 64, 44, 800, 600)
GUICtrlSetStyle($oShel_Ctrl , $WS_VISIBLE)
GUISetState(#SW_SHOW, $hGUI)
Send ("#r") !!!!
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
$oRDP.Disconnect()
Exit
EndSwitch
Did u try to do it with ShellExecute instead of opening the window first?
//Edit I just saw tht u asked the question before and tht u want to run the command at the other computer

Create new folder and highlight it for renaming

I was trying to create an AutoIt script to create a new folder and then highlight it for renaming like if we right click and create a new folder.
Here is my script. What I wanted to achieve is create the new folder, highlight it, then press F2.
#include <WinAPI.au3>
HotKeySet("#y", "NewFolder")
While 1
Sleep(200)
WEnd
Func NewFolder()
Local $hWnd = WinGetHandle("[ACTIVE]")
Local $class = _WinAPI_GetClassName($hWnd)
If StringCompare($class, "CabinetWClass") = 0 Then
Local $sSelected_Path = _GetWindowsExplorerPath($hWnd) & "\NewFolder" & $count
Local $iFileExists = FileExists($sSelected_Path)
If $iFileExists Then
$count += 1
Else
Local $success = DirCreate($sSelected_Path)
Sleep(500)
If $success = 1 Then
Local $Finditem = ControlListView($hWnd, "", "[CLASS:SysListView32; INSTANCE:1]", "Finditem", "NewFolder")
MsgBox(0, "", $Finditem)
Local $Select = ControlListView($hWnd, "", "[CLASS:SysListView32; INSTANCE:1]", "Select", $Finditem)
$count += 1
EndIf
EndIf
EndIf
EndFunc
Func _GetWindowsExplorerPath($hWnd)
Local $pv, $pidl, $return = "", $ret, $hMem, $pid, $folderPath = DllStructCreate("char[260]"), $className
Local $bPIDL = False
Local Const $CWM_GETPATH = 0x400 + 12;
; Check the classname of the window first
$className = DllCall("user32.dll", "int", "GetClassName", "hwnd", $hWnd, "str", "", "int", 4096)
If #error Then Return SetError(2, 0, "")
If ($className[2] <> "ExploreWClass" And $className[2] <> "CabinetWClass") Then Return SetError(1, 0, "")
; Retrieve the process ID for our process
$pid = DllCall("kernel32.dll", "int", "GetCurrentProcessId")
If #error Then Return SetError(2, 0, "")
; Send the CWM_GETPATH message to the window
$hMem = DllCall("user32.dll", "lparam", "SendMessage", "hwnd", $hWnd, "int", $CWM_GETPATH, "wparam", $pid[0], "lparam", 0)
If #error Then Return SetError(2, 0, "")
If $hMem[0] = 0 Then Return SetError(1, 0, "")
; Lock the shared memory
$pv = DllCall("shell32.dll", "ptr", "SHLockShared", "uint", $hMem[0], "uint", $pid[0])
If #error Then Return SetError(2, 0, "")
If $pv[0] Then
$pidl = DllCall("shell32.dll", "ptr", "ILClone", "uint", $pv[0]) ; Clone the PIDL
If #error Then Return SetError(2, 0, "")
$bPIDL = True
DllCall("shell32.dll", "int", "SHUnlockShared", "uint", $pv) ; Unlock the shared memory
EndIf
DllCall("shell32.dll", "int", "SHFreeShared", "uint", $hMem, "uint", $pid) ; Free the shared memory
If $bPIDL Then
; Retrieve the path from the PIDL
$ret = DllCall("shell32.dll", "int", "SHGetPathFromIDList", "ptr", $pidl[0], "ptr", DllStructGetPtr($folderPath))
If (#error = 0) And ($ret[0] <> 0) Then $return = DllStructGetData($folderPath, 1) ; Retrieve the value
DllCall("shell32.dll", "none", "ILFree", "ptr", $pidl[0]) ; Free up the PIDL that we cloned
Return SetError(0, 0, $return) ; Success
EndIf
Return SetError(2, 0, "") ; Failed a WinAPI call
EndFunc
This works for me in Win7 - should work in xp as long as address bar is turned on. MUCH simpler than using the DLL calls. I left you a little bit of work to do :)
HotKeySet("#y","_NewFolder")
HotKeySet("#n","_EXIT")
While 1
Sleep(250)
WEnd
FUNC _NewFolder() ; make all vars local
$TEXT = WinGetText("[active]")
; handle error here
$SPLIT = StringSplit($TEXT,#CR & #LF & #CRLF) ;split on new lines (idk which one of the three it uses)
IF IsArray($SPLIT) Then
; trigger = true
FOR $I = 1 to $SPLIT[0]
IF StringInStr($SPLIT[$i],"Address: ") Then
; trigger = false
$STRING = StringReplace($SPLIT[$i],"Address: ","")
$INPUT = InputBox("New Folder","Name your new folder")
; add some enforcement to input box, i like do until loops
$PATH = $STRING & "\" & $INPUT
$CREATE = DirCreate($PATH)
; handle error here
Return SetError(0,0,$PATH)
EndIf
Next
; if trigger then error
Else
Return SetError(1,0,0) ;if split is not an array - could add some more here in case address is the only line returned
EndIf
EndFunc
Func _Exit()
Exit
EndFunc
"create a new folder and then highlight it for renaming like its supposed to"
As per Documentation - Function Reference - DirCreate() :
Creates a directory/folder.
Example:
Global Const $g_sPathFolder = #DesktopDir & '\'
Global Const $g_sPromptText = 'Enter folder name :'
Global Const $g_sPromptExmp = 'NewFolder'
Global $g_sNameFolder = InputBox(#ScriptName, $g_sPromptText, $g_sPromptExmp)
DirCreate($g_sPathFolder & $g_sNameFolder)
ShellExecute($g_sPathFolder & $g_sNameFolder)

Resources