How to open the browser in collapsed form? - autoit

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.

Related

How to run custom function in a specific window and work in background?

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.

How to set focus on a client under mouse cursor when a tag is changed?

When I switch to another tag, a new client gets selected, but it is sometimes not a client that I have my mouse cursor over. To get a client under my mouse pointer focused, I have to either click somewhere on it, or switch to it with Mod4+j / k, or move mouse cursor out and back on that client.
I want awesome to give focus to a client that is under the mouse cursor whenever a tag is changed. How do I do that?
I found a function mouse.object_under_pointer() that finds the client I need, but I don't know when to call that function. Should I connect a handler to some particular signal? I tried connecting to various signals from Signals page on the wiki and checking with naughty.notify() if that is the right signal, but none of them were triggered when I was switching between tags.
This code did the trick, however there should be a better way to do this than setting up a huge 200 ms timer (smaller timeouts didn't properly focus some clients for me, but you can try setting a smaller one).
tag.connect_signal(
"property::selected",
function (t)
local selected = tostring(t.selected) == "false"
if selected then
local focus_timer = timer({ timeout = 0.2 })
focus_timer:connect_signal("timeout", function()
local c = awful.mouse.client_under_pointer()
if not (c == nil) then
client.focus = c
c:raise()
end
focus_timer:stop()
end)
focus_timer:start()
end
end
)
tag is this global object, so you should just place this code anywhere in your rc.lua.
Two things should be done:
First, you should remove require("awful.autofocus") from your config, so that this module no longer tries to focus some client via the focus history when you switch tags.
Then, this code works for me:
do
local pending = false
local glib = require("lgi").GLib
tag.connect_signal("property::selected", function()
if not pending then
pending = true
glib.idle_add(glib.PRIORITY_DEFAULT_IDLE, function()
pending = false
local c = mouse.current_client
if c then
client.focus = c
end
return false
end)
end
end)
end
This uses GLib directly to get a callback for when no other events are pending. This should mean that "everything else" was handled.
I know this is pretty old, but it helped me to come up with this
function focus_client_under_mouse()
gears.timer( { timeout = 0.1,
autostart = true,
single_shot = true,
callback = function()
local n = mouse.object_under_pointer()
if n ~= nil and n ~= client.focus then
client.focus = n end
end
} )
end
screen.connect_signal( "tag::history::update", focus_client_under_mouse )

Close windows in reverse order of creation

I have a program with a main window and multiple other windows and want to use autoit to close these windows. The main window however refuses to close if other windows of the program are open and creates a warning message.
To avoid this, I want to close the other windows first.
Because of the way the program is written, the windows are NOT in a parent-child relationship, so I cannot use "EnumChildWindows" in "user32.dll".
So my other option would be to obtain the order or time the windows were created at, and close them in reversed order. Is there any way to do this?
You can try to get it by the process. The function below will return the z-order of the windows, as far as I know, without you tracking the "creation time" of the windows themselves from within your own code, you won't get a solution that can do that.
#include <WinAPIProc.au3>
#include <WinAPISys.au3>
#region - Example
Global $gahWnd[3]
For $i = 0 To 2
$gahWnd[$i] = GUICreate("Example " & $i)
GUISetState(#SW_SHOW, $gahWnd[$i])
Next
Global $gaExample = _WinGetByProc(#AutoItPID)
If #error Then Exit 2
Sleep(3000) ; so you can at least see the windows were created
; windows are returned in the "last z-order"
; so they'll be returned "Example 2, Example 1, Example 0"
; This makes it easy to close all but the first one
; use -1 to keep the first window created
For $i = 1 To $gaExample[0] - 1
ConsoleWrite("Closing: " & $gaExample[$i] & ":" & WinGetTitle($gaExample[$i]) & #CRLF)
WinClose($gaExample[$i])
Next
Global $gaMsg
While 1
$gaMsg = GUIGetMsg(1)
Switch $gaMsg[1]
Case $gahWnd[0]
Switch $gaMsg[0]
Case -3
Exit
EndSwitch
Case $gahWnd[1]
Switch $gaMsg[0]
Case -3
GUIDelete($gahWnd[1])
EndSwitch
Case $gahWnd[2]
Switch $gaMsg[0]
Case -3
GUIDelete($gahWnd[2])
EndSwitch
EndSwitch
WEnd
#EndRegion - Example
; pass the exe or the process id
Func _WinGetByProc($vExe)
; will return pid if exe name sent or validate if pid is sent
Local $iPID = ProcessExists($vExe)
If Not ProcessExists($iPID) Then
Return SetError(1, 0, 0)
EndIf
; enum desktop window only (top-level-windows)
Local $ahWnds = _WinAPI_EnumDesktopWindows(_WinAPI_GetThreadDesktop( _
_WinAPI_GetCurrentThreadId()))
If #error Or Not IsArray($ahWnds) Then
Return SetError(2, 0, 0)
EndIf
Local $aExeContainer[11], $iDim
For $iwnd = 1 To $ahWnds[0][0]
; compare windows pid with our pid
If (WinGetProcess(HWnd($ahWnds[$iwnd][0])) = $iPID) Then
$iDim += 1
; sanity check, be sure the array has the right number of indexes
If (Mod($iDim, 10) = 0) Then
ReDim $aExeContainer[$iDim + 10]
EndIf
$aExeContainer[$iDim] = HWnd($ahWnds[$iwnd][0])
EndIf
Next
; if there were no matches return
If Not $iDim Then
Return SetError(3, 0, 0)
EndIf
; trim array and set number found in the zero index
ReDim $aExeContainer[$iDim + 1]
$aExeContainer[0] = $iDim
Return $aExeContainer
EndFunc

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

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