How to continuously run an autohotkey script? - autohotkey

I am trying to make a basic shortcut for windowsHome + Shift + w. If word is open, I would like to set Word as the active window and maximize Word; else, if word isn't open, I would like to open a specified word document. This script works fine running once but can't run multiple times without restarting the script. I tried adding #Persistent and a never-ending while loop but this still doesn't work. What I am doing wrong? Thank you for your help in advance! I will definitely rate you up if you help me out.
#Persistent
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
#+w::
while 1
{
IfWinExist *.docx - Microsoft Word
{
WinActivate
WinMaximize
}
else
{
Run, C:\Users\myHome\Desktop\311.docx
}
}
return

This is working for me:
SendMode Input
SetWorkingDir %A_ScriptDir%
#+w::
IfWinExist ahk_class OpusApp
{
WinActivate ahk_class OpusApp
WinMaximize ahk_class OpusApp
}
else
{
Run, C:\Users\myHome\Desktop\311.docx
WinWait ahk_class OpusApp
WinActivate ahk_class OpusApp
}
return
I highly recommend using the ahk_class for this purpose.
Also, you don't need the loop, otherwise it will consistently loop after the keys are pressed, keeping the window maximized. In this example, the actions happen after the keys are pressed, but only happen once.
Also, the ahk_class OpusApp is the correct class for Microsoft Word 2010.
Let me know if you need any more help.

Related

Issues sending key events to browser window autohotkey

I am trying to open up a webpage and save it as a complete file. This should be a relatively easy task for autohotkey. For some reason my script is not sending key events to the window. Here's the script:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn All, StdOut ; 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.
Run, www.google.com
WinWaitActive firefox.exe
sleep, 4000
ControlSend, ahk_class MozillaWindowClass, ^s
What would be a better approach to this problem?
Here is my solution:
Run, www.google.com
WinWaitActive ahk_exe firefox.exe
sleep, 4000
Send ^s
There were two main things that were changed for this to work
Include ahk_exe before the name of the exe in the WinWaitActive conditional
Removed the ControlSend and replaced it with a regular Send, since we are already waiting for the Window to be activated
Hope this helps, feel free to ask for any more clarification or help if you need it
ControlSend, ahk_parent, ^s, ahk_class MozillaWindowClass

User Accounts window doesn't allow any hotkey

I want to create a hotkey when ahk_class #32770 windows are active, in my case, the User Accounts window opened by typing netplwiz in the Run. But, it seems that no hotkey works.
Here's what I did:
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 2 ; A window's title can contain WinTitle anywhere inside it to be a match.
#ifwinactive
#e::Run,%A_AppData%\Microsoft\Windows\Recent
;this doesn't work for ahk_class #32770, so I make the following workaround:
#if winactive("ahk_class #32770") or winactive("ahk_exe explorer.exe")
#e::Run,%A_AppData%\Microsoft\Windows\Recent
;but neither does this work. So I made the following test:
^p::msgbox,hi
;and it doesn't work.
Did I do something wrong, or any workaround?
It should work. End/Close the context with
#if or #ifwinactive as appropriate, if you have not.
Like in
#ifwinactive ahk_class #32770
#e::Run,%A_AppData%\Microsoft\Windows\Recent
#ifwinactive
#if winactive("ahk_class #32770")
#e::Run,%A_AppData%\Microsoft\Windows\Recent
#if

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

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.

executing a script when at a specific classNN

How shouuld I tweak the following script so that the hotkey is activated not at ANY moment where I'm at the class shown (that is Outlook) but at a specific sub window ( the preview pane of the Inbox box (whose classNN is _WwG1 )) ?
#IfWinActive ahk_class rctrl_renwnd32
+!m::
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You don't seem to be in context.
return
#IfWinActive
Make the hotkey look for active controls once it is activated. This way you can use the same hotkey for multiple commands, each command depending on the control. You can do this with several if/else statements to test for the subcontrols.
The hotkey only works in Outlook.
Each control has its own command
Each command is limited to that particular control
#ifwinactive, ahk_exe outlook.exe
{
+!m::
controlgetfocus, thiscontrol
if(thiscontrol = "_Wwg1"){
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You seem to focused on %thiscontrol%
}else if(thiscontrol = "_Wsg2){
msgbox, you've discovered the second control!
}
return
}