AutoHotkey. How to make a script trigger every time certain window is active? - triggers

good people! I'm struggling with a question here.
I have the following script:
#If WinActive("ahk_class Chrome_WidgetWin_0") || WinActive("ahk_class Chrome_WidgetWin_1") || WinActive("ahk_class MozillaWindowClass") || WinActive("ahk_class IEFrame")
WinWaitActive, my_window_name_here - Opera, , 2
Sleep 1000
#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir%
#SingleInstance force
Send some-text
Send {TAB}
Send some-text
Send {TAB}
Send some-text
return
It triggers when a certain window in a browser gets active. The problem is, it does it only ONCE. But I need it to trigger EVERY TIME that window/tab gets active. Is that somehow possible?

A quick googling reveals http://www.autohotkey.com/board/topic/85660-run-scripts-when-window-opensbecomes-active/ . Its essense is:
You have to check in a continuous loop:
There is no way to >run< a script when a program opens/is active.
The reason is that there is "nobody home" to say 'wake up'.
Instead, you can have a script that is running all the time, and one of its functions is to watch for your target window(s) and then call a subroutine to "do whatever".
The "WinTrigger" script linked to further in the thread uses #Persistent and SetTimer which is essentially the same approach but is neater.

Related

ctrl+s not being sent in autohotkey script

i.e. hitting ctrl+s should activate ctrl+s...the below does not work. Neither does the simpler
SendInput, ^s. Goal is to have ctrl+s save current doc and then load another via more code, the saving part never works tho. The bad code, depending on where i put sleep or no sleep, either returns s or shift s (in 1 code editor anyways) or nothing. I basically want a hotkey that mimics itself.
F4::ExitApp
<^s::
send, {lctrl down}
SLEEP 300
SEND s
SLEEP 300
SEND {lctrl up}
return
I would think that the issue your program is running into is that having the ^s send another ^s inside of itself is creating an infinite recursive loop in which nothing is ever able to run past the place you invoke ^s. To prevent this, we can use the $ modifier as so:
$<^s::
SendInput ^s
return
From the relevant section of the Modifier section of the docs:
This is usually only necessary if the script uses the Send command to
send the keys that comprise the hotkey itself, which might otherwise
cause it to trigger itself. The $ prefix forces the keyboard hook to
be used to implement this hotkey, which as a side-effect prevents the
Send command from triggering it. The $ prefix is equivalent to having
specified #UseHook somewhere above the definition of this hotkey.
Edit: it seems to work fine for me even if I remove the $ modifier. Testing the following code shows me there appears to be no problems regarding code execution before, after, or during the SendInput statement.
<^s::
MsgBox no
SendInput, ^s
MsgBox yes
return
Maybe check your version or installation of AHK?

Auto Maximize skype live conversation window with Autohotkey

Here is what I'm trying to do and would greatly appreciate any help here.
I am trying to automate maximizing the live conversation window in skype with an Autohotkey script. I'm trying to make it so I can call into a remote machine using skype and have it auto answer (this is native in skype)...once I have a live conversation window I would like to maximize the live conversation window to fill the screen.
I've given this a shot but somehow don't think that I have the correct ahk_class for the live conversation window but there may be something else I am missing. I've placed a the code I've tried using below...any help would be great.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
IfWinExist, TLiveConversationWindow ;
{
WinActivate
WinMaximize
send !{Enter};When using skype normally this Alt+Enter will maximize the window
return
}
I also tried to use this code to determine the proper class for the live conversation window...but has not helped as of yet.
Alt & Enter::
IfWinExist, TLiveConversationWindow
WinActivate
ControlFocus, ClassNN TLiveConversation1
ControlClick, ClassNN TLiveConversation1, , , , 2,
MouseClick, left, , , 2
send !{Enter}
; now we get the ID & CLASS
WinGet, Active_Window_ID, ID, A
WinGetClass, Active_Window_Class, A
MsgBox, The active window's class is "%Active_Window_class%" and ID is %Active_Window_ID%
Was able to get a simplified version of the code to work with a hotkey to initiate but have not been able to get the WinWait function to work as per #Schneyer.
Functioning Code activated by hotkey
#NoEnv
#Warn
#Persistent
SendMode Input
SetWorkingDir %A_ScriptDir%
; Skype Maximizer initiating functions
^!p::
;WinWait ahk_class TLiveConversation1
;WinWait ahk_class TConversationForm
;WinWait ahk_class TLiveConversationWindow
;WinMaximize ahk_class TLiveConversation1
;functioning code
;Activate tSkMainForm.
WinActivate ahk_class tSkMainForm
;Send Alt Enter Input to maximize.
SendInput !{Enter}
;TLiveConversationWindow Always On Top
WinSet, AlwaysOnTop,,ahk_class TLiveConversationWindow
;Minimize main form
WinMinimize ahk_class tSkMainForm
Return
When swapping the ^!p:: with any of the WinWait Functions nothing seems to happen. The WinWait seems like it should be the proper method, any thoughts on why it won't work?
Problems
#persistent lets the script run, but your code still gets executed only once when you start the script. After that it stays active but does nothing.
Use a WinWait to wait for the window to appear (wrap it in a Loop if you want it to run more than once).
Use ahk_class to to search for a window class instead of the window title
Working code
You can use the Window Spy tool which is included in AHK. Use the tray icon menu of a running AHK script to start it.
I use TConversationForm in the code, but it works with every window class.
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
WinWait ahk_class TConversationForm
WinActivate
WinMaximize
Scanning through AHK forums I was able to find a post that led me to the answer for this little problem. Check the below link for further information.
https://autohotkey.com/board/topic/96491-detect-when-a-classnn-window-exists/
The problem with using ahk_class to identify when a skype call was active is that the "active call window" identified as classNN TLiveConversation1 in the inspector was actually a Control within the window of ahk_class tSkMainForm rather than a Window. This made the WinWait function ineffective at identifying it when the call initiated.
In order to identify the Control it is necessary to loop through the controls (using WinGet) in ahk_class tSkMainForm until the TLiveConversation exists and then kick off any subroutines needed. For me that was to maximize the live conversation window.
You'll see the code to do this starting with the WinGet function. All of this is wrapped in a while loop so that it will run persistently allowing it to be called over and over. In essence this code will do the following:
Create a list (SkypeControlList) of controls existing in ahk_class tSkMainForm.
Continually Loop Through SkypeControlList
When a new Live Conversation is initiated a control TLiveConversation1 will exist
Once TLiveConversation1 esists run necessary code
#NoEnv
#Warn
#Persistent
SendMode Event
SetWorkingDir %A_ScriptDir%
DetectHiddenWindows, on
stop = 0
Loop
{
While stop = 0
{
WinGet, SkypeControlList, ControlList, ahk_class tSkMainForm
Loop, Parse,SkypeControlList, `n
{
;Loop to search for control TLiveConversation1
if (A_LoopField = "TLiveConversation1")
{
;Deactivate active screensaver
PostMessage, 0x0112, 0x0F060, 0,, A
;RegWrite REG_SZ, HKEY_CURRENT_USER, Control Panel\Desktop, ScreenSaveActive, 0
;SetKeyDelay, 500
Send {Esc}
;Activate tSkMainForm.
WinActivate ahk_class tSkMainForm
;Send Alt Enter Input to maximize.
Send !{Enter}
;TLiveConversationWindow Always On Top
WinSet, AlwaysOnTop,,ahk_class TLiveConversationWindow
stop = 1
sleep, 100
}
}
}
IfWinExist ahk_class TLiveConversationWindow
{
stop = 1
sleep, 1000
}
IfWinNotExist ahk_class TLiveConversationWindow
{
;Minimize all windows by win+D show desktop
send #d
sleep,1000
; is that call quality feedback window up? kill it.
SetTitleMatchMode, Regex
WinClose, ^Skype.*Call quality feedback$
stop = 0
}
sleep 1000
}
Return
The above code works great as long as there is no screen saver active on the machine being called. If there is an active screen saver the call will answer but the screen saver will not go away. You can see remnants of code trying to wake the computer from a screen saver (This does not currently work).
I've also added a few bits of code to clean up the experience such as removing the call quality popup window that skype throws up after a call as well as clearing the desktop after the call ends.
Thanks to #Blauhirn and #Schneyer for their input in trying to get this solved.

autohotkey Restricting a script to only run a designated program

I use Teraterm for my terminal program. My issue is I cannot make my script run only when I am in the teraterm program. Is there a way to make it pop into teraterm if I am in a different app?
The script works great please share it to anyone who uses teraterm.
We always use the same server ip that shows up in the window title it contains 10.155.3.8. That text is always in the title.
How do I make it execute only in teraterm. I know this is an incredibly simple question but I have spend days looking around any help would be greatly appreciated.
If you have any basic tutorial sites I would greatly appreciate it.
I am a programming neophyte.
::ttwa:: ; change teraterm window name to current device or state.
SetTitleMatchMode, 2 ;// allow partial window title matches
#IfWinActive, 156.99.121.173, 156.99.121.173
send !e
sleep 10
send s
send {enter 100}
sleep 100
Send {click 3}
send !s
sleep 10
Send w
sleep 10
send %clipboard%
sleep 100
;send {backspace}
sleep 10
send {enter}
send !e s {enter}
send {enter 10}
Clipboard :=
return
There are several methods:
assign a hotkey which you would press to initiate the script:
^+F1::
.... send stuff
....
return
wait for the teraterm window to appear (WinWait) or become active (WinWaitActive):
winwait, teraterm ; change to the actual window qualifications
.... send stuff
....
return
run teraterm from your script, so you'll run the script icon instead of running teraterm directly:
run teraterm ; change to the actual path
winwait, teraterm ; change to the actual window qualifications
.... send stuff
....
return
Well. To me it looks like you have the answer already in your script.
From the Docs here: #IfWinActive
Creates context-sensitive hotkeys and hotstrings. Such hotkeys perform
a different action (or none at all) depending on the type of window
that is active or exists.
You simply have your script executing that requirement, out of order in which in needs to be.
SetTitleMatchMode, 2
#ifWinActive, 156.99.121.173, 156.99.121.173 ;Assuming this is correct
::ttwa:: ; change teraterm window name to current device or state.
send !e
sleep 10
send s
send {enter 100}
sleep 100
Send {click 3}
send !s
sleep 10
Send w
sleep 10
send %clipboard%
sleep 100
;send {backspace}
sleep 10
send {enter}
send !e s {enter}
send {enter 10}
Clipboard :=
return
As for recommendations on furthering your understanding of AutoHotkey, I strongly suggest starting with the official Tutorial.

Send, SendInput, SendEvent functions entering single key multiple times

I have created a script in AutoHotKey.I have to Enter the entire file path to be opened in the
file browser.i have used send function but it passes the same keys multiple times. I tried using the function SetKeyDelay but still it enters the keys multiple times. I tried other alternatives as sendInput and SendEvent but still it didnt work.
Even if i terminate the script in between and if the control switches to some Input box or Editor, it starts entering the values into that area. Send function keeps on entering the keys even after the script execution is terminated.
Script:
;Open Adobe Acrobat 8.
run Acrobat.exe sleep, 1000
WinWait, Adobe Acrobat Professional,
Sleep, 1000
;Open Compare Documents Window
send, {ALT}A
Sleep, 1000
send, U
WinWait, Compare Documents,
;Enter File Path
IfWinNotActive, Compare Documents,
WinActivate, Compare Documents,
WinWaitActive, Compare Documents,
Sleep, 1000
send, !H
WinWaitActive, Open,,1000
sleep, 1000
SendEvent, "D:\Sample\a.pdf"
It Enters text something like this
CCCC:::::\\DDDDiiiiii
Things to try:
Update AutoHotkey to the latest version.
Don't use SendEvent. It allows other input to interrupt the text as it is being entered. Use SendInput instead. It is recommended to put the following at the top of all your scripts:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SendMode Input changes Send to use SendInput instead of SendEvent. It buffers the input and sends it all at once instead of letting other keyboard/scripts interrupt the text. See: Send for more information.
Restart your computer and make sure that there are no other AutoHotkey programs running, or that there isn't multiple instances of your Autohotkey script running. This is especially a problem when testing, as you might have launched the same script multiple times, and if the thread hasn't been properly terminated, you may have multiple instances there waiting for the Open window to be activated. Once the Open window has been activated, then you may have 4+ dormant scripts all trying to type at the same time, which would give you the output that you are describing.

Writing script for Autohotkey for invoking 'Shift+F5' keystrokes

I have to run few applications like browser and ticketing tool. But, I need to continuously press shift+F5 keystrokes for reloading the pages of my browser and ticketing tool.
Can I use any script through autohotkey for invoking 'Shift+F5' keys repeatedly for every 20 seconds regular interval?
Can anybody please help me with writing the script for autohotkey for invoking 'Shift+F5' keystrokes?
I assume that you want to be able to turn this feature on/off
#SingleInstance Force
#installKeybdHook
#Persistent
#F5:: ; [Win]+[F5] to start timer
SetTimer, RefreshPage, 20000
TrayTip, Refresh, Started, 1
ToolTip, Refresh Active
Return
+#F5:: ; [Shift]+[Win]+[F5] to stop timer
SetTimer, RefreshPage, Off
TrayTip, Refresh, Stopped, 1
ToolTip
Return
RefreshPage:
Send, +{F5}
Return