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

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.

Related

AHK: Paste into Notepad

I just want to paste the current clipboard content into a new Notepad-window, but the following doesn't work:
#t::
Run "notepad.exe"
WinWaitActive, Untitled - Notepad
WinActivate, Untitled - Notepad ; to bring the window to the front ?
ControlSend, Edit1, ^v, ahk_class Notepad
return
Also...
ControlSetText, RichEditD2DPT1, New Text Here
...only works (writes New Text Here into Notepad) after I click into Notepad's text field myself manually.
How can I write the current clipboard item automatically into a new Notepad window via AHK, best via Ctrl+V?
Run notepad,,, PID ; runs notepad storing its unique Process ID (PID)
WinWait, ahk_pid %PID%
WinActivate, ahk_pid %PID%
WinWaitActive, ahk_pid %PID%
SendInput, ^v
https://www.autohotkey.com/docs/v1/lib/Run.htm#Parameters

AutoHotKey select pop-up window not work

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.

AutoHotKey IfWinActive not working

I'm trying to use a simple script that would work only when the active window is open and when you're in that window (e.g. some windowed mode game). So the script works, but it also works outside the active window, e.g. on desktop or a browser too. I don't need that. I need it to work only in the active window I set.
The script:
RButton::rightMouseClick()
rightMouseClick(){
if WinActive("ahk_class Notepad") {
WinMaximize
Send, Some text.{Enter}
return
}
}
So, this example works when you go to Notepad and right click. But also now right click doesn't work anywhere else on the computer? It works only if you hold down shift?!
How do I make this script react/work/run only when active window is Notepad? And not work globally.
Here is my suggestion:
#If WinActive("ahk_class Notepad") ;if the window with the ahk_class "Notepad" is active
RButton:: ;this hotkey is only enabled while the above statement is true
WinMaximize ;maximize the active window
Send, Some text.{Enter} ;send some keyboard input
Return ;end of the hotkey
#If ;end of the special "#If" statement
Correct indentation alone can help a lot understanding the code.
#IfWinActive ahk_class Notepad
RButton::
WinMaximize
Send, Some text.{Enter}
Return
#If

Autohotkey - Copy selected text, paste in Notepad++

I'm new to Autohotkey, tried to make a macro to:
Copy selection
Paste it in Notepad++
Playback Notepad++ macro (for formatting) shortcut: Ctrl+Shift+B
Copy all the edited text
Paste in Firefox text field
I tried starting with the following code, but I couldn't even get AHK to copy paste my selection to Notepad++.
^!x::
Send, ^c
ClipWait
IfWinExist, Notepad++
{
WinActivate
Send ^v
}
Here is working script:
^!x::
Send, ^c
ClipWait
SetTitleMatchMode, 2
IfWinExist, Notepad
{
WinActivate
Send, ^v
}
return
I added command SetTitleMatchMode, 2 as Joe DF mentioned in comments. That command (link) with parameter 2 sets the matching behavior of IfWinExist command so that a window's title can contain WinTitle anywhere inside it to be a match. Also added return in the end.

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.