I'm writing an AutoIt script to make GUI buttons by looping through an array of button definitions.
It's a script that I'll be adding/removing buttons to/from quite often, so I thought a loop makes sense. I add the button handle, button text, and function name to bind the button to an array called $buttons. The button parameters are saved to a row of the $buttons array as a pipe delimited string.
Func make_buttons()
local $i = 1
Local $bHandles[Ubound($buttons)]
_arraydisplay($bHandles)
For $button In $buttons
local $params= StringSplit($button,"|")
local $top = $i*40
local $left = 10
local $width = 100
Global $bHandles[$i] = GUICtrlCreateButton($params[1],$left,$top,$width)
GUICtrlSetOnEvent($bHandles[$i],$params[2])
$i = $i+1
Next
EndFunc
I'm getting this error on execution:
Global $params[1] = ^ERROR
Error: Missing subscript dimensions in "Dim" statement
Any help clarifying what the error means is appreciated.
Update
#Sachadee's answer below clued me along to the fact athat I had been using the Global keyword to declare the handle variable to GuiCtrlCreateButton() while trying to use a variable as the name. Leaving off the Global keyword helped me eliminate the error I was receiving. My final button creation lines of code worked as this:
Func make_buttons()
local $i = 1
For $button In $buttons
local $params= StringSplit($button,"|")
local $top = $i*40
local $left = 10
local $width = 100
Global $handle = $params[2] & "_handle"
$handle = GUICtrlCreateButton($params[1],$left,$top,$width)
GUICtrlSetOnEvent($handle,$params[2])
$i = $i+1
Next
EndFunc
Edit :
You're defining an array structure Local $bHandles[Ubound($buttons)] with the place for [N] elements. But you're not defining the content of this array. And then you're trying to redefine it with another value with the Global:
Global $bHandles[$i] = GUICtrlCreateButton($params[1],$left,$top,$width).
Here's a better way to do it :
#include <GUIConstants.au3>
#include <ButtonConstants.au3>
Global $AllButtons[4] = ["3","Button1","Button2","Bouton3"]
GuiCreate ("Title", 120, 200)
$Btn_Start = GUICtrlCreateDummy()
For $i = 1 To $AllButtons[0]
local $top = $i*40
local $left = 10
local $width = 100
GUICtrlCreateButton($AllButtons[$i],$left,$top,$width)
Next
$Btn_End = GUICtrlCreateDummy()
GUISetState ()
While 1
$Msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
Exit
Case $Btn_Start To $Btn_End
MsgBox(0, "Test", GUICtrlRead($Msg))
EndSwitch
Wend
Related
Why can I not run these features one after another? Functions _IEFormElementSetValue() and _IEAction() do not work together?
Code:
#include <IE.au3>
Local $oIE = _IECreate("http://www.google.com")
WinSetState("[ACTIVE]", "", #SW_MAXIMIZE)
; Feature 1: inserts the text into the search box google
Local $oDigita = _IEGetObjByName($oIE, "q")
_IEFormElementSetValue($oDigita, "Nome pesquisado")
; Feature 2: option button "I'm feeling lucky"
Local $oClica = _IEGetObjByName($oIE, "btnI")
_IEAction($oClica, "click")
Your code works on AutoIt version 3.3.14.2, IE version 11.0.9600.19266. Try my adjusted variant below, because the behavior of _IECreate() isn't always stable.
Code:
#include-once
#include <IE.au3>
Global $oIE = _IECreate( 'http://www.google.com', 0, 1, 1, 1 )
WinSetState( '[ACTIVE]', '', #SW_MAXIMIZE )
Sleep( 1000 )
; Feature 1: inserts the text into the search box google
Global $oDigita = _IEGetObjByName( $oIE, 'q' )
_IEAction( $oDigita, 'click' )
ClipPut( 'Nome pesquisado' ) ; save string in clipboard
Send( '^v' ) ; paste string with CTRL+V
; Feature 2: option button "I'm feeling lucky"
Global $oClica = _IEGetObjByName( $oIE, 'btnI' )
_IEAction( $oClica, 'click' )
Notice:
The function _IEAction() is quite unstable just like Send(). I suggest to use an IE object, embedded into a GUI with _IECreateEmbedded() instead.
I wrote a script using AutoIt.
The problem is to run it in background so that I can use my desktop for other work.
I used _ImageSearch() which usually works on an active window.
Is there any way to call such a function in background? Here is my code:
WinActivate('Window Title')
$x = 0
$y = 0
If _ImageSearch('searchButton.PNG', 1, $x, $y, 20) = 1 Then
Return 1
ElseIf _ImageSearch('bundle.PNG', 1, $x, $y, 20) = 1 Then
Send('{ESC}{LCTRL}{LCTRL}{LCTRL}')
sleep(1000)
Else
Send('{LCTRL}')
EndIf
Write a script that runs continuously and call the image search with a hotkey:
HotKeySet('+!e', '_endScript') ; terminate the script with SHIFT+ALT+E
HotKeySet('+!i', '_searchImage') ; calls the image search function with SHIFT+ALT+I
; main loop
While True
Sleep(50)
WEnd
Func _endScript()
Exit
EndFunc
Func _searchImage()
; your image search stuff
EndFunc
But it may be that the other application blocks your hotkeys. Try it.
I am having trouble with the code I am writing. I have a table which lists staff member details. I am trying to have an email address copy to the clipboard when you select a record and right click. I have the following code:
#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
global $title = "E-Mail address lookup"
global $name = InputBox($title,"Please type the name of the person you wish to find")
global $sqlCon = _MSSQL_Con("server", "username", "password", "directory-plus")
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 $rset = UBound($result) - 1
Global $ControlID = GUICreate($title, 500, 150)
Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 480, 150)
for $count = 1 to $rset step 1
GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][1] & "|" & $result[$count][2], $idListview)
if MouseClick($MOUSE_CLICK_RIGHT)==1 Then
ClipPut($result[$count][2])
EndIf
Next
GUISetState(#SW_SHOW)
GUISetState()
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
EndIf
I would have thought my IF statement within the for loop would do the trick but, when I run my code, it just copies whatever is in email address column in the last row when in fact I want it to copy the email address when you right click on a particular row but I'm not sure how to do it.
Your code has two major problems. The first is that MouseClick() doesn't check for the mouse button being pressed, but instead sends a mouse button click. Since sending a mouse button click is going to succeed, MouseClick($MOUSE_CLICK_RIGHT)==1 will evaluate to true. For each list view item you create, you are putting the email address on the clipboard.
The second problem is that the if statement is in the wrong place. It's executed just after you create each list view item.
Instead, change your While statement as follows
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
case $GUI_EVENT_SECONDARYDOWN
$selecteditem=StringSplit(GUICtrlRead(GUICtrlRead($idListview)),"|")
if #error=0 and $selecteditem[0]>1 Then
ClipPut($selecteditem[2])
EndIf
EndSwitch
WEnd
I want to get pixel color from a game and react.
So I have this sript:
#include <Color.au3>
Local $pause = False;
$WinName = "Game" ; Let's say there is a game with this name =)
$hwnd = WinGetHandle($WinName) ; Checked handle with powershell and Au3Info, handle is correct
local $res
Opt("PixelCoordMode", 2);
HotKeySet("{F10}", "exitNow");
HotKeySet("{F11}", "Pause");
While 1
;WinWaitActive($hwnd) ; The script stops here if it is uncommented no matter if window is active or not
if ( $pause ) Then
$res = GetPos();
ConsoleWrite ( StringFormat ("Result color: %s %s", $res[0], $res[1] ) )
Sleep (1000)
EndIf
WEnd
Func exitNow()
Exit
EndFunc
Func Pause()
$pause = Not $pause
EndFunc
Func GetPos()
local $var = Hex(PixelGetColor(5, 5, $hwnd)) ; Only works in windowed mode, FullScreen return some random colors like 00FFFFFF or 00AAAAAA
$var = StringTrimLeft($var,2) ; Removing first 2 numbers they r always 00
local $var1 = Hex(PixelGetColor(15, 5, $hwnd)) ; Only works in windowed mode, FullScreen return some random colors like 00FFFFFF or 00AAAAAA
$var1 = StringTrimLeft($var1,2) ; Removing first 2 numbers they r always 00
local $result[2] = [ $var, $var1 ]
return $result
EndFunc
Main script should before window is active and only then should try to GetPixelColor but that never happens, no matter what I do, i've tried WindowActivate still no result.
a) - What am I doing wrong ? Or may be there is another way to check if window is currently active ?
So at the moment I start script disabled, and when I activate window manually I press F11 to enable.
b) - PixelGetColor only works if game is running in Windowed mode, if it's a fullscreen mode result is unpredictable. Is there a way to make PixelGetColor work in fullscreen.
I've tried to run game x32, x64, DX9 and DX11 in different combinations Fullscreen result is just wrong.
ADDED:
Now While looks like this and that works :)
Thanks to Xenobeologist!!!
While 1
$hwnd = WinGetHandle('[Active]');
If ( $pause ) Then
If WinGetTitle($hwnd) <> $WinName Then
Pause()
ContinueLoop
EndIf
$res = GetPos();
ConsoleWrite ( StringFormat ("Result color: %s %s", $res[0], $res[1] ) )
Sleep (1000)
Else
If WinGetTitle($hwnd) = $WinName Then
Pause()
ContinueLoop
EndIf
EndIf
WEnd
a) is now solved
b) is still not solved. One more thing, topic of this question doesn't say anything bout this question, should add info bout this into the topic or it's better to start a new thread? a) was my main question, it would be prefect to solve b) as well but I can live without it. As far as I understood it's much more complicated. What do you think?
ADDED2:
As far as I understand problem b) can be solved by using more complex code. Here http://www.autohotkey.com/board/topic/63664-solved-imagesearch-failure-wdirectx-fullscreen-gamewin7/ was discussed pretty same problem. It's AHK and ImageSearch function, but im pretty sure that the reason I get wrong colours in fullscreen is the same. I don't want to complicate code that much and PrintScreen is too slow so I'll use windowed mode and wont be bothering with b).
Does this help?
#include <MsgBoxConstants.au3>
$handle = WinGetHandle('[Active]')
ConsoleWrite('!Title : ' & WinGetTitle($handle) & #CRLF)
ConsoleWrite('!Process : ' & WinGetProcess($handle) & #CRLF)
ConsoleWrite('!Text : ' & WinGetText($handle) & #CRLF)
Sleep(Random(10, 3000, 1))
If WinActive($handle) Then MsgBox($MB_SYSTEMMODAL, "", "WinActive" & #CRLF & "Scite ")
My path of file is C:\Documents and Settings\12313\My Documents\Downloads\Bubble_Hit_TSA31DIBX.exe
I wanna run it with sandboxie but anytime redownload,the name of file is changing.
For example:Buble_Hit_**.exe " * " is the changing.
You can use the wildcard feature of FileFindFirstFile and FileFindNextFile to do this easily from AutoIt. The helpfile for those functions contains an example of enumerating over a set of files that match the pattern, but you are only interested in the first, so only have to call FileFindNextFile once.
I would implement it as a function like the following:
; Returns the matching file name.
; If the file could not be found then return an empty string and sets #error to 1
Func _FindFile($wildcard)
Local $hSearch = FileFindFirstFile($wildcard)
If $hSearch = -1 Then Return SetError(1, 0, "")
Local $ret = FileFindNextFile($hSearch)
Local $found = Not #error
FileClose($hSearch)
If Not $found Then Return SetError(1, 0, "")
Return $ret
EndFunc ;==>_FindFile
Which would then be called like this (to find Program Files (x86)):
MsgBox(0, "Test", _FindFile("C:\Program Files (*)"))
Once you have the file name then Run(...) etc. etc. the rest of it should be fairly normal stuff you can find the helpfile.