How do I return from inside an if/else block in Autohotkey? - autohotkey

Below is the code snippet I used. If you are doing something in a browser, it should detect that you are currently in a browser and copy the URL to the clipboard. If you are not in a browser, then since there is no URL there is no question of doing that.
Regardless what browser I am in, the first MessageBox is always outputting the value 0. However, the MessageBox inside the function never gets activated, that is "No browser detected" message is never displayed. The value therefore can't be zero, yet when I am in notepad the if function still gets processed.
if (Retrieve_Browser_Type <> 0)
{
Send, {F6}
Send, ^{ins}
URL = %clipboard%
MsgBox, % Retrieve_Browser_type
MsgBox, URL copied: %URL%
}
Retrieve_Browser_Type() {
IfWinActive, ahk_class MozillaWindowClass return 1
IfWinActive, ahk_class Maxthon3Cls_MainFrm return 2
IfWinActive, ahk_class IEFrame return 3
IfWinActive, ahk_class Chrome_WidgetWin_1 return 4
MsgBox, No browser detected
return 0
}

It was a syntax mistake that thee compiler didn't detect.
Instead of:
IfWinActive, ahk_class MozillaWindowClass return 1
You ought to write:
IfWinActive, ahk_class MozillaWindowClass
return 1

Related

How to check if clipboard is equal to a predetermined number, or several numbers in AHK?

; Copy cell
var := clipboard
sleep, 1000
WinActivate, doesntmatter - Internet Explorer
IfEqual 30684047, %var%
{
sleep, 500
Send, inform
}
else
{
msgbox, nope
}
return
My problem is that even though I have the correct number (30684047) in the clipboard, the code still goes straight to the MsgBox and tells me that the clipboard (%var%) isnt equal to the predetermined code.
What am i missing? I am 100% sure that %var% contains my copied code from the clipboard because if i do a MsgBox with %var% after i copy it, it gives me a box containing that correct code.
Look back at the documentation for IfEqual...you switched var and value.
; Copy cell
var := clipboard
sleep, 1000
WinActivate, doesntmatter - Internet Explorer
IfEqual, var, 30684047
{
sleep, 500
Send, inform
}
else
{
msgbox, nope
}
return
There's really no need to save off the value of clipboard to a different variable unless you're wanting to re-use that value after clipboard has later changed. So the above could also be:
WinActivate, doesntmatter - Internet Explorer
If (clipboard = "30684047") {
sleep, 500
SendInput, inform
}
else
msgbox, nope
return

AHK: closing a window whenever it pops up

I wrote an AHK script designed to copy a text from Adobe Acrobat when I press F9. It then changes it according to a regex and shows the copied text in tooltip. Additionally, I added code to automatically close an annoying window that Acrobat sometimes shows when coping a text, an infamous There was an error while copying to Clipboard. An internal error occurred. When this window doesn't show up, the script keeps showing a tooltip, which is designed to close after a specified amount of time. I've been banging my head against the wall but I don't know how to correct this.
;#NoTrayIcon
#Persistent
#SingleInstance
F9::
#If WinActive("ahk_exe Acrobat.exe")
{
Clipboard:=""
send,^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer,CheckForMsgBox,100
CheckForMsgBox:
IfWinExist, Adobe Acrobat
{
Send {Enter}
SetTimer,CheckForMsgBox,Off
}
;Return
If (StrLen(Clipboard) < 120)
ToolTip % Clipboard
Else
ToolTip Copied
SetTimer, ToolTipOff, -1000
return
}
#If
ToolTipOff:
ToolTip
return
;#NoTrayIcon
; #Persistent ; (1)
#SingleInstance
SetTimer,CheckForMsgBox,100 ; (2)
return
#If WinActive("ahk_exe Acrobat.exe") ; (3)
F9::
clipboard:=""
send,^c
ClipWait, 1
Clipboard := RegExReplace(Clipboard, "\r\n", " ")
If (StrLen(Clipboard) < 120)
ToolTip %Clipboard%
Else
ToolTip Copied
SetTimer, ToolTipOff, -1000
return
#If ; turn off context sensitivity
ToolTipOff:
ToolTip
return
CheckForMsgBox:
; ControlSend, Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText
ControlSend, , {Enter}, Adobe Acrobat ; Close this unwanted window whenever it appears
return
(1) Scripts containing hotkeys, hotstrings, or any use of OnMessage() or Gui are automatically persistent.
(2) SetTimer causes a subroutine (Label) to be launched automatically and repeatedly at a specified time interval.
https://autohotkey.com/docs/commands/SetTimer.htm
(3) Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script.
https://autohotkey.com/docs/commands/_If.htm

What is the right way to send Alt + Tab in Ahk?

Ok. I know this is a very stupid question.
But I'm stuck already for an hour.
I got very little experience with ahk, however I made work every script until now with no problems. I explored the ahk tutorials but found no solution up to now.
I'm trying to switch to prev. app with a single numpad off key.
I've tried:
!{Tab}
,
{Alt down}{Tab}{Alt up}
I've tried it with Sleep delays, multiline, multiline inside brackets, with and without commas after commands, etc.
I'm quite sure is very simple but something I've not tried yet.
Any suggestion?
$F1:: AltTab()
$F2:: AltTabMenu()
; AltTab-replacement for Windows 8:
AltTab(){
list := ""
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
IfWinActive, ahk_id %this_ID%
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (!IsWindow(WinExist("ahk_id" . this_ID)))
continue
WinActivate, ahk_id %this_ID%
WinWaitActive, ahk_id %this_ID%,,2
break
}
}
; AltTabMenu-replacement for Windows 8:
AltTabMenu(){
list := ""
Menu, windows, Add
Menu, windows, deleteAll
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (!IsWindow(WinExist("ahk_id" . this_ID)))
continue
Menu, windows, Add, %title%, ActivateTitle
WinGet, Path, ProcessPath, ahk_id %this_ID%
Try
Menu, windows, Icon, %title%, %Path%,, 0
Catch
Menu, windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0
}
CoordMode, Mouse, Screen
MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
CoordMode, Menu, Screen
Xm := (0.25*A_ScreenWidth)
Ym := (0.25*A_ScreenHeight)
Menu, windows, Show, %Xm%, %Ym%
}
ActivateTitle:
SetTitleMatchMode 3
WinActivate, %A_ThisMenuItem%
return
;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------
IsWindow(hWnd){
WinGet, dwStyle, Style, ahk_id %hWnd%
if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {
return false
}
WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
if (dwExStyle & 0x00000080) {
return false
}
WinGetClass, szClass, ahk_id %hWnd%
if (szClass = "TApplication") {
return false
}
return true
}
EDIT (suggested by the user Ooker):
The script pops up a menu for you to choose.
This is what it looks like:
If you just want to switch back to the previous application, use Send, !{Esc}
You shouldn't manually send alt+tab as it is a special windows command, rather use the AltTab commands that do that for you.
AltTabMenu opens the tab menu and selects the program, whileAltTab, ShiftAltTab navigate through it.
h::AltTabMenu
n::AltTab
m::ShiftAltTab
There are some issues with Windows 8/10 and keys like ctrl-alt-del and alt-tab. Here is one solution:
F1::
{
Send {Alt Down}{Tab} ;Bring up switcher immediately
KeyWait, F1, T.5 ; Go to next window; wait .5s before looping
if (Errorlevel)
{
While ( GetKeyState( "F1","P" ) ) {
Send {Tab}
Sleep, 400 ; wait 400ms before going to next window
}
}
Send {Alt Up} ;Close switcher on hotkey release
}
return
My personal goal was to remap Alt-Tab to Win-Tab (because I'm using a Mac keyboard on a Windows 10) so I took what Stepan wrote above plus some documentation and here is is, working fine for me :
#Tab::
{
Send {LAlt Down}{Tab}
KeyWait, LWin ; Wait to release left Win key
Send {LAlt Up} ; Close switcher on hotkey release
}
return
Worked for me:
F1::
Send, {ALT DOWN}{TAB}{ALT UP}
return
It simulates the Alt + Tab behavior for F1 key.
Well, finally I found the reason and some "solutions" here and here.
It seems that Windows 8 blocks Ahk {Alt Down}{Tab} and AltTabMenu and some other keys.
For now I'm using this to scroll windows forward:
Send !{ESC}
This to display the AltTabMenu:
Run, "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"
And this to switch to the previous app as suggested in one of the topics:
LCtrl & z:: ; AltTabMenu
state := GetKeyState("Capslock", "T")
if state = 1
SetCapsLockState, Off ; CapsLock On blocks Task Switching metro window
Send, !{Tab} ; prevents displaying inactive Task Switching metro window
run, Window Switcher.lnk ; must be in script directory otherwise include path
WinWait, Task Switching,, 2
KeyWait, Ctrl
Send, {Enter}
if state = 1
SetCapsLockState, On ; restores CapsLock State
state =
return
#IfWinActive, Task Switching
LCtrl & q::Send, {Right}
LCtrl & a::Send, {Left}
It would be great to get to the previous app with no AltTabMenu splashing.
In case you want to do multiple "tabs", then the below function should help doing that. This was at least own solution on my Windows 8.1 machine.
The approach is:
1) Get a list of all the windows
2) Loop 1:
find the index of the current window
set the index to switch to ("current" + "offset")
3) Loop 2:
loop until you hit the index to switch to, then switch window
AutoHotKey code sample below:
; Test switch of 1 window
F1::AltTabFunction(offset:=1)
; Test switch of 2 windows
F2::AltTabFunction(offset:=2)
AltTabFunction(offset:=1)
{
; ****************************
; Function for switching windows by ALT-TAB (offset = number of windows to "tab")
; ****************************
; Get list of all windows.
WinGet, AllWinsHwnd, List
WinGetTitle, active_title, A ; Get title of active window.
; Find index of the current window.
counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
Loop, % AllWinsHwnd
{
; Find title for window in this loop.
WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
; [1] : https://autohotkey.com/docs/commands/WinGet.htm
WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%
; Skip hidden windows by checking exStyle.
If !(exStyle & 0x100){
Continue
}
; Window is not hidden. Increase counter.
counter_of_none_hidden_windows := counter_of_none_hidden_windows+1
; Set flag.
titles_match := CurrentWinTitle = active_title
If (titles_match) {
window_index_to_switch_to := counter_of_none_hidden_windows+offset
break
}
}
; Find index of the window to switch to and do the actual switch
counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
Loop, % AllWinsHwnd
{
; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
; [1] : https://autohotkey.com/docs/commands/WinGet.htm
WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%
; Skip hidden windows by checking exStyle.
If !(exStyle & 0x100){
Continue
}
; Window is not hidden. Increase counter.
counter_of_none_hidden_windows := counter_of_none_hidden_windows+1
; Set flag.
found_window_to_switch_to := counter_of_none_hidden_windows = window_index_to_switch_to
; Switch window.
If (found_window_to_switch_to) {
; Get title.
WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
; Activate by title.
WinActivate, %CurrentWinTitle%
; Stop loop.
break
}
}
return ; Nothing to return
}
send {Alt down}{tab}
send {Alt up}
!{Tab} works to switch between windows if you add sleep before and after it.
Sleep 100
Send !{Tab}
Sleep 100
Please refer to this link: https://www.autohotkey.com/docs/Hotkeys.htm#alttab
To cancel the Alt-Tab menu without activating the selected window, press or send Esc. In the following example, you would hold the left Ctrl and press CapsLock to display the menu and advance forward in it. Then you would release the left Ctrl to activate the selected window, or press the mouse wheel to cancel. Define the AltTabWindow window group as shown below before running this example.
LCtrl & CapsLock::AltTab
#IfWinExist ahk_group AltTabWindow ; Indicates that the alt-tab menu is present on the screen.
MButton::Send {Blind}{Escape} ; The * prefix allows it to fire whether or not Alt is held down.
#If
I've modified the example from the help page on this found here: https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail
This was mainly to remap the Windows+Tab key to the Alt+Tab key in this example.
It opens the task view and waits for the user to click, escape or enter. The example from the help page has the alt key getting stuck for me so I changed it to work a little better.
Please let me know if this works for you all.
; Override the Left Win key and tab to Alt + Tab
; Help found here:
; https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail
; #IfWinExist ahk_group AltTabWindow
#NoEnv
#SingleInstance force
SendMode Input
LWin & Tab::Send {Alt down}{tab} ; Asterisk is required in this case.
!LButton::
{
Click
Send {Alt up} ; Release the Alt key, which activates the selected window.
}
!Enter::
{
Send {Alt up} ; Release the Alt key, which activates the selected window.
}
~*Esc::
{
Send {Alt up} ; When the menu is cancelled, release the Alt key automatically.
;*Esc::Send {Esc}{Alt up} ; Without tilde (~), Escape would need to be sent.
}
I got ALT TAB to work with F1
By pressing F1 you can switch to the last window
While F1 is kept pressed you can move around with arrow keys or tab to select the window you need.
Code:
`F1::
Send, {ALT DOWN}{TAB}{TAB UP} ; If F1 is down it invokes the menu for switching windows.
KeyWait, F1 ; Show menu for switching windows by keeping ALT down until user physically releases F1.
Send, {ALT UP} ; If F1 is released release ALT key
return`
Documentation links
KeyWait
KeyList
I think this question was meant to be a simple request of: how to alt tab in Win10 using AHK, since win10 changed things up? -> I found the most simple solution as shown below... the code makes it necessary to keep the alt key down while the Win10 emu is open - then use the arrow keys an additional number of tabs (if you need three alt tabs, then it's "alt tab, then right 2", see?
macro key name::
{
Sleep 100
Send, ^c
Sleep 1000
Send, {alt down}{tab}
Sleep 400
Send, {right 2}{alt up}
Sleep 400
Send, ^v
Sleep 400
}
So just play with this snip in your code and you can jump passed the 'next' window(s) open.
Rossman

Typing on two windows at the same time

I want to be able to select both window 1 and window 2 and be able to type on both windows at the same time.
Whatever I type in window 1 will be typed in window 2 as well, for example: "hey everyone".
Being able to type on both windows at the same time, having both selected for typing. I have 2 notepad windows open. I want to type on the the first notepad window I opened, and what ever I type on window 1 will also be typed on window 2.
Sadly, I can't put up a image for you can see what I'm trying to say
WinGet, windowid, List, <Name of Window goes here>
#IfWinActive, <Name of Window goes here>
Space:: ; jump
KeyWait, Space, D
ControlSend,, {Space} , ahk_id %windowid1%
ControlSend,, {Space} , ahk_id %windowid2%
ControlSend,, {Space} , ahk_id %windowid3%
ControlSend,, {Space} , ahk_id %windowid4%
ControlSend,, {Space} , ahk_id %windowid5%
Return
I'm not sure if I even understand what you're trying to do... but have you tried using Input?
loop {
input, char, I L1 V M
ControlSend,, %char%, ahk_id %windowid1%
}
For example: Catching every single character sent in any %processID% window by Input and sending it to all remaining %processID% windows afterwards:
processID := "notepad.exe"
loop {
input, char, I L1 V M
winGet, active_win_ID, ID, A
winGet, active_win_exe, ProcessName, A
winGet, windowsToBeSentTo_IDs, List, ahk_exe %processID%
if active_win_exe != %processID%
continue
loop, %windowsToBeSentTo_IDs% {
_id := windowsToBeSentTo_IDs%a_index%
if _id != %active_win_ID%
controlSend,, %char%, ahk_id %_id%
}
}

How do I find a "Save As" window with AutoHotkey?

I'm trying to find out if there is a "Save As" box open with this WinExist script
if WinExist,Save As,Save As
MsgBox, is there
else
MsgBox, is not there
Your original code is almost correct:
The space between if and WinExist must be removed.
The second parameter of ifWinExist is WinTitle. If the Save As dialog does not contain the text "Save As", you must remove that second parameter. This is the case on my Windows 7 system with English (Australia) regional settings.
Working examples:
; ifWinExist command
ifWinExist, Save As
MsgBox, is there
else
MsgBox, is not there
; if(expression) and WinExist() function
if WinExist("Save As")
MsgBox, is there
else
MsgBox, is not there
You can also combine criteria:
ifWinExist, Save As ahk_class #32770
Assuming it is an OSs' window, I would do something like,
^F1:: ;press Control + F1 to serch the window.
if FindWindow()
msgbox Found
else
msgbox Not Found
Return
FindWindow(strPartOfTitle="Save", strClass="#32770") {
if (hWnd := WinExist("ahk_class " . strClass))
{
WinGetTitle, strWinTitle, ahk_id %hWnd%
if InStr(strWinTitle, strPartOfTitle)
return true
else
return false
}
}