AutoHotKey select pop-up window not work - autohotkey

I write a script to test selecting pop-up windows.
SetTitleMatchMode, 2
winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
IfWinExist,%winTitle%
{
WinActivate
send !{F4}
}
IfWinExist,%popWin%
{
WinActivate
WinWaitActive, %popWin%
WinGetClass, outputvar, %popWin%
MsgBox %outputvar%
}
This script is intended to send ALT-F4 to close an opened R window, and when the confirmation pop-up window occurs, display the pop-up window's class name.
The first if block works fine. However, the send if block sometimes works, sometimes not. Active window info shows the pop-up windows' class info is:
Window Title, Class and Process
Question
ahk_class #32770
ahk_exe Rgui.exe
snapshot of the above info
I don't know why IfWinExist,%popWin% does not work. I tried changing popWin:="ahk_class #32770 ahk_exe Rgui.exe" to popWin:="ahk_class #32770", still it sometimes works, and sometimes not. So what should I do to select the pop-up windows correctly?

I have changed your AutoHotkey code, such that it should give you the functionality you require.
SetTitleMatchMode, 2
winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
if (hWnd := WinExist(winTitle)) ;this assigns hWnd, it does not compare hWnd with WinExist(winTitle)
{
;WinActivate, ahk_id %hWnd%
;send !{F4}
WinClose, ahk_id %hWnd% ;WinClose is more direct than Alt+F4 if it works (Send can potentially send key presses to the wrong window if a new window suddenly appears)
}
WinWait, %popWin%, , 5 ;wait for window to exist, give up after 5 seconds
if !ErrorLevel ;if window found within 5 seconds
{
WinGet, hWnd, ID, %popWin%
WinActivate, ahk_id %hWnd%
WinGetClass, outputvar, ahk_id %hWnd%
MsgBox %outputvar%
}
Note:
In most cases WinActivate requires a window title/hWnd be specified.
The second part of your code works sometimes but not other times, probably because if the popup appears very quickly, then IfWinExist will find the window, but if the popup appears slowly, then the IfWinExist check will occur before the window exists, and will thus not find the window.

Related

Activate most recently active window in a group with autohotkey

I am making an autohotkey script to switch to a text editor, but depending on what I'm working on I may have different text editors.
I created a group TextEditor with my three text editors:
GroupAdd, TextEditor, ahk_class Notepad++
GroupAdd, TextEditor, ahk_exe Code.exe
GroupAdd, TextEditor, ahk_class Notepad
The group seems to be created properly but whenever I use GroupActivate, TextEditor, R it always opens Nodepad++ and repeated presses switch between the three editors.
I want it to open whatever I last used first, like if I was using Code and switched to another program it should activate Code again.
GroupAdd, TextEditor, ahk_class Notepad++
GroupAdd, TextEditor, ahk_exe Code.exe
GroupAdd, TextEditor, ahk_class Notepad
F1::
IfWinActive ahk_group TextEditor
GroupActivate, TextEditor, R
else
{
list := ""
; windows are retrieved in order from topmost to bottommost:
WinGet, id, list, ahk_group TextEditor
Loop, %id%
{
this_ID := id%A_Index%
WinActivate, ahk_id %this_ID% ; activate the most recently active window in this group
break
}
}
return
https://autohotkey.com/docs/commands/WinGet.htm#List

How can I set AutoHotKey's code to run only in chrome?

I want to simulate a "find" operation when I double click a word in Chrome.
I've managed to do this :
~LButton::
SetTitleMatchMode, 2
#IfWinActive, ahk_class Chrome
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
SendInput ^c
Sleep, 10
SendInput ^f
Sleep, 10
SendInput ^v
}
Return
But it runs even for non-chrome process (when double click a word)
Question:
How can I make this script run only when double click in chrome ?
#IfWinActive, ahk_exe Chrome.exe ;; start of IfWinActive condition, for me it didn't work with ahk_class so i changed it to ahk_exe
~LButton::
SetTitleMatchMode, 2
If (A_TimeSincePriorHotkey<400) and (A_TimeSincePriorHotkey<>-1)
{
SendInput ^c
Sleep, 10
SendInput ^f
Sleep, 10
SendInput ^v
}
Return
~RButton::
SendInput {Escape}
Return
#IfWinActive ;; end of condition IfWinActive
You can get the title of the window or active window with WinGet - and then only apply the code IF that given window is active.
SetTitleMatchMode, 2
IfWinActive, - Google Chrome
This is a snippet I found:
DetectHiddenText, On ; the url of the active Chrome window is hidden text...
SetTitleMatchMode, Slow ; ...we also need match mode 'slow' to detect it
; activate chrome window,
; just for demonstation:
WinActivate, ahk_class Chrome_WidgetWin_1
IfWinActive, ahk_class Chrome_WidgetWin_1 ; we only want to check for the hidden text if Chrome is the active window,
{
WinGetText, wintext, ahk_class Chrome_WidgetWin_1 ; if it is we grab the text from the window...
If InStr(wintext,"autohotkey.com") ; ...and if it contains a url that we want,
{
;### code conditional on url goes here ###
msgbox % "The active Chrome window is on the autohotkey.com domain! The page title and URL is:`n`n>" wintext ; <<== we run the desired code here.
}
}
exitapp
but it needs to be changed to fit your specific needs.

Solution to a "dynamic"/"unstable" ahk_class name?

In AutoHotKey, ahk_class NAME has been a good identifier for programs. Typically, I now use it in the following two ways:
Simply calling the most recently called active instance:
; Pressing Win+Shift+X will bring up (or switch to) the Windows Journal
Application
#+x::
IfWinExist ahk_class JournalApp
WinActivate ahk_class JournalApp
else
Run C:\Program Files\Windows Journal\Journal.exe
return
Grouping windows with the same ahk_class and looping through them:
GroupAdd, FIRE, ahk_class MozillaWindowClass
; Pressing Win+3 will go through all open Firefox windows one by one.
#3::
IfWinExist ahk_class MozillaWindowClass
GroupActivate, FIRE, r
else
Run firefox
return
However, there are certain programs that do not have a stable ahk_class name. For example, the ArcMap.exe should
display the following information in the "Windows Spy" window of AHK:
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Untitled - ArcMap
ahk_class Afx:012F0000:b:00010003:00000006:001C0BB0
Any idea how could I refer to this application that has dynamic ahk_class?
I am aware that there is also something called ahk_exe. However, I did not find it compatible with the method GroupAdd,
GroupActivate or IfWinExist.
You definitely are on the right track trying to incorporate ahk_exe. GroupAdd (and every other command receiving a WinTitle parameter at that) is compatible with ahk_exe in AHK_L; this sample code works perfectly on my machine:
GroupAdd, notepad, ahk_exe notepad.exe
Run, notepad.exe
WinWaitActive, ahk_group notepad
Sleep, 1000
WinClose, ahk_group notepad
I suspect you're not using the most recent version of AHK. Get it here and have another try.

executing a script when at a specific classNN

How shouuld I tweak the following script so that the hotkey is activated not at ANY moment where I'm at the class shown (that is Outlook) but at a specific sub window ( the preview pane of the Inbox box (whose classNN is _WwG1 )) ?
#IfWinActive ahk_class rctrl_renwnd32
+!m::
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You don't seem to be in context.
return
#IfWinActive
Make the hotkey look for active controls once it is activated. This way you can use the same hotkey for multiple commands, each command depending on the control. You can do this with several if/else statements to test for the subcontrols.
The hotkey only works in Outlook.
Each control has its own command
Each command is limited to that particular control
#ifwinactive, ahk_exe outlook.exe
{
+!m::
controlgetfocus, thiscontrol
if(thiscontrol = "_Wwg1"){
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You seem to focused on %thiscontrol%
}else if(thiscontrol = "_Wsg2){
msgbox, you've discovered the second control!
}
return
}

autohotkey not able to get Bing Desktop control

The Bing Desktop has a Win + Y hotkey to toggle the display of itself. But I want to bring it foreground with my Win + I hotkey.
I met with a problem that I can't set focus to the search box with ControlFocus, Edit1 where Edit1 is the ClassNN reported by Window Spy.
The script is posted here:
#i::
IfWinExist, ahk_exe BingDesktop.exe
{
IfWinNotExist, ahk_class BingToolBand
{
Send #y
Return
}
WinWait, ahk_class BingToolBand
WinActivate
WinWaitActive
ControlFocus, Edit1
Return
}
Run "C:\Program Files (x86)\Microsoft\BingDesktop\BingDesktop.exe"
Return
I figured out that the ahk_class BingToolBand is not the top windowtitle of Bing Desktop.
I should have used 必应Bing 缤纷桌面 instead.