AHK: Paste into Notepad - autohotkey

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

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.

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.

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.

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.