I would like to be able to trigger "Search Tabs" from Autohotkey, since I have lots of opened tabs this would help me quickly find the tab I am looking for, I know I might be able to loop through all tabs by scripting this, but that's not what I want.
This is what I've done
!+a::
WinActivate, Brave
Sleep, 100
Send, {Ctrl}{Shift}a
Return
If I change Send, {Ctrl}{Shift}a with Send, {Ctrl}t is correctly opens a tab, so the problem must be either some error in my {Ctrl}{Shift}a configuration or that Brave is somehow not reacting.
The most appropriate way to activate a specific window and send keystrokes to it is as follows:
!+a::
SetTitleMatchMode, 2 ; if you want to use it only in this hotkey
IfWinNotExist, Brave
{
MsgBox, Cannot find Brave
Return ; end of the hotkey's code
}
; otherwise:
WinActivate, Brave
WinWaitActive, Brave, ,2 ; wait 2 seconds maximally until the specified window is active
If (ErrorLevel) ; if the command timed out
{
MsgBox, Cannot activate Brave
Return
}
; otherwise:
; Sleep 300 ; needed in some programs that may not react immediately after activated
Send, ^+a
Return
Otherwise the script can send the keystrokes to another window.
For not repeating the whole code in every hotkey you can create a function:
!+b::
SetTitleMatchMode, 2
Activate("Brave", 2)
; Sleep 300
Send, ^+a
Return
Activate(title, seconds_to_wait){
IfWinNotExist, % title
{
MsgBox % "Cannot find """ . title . """."
Return
}
; otherwise:
WinActivate, % title
WinWaitActive, % title, ,% seconds_to_wait
If (ErrorLevel)
{
MsgBox % "Cannot activate """ . title . """."
Return
}
}
Related
I am writing an expect script to do make (compile). It's a multi step script and I try to catch the error and stop the script if there is any.
Here is a sample:
set BLD_PASS 1
set BLD_PASS [catch {exp_send "make buildXYZ\r"} output]
expect {
folder] {
if { $BLD_PASS == "0" } {
send_user "Build passed on XYZ \n\n"
} else {
send_user "Build failed on XYZ\n\n"
exit
}
}
}
The problem is I always get BLD_PASS as 0 even when there is real failure.
As I see, the set command gets executed and the BLD_PASS gets the value immediately though the 'make' takes minutes to finish. So, it doesnt look like it's really getting the output of make.
I have verified 'make' outputting the right return code by using it in a bash script.
Could anyone please help me to solve this problem ? Appreciate your help.
Please let me know if you need more info.
The reason why your code isn't working is that the send itself is succeeding: it sent the message to the other end. It just didn't get the result back yet. It might be easiest to do this by asking the other side to report the exit status and then listen for that.
send "make buildXYZ; echo FROGBARGLE_\$?_BARGLEFROG\r"
expect {
-re {FROGBARGLE_([^\s_]+)_BARGLEFROG} {
puts "exit code was $expect_out(1,string)"
}
}
There are fancier ways of tracking this sort of thing (and FROGBARGLE_…_BARGLEFROG is perhaps a bit too arbitrary; I picked it to be something really unlikely to occur in make output) but this sort of thing in general is the way you do it when working remotely: you have to tell the other side to tell you what happened.
This script is supposed to press Z every 50 um..units of time when I press Q until I press W.
What I have trouble doing is making it pause(stops pressing z) whenever chrome is not in focus and resumes when it comes back into focus again.
Thank you. Below is the script that does what I said in the first sentence.
q::
stop = 0
Loop
{
SendInput, z
Sleep 50 ;adjust for speed of repetition
if stop
break
}
return
w::
stop = 1
return
Try adding a WinWaitActive command after the sleep command.
I want to have keyboard shortcut in Vim to copy a whole function from a Powershell file to the Windows clipboard. Here is the command for it:
1) va{Vok"*y - visual mode, select {} block, visual line mode, go to selection top, include header line, yank to Windows clipboard.
But it would work only for functions without an inner {} block. Here is a valid workaround for it:
2) va{a{a{a{a{a{a{Vok"*y - the same as (1), but selecting {} block is done multiple times - would work for code blocks that have 7 inner {} braces.
But the thing is - the (1) command works fine when called from a vim function, but (2) misbehaves and selects wrong code block when called from a vim function:
function! CopyCodeBlockToClipboard ()
let cursor_pos = getpos('.')
execute "normal" 'va{a{a{a{a{a{a{Vok"*y'
call setpos('.', cursor_pos)
endfunction
" Copy code block to clipboard
map <C-q> :call CopyCodeBlockToClipboard()<CR>
What am I doing wrong here in the CopyCodeBlockToClipboard?
The (2) command works as expected when executed directly in vim.
UPDATE:
I've noticed that:
if there are more a{ then the included blocks in the function
then vim wouldn't execute V
Looks like vim handles errors differently here. Extra a{ produces some error and regular command execution just ignores it. But execution from withing a function via :normal fails and wouldn't call V (or probably any command that follows the error).
Any workaround for this?
Try this function
function! CopyCodeBlockToClipboard()
let cursor_pos = getpos('.')
let i = 1
let done = 0
while !done
call setpos('.', cursor_pos)
execute "normal" 'v' . i . 'aBVok"*y'
if mode() =~ "^[vV]"
let done = 1
else
let i = i + 1
endif
endwhile
execute "normal \<ESC>"
call setpos('.', cursor_pos)
endfunction
This preforms a execute command to select blocks until it fails to select a block larger block. ([count]aB selects [count] blocks) It seems when the selection fails we end up in visual mode. So we can use mode() to check this.
When this function exits you should be in normal mode and the cursor should be restored to where you started. And the function will be in the * register.
This macro should come close to what you want to achieve:
?Function<CR> jump to first Function before the cursor position
v enter visual mode
/{<CR> extend it to next {
% extend it to the closing }
"*y yank into the system clipboard
I am using autohotkey to do some automate process.
I need help with closing chrome.exe
I tried
Process, Close, chrome.exe
; or
Run taskkill /im chrome.exe
but it give me chrome crashed error when I start it again.
Thanks
Use WinClose to send a close message to the window, rather than killing the process:
SetTitleMatchMode 2
WinClose Google Chrome
mihai's helpful answer works well if a single Chrome window (typically containing multiple tabs) is open.
However, more work is needed if you want to close all open Chrome windows:
; Get all hWnds (window IDs) created by chrome.exe processes.
WinGet hWnds, List, ahk_exe chrome.exe
; Loop over all hWnds and close them.
Loop, %hWnds%
{
hWnd := % hWnds%A_Index% ; get next hWnd
WinClose, ahk_id %hWnd%
}
Also note that the WinClose documentation calls using WinClose's use of the WM_CLOSE message "somewhat forceful" and suggests the following PostMessage alternative, which mimics the user action of pressing Alt-F4 or using the Window menu's Close item:
; Get all hWnds (window IDs) created by chrome.exe processes.
WinGet hWnds, List, ahk_exe chrome.exe
; Loop over all hWnds and close them.
Loop, %hWnds%
{
hWnd := % hWnds%A_Index% ; get next hWnd
PostMessage, 0x112, 0xF060,,, ahk_id %hWnd%
}
Here is a function for 'soft closing' specific processes:
CloseAllInstances( exename )
{
WinGet LhWnd, List, % "ahk_exe " exename
Loop, %LhWnd%
PostMessage, 0x112, 0xF060,,, % "ahk_id " LhWnd%A_Index%
}
So you simply need to call the following line to close all chrome instances:
CloseAllInstances( "chrome.exe" )
As I've started using AutoHotKey daily, I thought it'd be a good idea to implement it in my coding.
I'd like it to create a structure like this:
{
(Tab)
}
when { followed by an Enter are entered.
So far, I've got:
:*{Enter::
SendInput, {{}
SendInput, {Enter}
SendInput, {Enter}
SendInput, {}}
SendInput, {Up}
SendInput, {Tab}
return
but I keep getting errors and strange anomalies.
There are 2 things that must be included in order to get your code to work:
1. a backtick must be used for a curly bracket to be in hotstring
2. the option 'o' must be used to prevent a return from being sent after a curly bracket within your send command.
Try the following:
:o:`{::{{}`n`n{}}{up}{tab}
Hotstring options
Note: You may need to modify ending characters for this to fire only on Enter. This will affect hotstrings globally.
#Hotstring EndChars `n
Try this.
Send, {{}{Enter}{tab}{Enter}{}}{up}{End}
This works for me, but your hotkey "*{Enter" was not accepted by my AutoHotKey_L, so I used an other temporary hotkey "^q".
This hotstring worked for me.
:*:`{`n::
That should catch it without need for settning the EndChars.