Functions "_IEFormElementSetValue" and "_IEAction" in AutoIT - autoit

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.

Related

How to open the browser in collapsed form?

The code:
Local $iPID=ShellExecute("C:\Program Files\Mozilla Firefox\firefox.exe", "","","", #SW_MINIMIZE);
How to open the browser in collapsed form?
; Optional debugging feature.
Opt('TrayIconDebug', True)
; Run Firefox if not running.
If Not ProcessExists('firefox.exe') Then
ShellExecute('"C:\Program Files\Mozilla Firefox\firefox.exe"')
EndIf
; Wait for a window to match the class.
WinWait('[CLASS:MozillaWindowClass]')
$hFirefox = 0
$iAbort = 5
Do
Sleep(1000)
; Get all class windows into an array.
$aWList = WinList('[CLASS:MozillaWindowClass]')
For $i1 = 1 To UBound($aWList) -1
; Get the visible window from the window handle.
If BitAND(WinGetState($aWList[$i1][1]), 2) Then
$hFirefox = $aWList[$i1][1]
_WinState($aWList[$i1][1], $aWList[$i1][0], True)
ExitLoop 2
Else
_WinState($aWList[$i1][1], $aWList[$i1][0], False)
EndIf
Next
$iAbort -= 1
Until Not WinExists('[CLASS:MozillaWindowClass]') Or $iAbort = 0
; Minimize the visible window.
If WinSetState($hFirefox, '', #SW_MINIMIZE) Then
ConsoleWrite('A Firefox window has been minimized.' & #CRLF)
EndIf
Exit
Func _WinState($hWinHandle, $sWinTitle, $bWinVisible = False)
; Debug output for visible state of a window.
Local $sWinVisible = $bWinVisible ? 'True' : 'False'
ConsoleWrite(StringFormat('Handle: %s Visible: %-5s Title: %s', $hWinHandle, $sWinVisible, $sWinTitle) & #CRLF)
EndFunc
Even if you set a Firefox shortcut to minimize and use it
to start Firefox, Firefox may not minimize at startup.
This is why the show flag of #SW_MINIMIZE for the
ShellExecute function may not work.
A -tray argument exists though I cannot get it to work as
mentioned in Command Line Options.
To minimize at startup, the code provided may help.
It will find the 1st window that is visible of the class
MozillaWindowClass, and will attempt to minimize it.
The variable $hFirefox may contain the handle of the
visible Firefox window which can be used after the Do
loop has complete.
Some code is optional, i.e. _WinState() is used to output
debug information to console to know if the the window search
for the visible window is working OK.

autoit3 - trigger context menu to open on button click/press

I am working on a project, in which I decided I should have history of previously searched stuff (since it's an app with which you can search stuff), so I have been thinking that I could just create the context menu or regular menu, that I could somehow force to activate on a button click or by using accelerators.
Now, I haven't been able to figure out how to make that work.
First I tried the GuiCtrlSetState() function, which didn't seem to work (I tried show and focus), then I tried accelerators and they didn't do anything.
So I'm wondering is there any way I can make the context menu or regular menu pop up on button press/click or key press (with accelerators)?
So what I'm basically trying to do is some sort of a pop-up, you click on a button or press a key to trigger the context menu or regular menu to open.
I know I could be old school and use a new GUI or just a combo box for the history or anything but I kinda want this, if there's any way to do so.
Any help, or an alternative way of doing the same thing appreciated.
Here's the testing code in which I was trying to make the test button trigger and activate the context menu, and I have tried the same with the regular
menu: 
#NoTrayIcon
#include <GUIConstantsEx.au3>
GUICreate("test123")
$but = guictrlcreatebutton("test", -1, -1, -1)
$xt = GUICtrlCreateContextMenu("test context menu")
GUICtrlCreateMenuItem("test1", $xt)
GUICtrlCreateMenuItem("test2", $xt)
GUISetState(#SW_MAXIMIZE)
While 1
$msg = GUIGetMsg()
If $msg = $but Then
GUICtrlSetState($xt, $GUI_FOCUS)
EndIf
Sleep(5)
WEnd
If you check the help file under GUICtrlCreateContextMenu:
; right click on gui to bring up context Menu.
; right click on the "ok" button to bring up a controll specific context menu.
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
GUICreate("My GUI Context Menu", 300, 200)
Local $idContextmenu = GUICtrlCreateContextMenu()
Local $idNewsubmenu = GUICtrlCreateMenu("new", $idContextmenu)
Local $idNewsubmenuText = GUICtrlCreateMenuItem("text", $idNewsubmenu)
Local $idButton = GUICtrlCreateButton("OK", 100, 100, 70, 20)
Local $idButtoncontext = GUICtrlCreateContextMenu($idButton)
Local $idMenuAbout = GUICtrlCreateMenuItem("About button", $idButtoncontext)
Local $idMenuOpen = GUICtrlCreateMenuItem("Open", $idContextmenu)
Local $idMenuSave = GUICtrlCreateMenuItem("Save", $idContextmenu)
GUICtrlCreateMenuItem("", $idContextmenu) ; separator
Local $idMenuInfo = GUICtrlCreateMenuItem("Info", $idContextmenu)
GUISetState(#SW_SHOW)
; Loop until the user exits.
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $idButton
MsgBox($MB_SYSTEMMODAL, "Button Clicked", 'OK')
Case $idMenuAbout
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'About')
Case $idMenuOpen
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Open')
Case $idMenuSave
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Save')
Case $idMenuInfo
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Info')
Case $idNewsubmenuText
MsgBox($MB_SYSTEMMODAL, "SubMenu Selected", 'Text')
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>Example

AutoIT WinWaitActive doesn't uderstands that window is active

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

Subscript Dimension Error When Dynamically Creating Buttons in AutoIt

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

Awesome wm keyup and keydown events

I'm using Awesome Window Manager. I want to show my top bar by pressing mod4 and then hide it when I release. I tired passing "keyup Mod4" to awful.key but it does not work. How can I tell it that I want to trigger an event on keyup?
Try
`awful.key({ modkey }, "", nil, function () staff here end)`
3rd param is handler for "release" event when passed.
I wanted the same thing! After some research I came up with:
Use external program to execute echo 'mywibox[1].visible = true' | awesome-client when mod4 is pressed and echo 'mywibox[1].visible = false' | awesome-client when released.
Use other key, not modifier, like Menu (near right Ctrl), because for some reason you can't hook up press and release event to mod4 (or it just doesn't work).
Here is my solution (timer is required because pressed key sends events as long as it is pressed):
-- Put it somewhere at the beginning
presswait = { started = false }
-- Put it in key bindings section (globalkeys = within awful.table.join)
awful.key({ }, "Menu", function()
if presswait.started then
presswait:stop()
else
-- One second to tell if key is released
presswait = timer({ timeout = 1 })
presswait:connect_signal("timeout", function()
presswait:stop()
-- Key is released
for i = 1, screen.count() do
mywibox[i].visible = false
end
end)
-- Key is pressed
for i = 1, screen.count() do
mywibox[i].visible = true
end
end
presswait:start()
end)
You could connect a signal to the key object:
key.connect_signal("press", function(k)
-- Analyze k and act accordingly
end)
More about signals here:
http://awesome.naquadah.org/wiki/Signals
Using the first suggestion from https://stackoverflow.com/a/21837280/2656413
I wrote this python script: https://github.com/grandchild/autohidewibox
What it does is, it runs xinput in the background and parses its output. You could also parse /dev/input/event1 directly in python, but I was lazy.
It then pipes the following lua code to awesome every time the key is pressed or released:
echo 'for i, box in pairs(mywibox) do box.visible = true end' | awesome-client
and
echo 'for i, box in pairs(mywibox) do box.visible = false end' | awesome-client
respectively.
Update:
For awesome 4+ use:
echo "for s in screen do s.mywibox.visible = false end" | awesome-client
or true.

Resources