Autohotkey winclose just wont work - autohotkey

I just want to have a simple function where Ctrl+F8 kills sublime text 3.
This is what i have so far, which doesn't work.
^F8::
WinClose, "C:\Users\Eduardo\My Programs\Execs\Sublime Text 3\sublime_text.exe"
return
I've also tried:
^F8::
Winclose, Sublime Text 3
return
and:
^F8::
WinClose, Sublime Text
return
Please help me with this. Ive looked at other examples and can't find what I am doing wrong.

WinClose command works with the window, not with the process. If you want to use WinClose command then you have to specify WinTitle and/or WinText of window you want to close. Also it is convenient to use SetTitleMatchMode command with WinClose. SetTitleMatchMode command sets the matching behavior of the WinTitle parameter of several commands, including WinClose. Particularly SetTitleMatchMode, 2 allows us to match not exact WinTitle for WinClose command. In that case a WinTitle can contain any part of actual WinTitle to be matched. Here is working example for notepad:
SetTitleMatchMode, 2
^F8::
WinClose, Notepad
return
To work with process you can use Process command. Particularly, Process, close, notepad.exe will close notepad.exe process. Here is code:
^F8::
Process, close, notepad.exe
return
If my examples are not working for you:
Use AutoHotkey and its documentation from http://ahkscript.org/ . You should always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!
Run it with administrator privileges.
Make sure you saved script before running it.

Related

How to start a program if not already started, put on focus if already started?

I would like to make a ahk script to start apps if they are not currently started and maximize them if they are. Is it possible using AHK ?
CapsLock & w::
Run firefox.exe
Return
Something like this but make it so that if I press CapsLock & w and then minimize firefox, pressing CapsLock & w would bring it back maximized / in focus. Any ideas? Thanks!
Sure, this is very easy and doable with AHK.
Here's a very easy and straight forward example
CapsLock & w::
if (WinExist("ahk_exe notepad.exe"))
{
WinActivate, ahk_exe notepad.exe
WinMaximize, ahk_exe notepad.exe
}
else
Run, notepad.exe
return
ahk_exe (docs) is used to refer to windows by their process. It's very convenient.
The code that you are looking for is somewhat similar to the example they give in the docs for WinActivate
So modifying that example for your purpose, and adding conditionals would give you:
CapsLock & w::
if WinExist("ahk_exe firefox.exe")
if WinActive("ahk_exe firefox.exe")
WinMinimize
else
WinActivate
else
Run firefox.exe
Return
Take note that this script will currently only minimize a Firefox window if it is the currently active window. If you need it to minimize a Firefox in the background, the script would potentially be a bit more complex since you could possibly have multiple Firefox windows open, and you would need to provide conditions and logic to handle cases like those. However, if you need this functionality, describe what behavior you would like to occur if this condition occurs, and I can work on it.

WinActivate does not work as expected. Re-activating focus to the starting window

I am having some serious struggles fully grasping the control on activating windows and forcing their focus and foremost position.
In order to debug a larger script I made a separate script to test the use of WinActivate and again I am observing frustrating behaviour as it either all together ignores the title I have defined or is failing in some other way. In the smaller test script I am simply requesting that the window in which the hotkey was triggered be set as active after another action, specifically an input box
Below is the simple code for testing:
F10::
SetTitleMatchMode, 1
DetectHiddenWindows, Off
WinGetTitle, startTitle, A
msgbox % "Start Title = <" . startTitle . ">"
;WinActivate, startTitle
inputbox, mode, Test box, Testing,,260,160
sleep 500
WinActivate, startTitle
Return
This code does not properly activate the starting window. For example I execute the hotkey in an empty notepad window and upon submitting blank into the input box the focus becomes notepad++ on my second monitor. The second time I press the hotkey from within notepad (or another application) notepad does not lose focus. In a third execution I begin from notepad again and after the input box appears I switch the focus to another window. I again submit blank to the inputbox but that new window remains the focus and notepad is not activated or brought to the foremost position.
Can someone please explain to me what is going on with WinActivate?
I was having similar frustration with unexpected results making a windows script host file and I think I must be missing some fundamental detail in windows.
You are trying to activate a window that start with the literal text "startTitle".
You forgot(?) to either enter expression syntax with % or use the legacy way of referring to a variable %startTitle% (please don't use legacy).
Extra stuff:
You shouldn't specify SetTitleMatchMode and DetectHiddenWindows inside your hotkey statement. There is no need (unless there actually is) to set those every time you hit the hotkey. Just specify them at the top of your script once.
Both of them are useless for you though, below I'll show why. Also DetectHiddenWindows is already off by default.
WinGetTitle is not good to use for this. What you actually want to do is get the hwnd of the window you wish by using e.g. WinExist().
And then refer to the window by its hwnd. Much better than working with window titles, and impossible to match the wrong window as well. To refer to a window by its hwnd, you specify ahk_id followed by the hwnd on a WinTitle parameter.
And lastly, the concatenation operator . is redundant. Of course you may prefer to use it, but in case you didn't know, it can just be left out.
Here's your revised code:
F10::
_HWND := WinExist("A")
MsgBox, % "Start hwnd = <" _HWND ">"
InputBox, mode, Test box, Testing,,260,160
Sleep, 500
WinActivate, % "ahk_id " _HWND
Return

Autohotkey autocomplete limit to certain windows

I am trying to use the Autohotkey autocomplete script here: http://www.autohotkey.com/board/topic/60998-autocomplete-updated-26713/ but limit it to certain windows. Enclosing the entire script within #IfWinActive does not seem to work. I really like this completion script but would not want it showing up all over the place. Is there a way to limit the autocompletion to specific windows?
A quick study of the source code showed that every keypress results in a call of the Suggest subroutine. This seems to be a good place to check for the active window. I've implemented a minimal change in the source, you can check it out here.
First, you have to define which windows you want to exlude from the functionality, I achieved this by defining a window group:
GroupAdd, excludedWins, ahk_class CabinetWClass ; windows explorer
GroupAdd, excludedWins, ahk_class DV2ControlHost ; start menu search bar
GroupAdd, excludedWins, ahk_class ConsoleWindowClass ; console
Please note that I'm using Windows 7; maybe, the windows have other identifiers in other versions.
Second, you need to tell the Suggest subroutine to ignore these windows:
Suggest:
IfWinActive, ahk_group excludedWins
{
return
}
It seems to work, but I only tested very superficially and didn't investigate the source code dependencies. Let me know how it works for you.

script opens document but not with hotkey

I'm creating a script to open and run with a program that has no help files but comes with a huge PDF manual. There is no way to open it from the program, so I have set up a command to open it. The command opens the PDF when run on its own, but when I assign a hotkey, it does not work. What very basic information am I missing?
thanks,
Ellen
SetTitleMatchMode, 2
runwait C:\Program Files\FontLab\TypeTool3\TTool3.exe, , max
IfWinExist TypeTool 3
Return
ExitApp
Return
#ifWinActive, TypeTool 3
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive
Do you have a Return before your #IfWinActive statement? If not then during startup the script will run every line until it hits the first Return, which seems to be all the way to the end for you. So also place a return after your Run, C:..... command
And oh.. Are you sure about that weird long ahk_class? It could be correct but it looks strange and if this is just a littlebit off, your hotkey would never work, so try it first with the #IfWinActive line commented out.
SetTitleMatchMode, 2
;All the stuff you want to run at startup....
Return
#ifWinActive, (part of) the window name here e.g. Excel
$wheeldown::wheelup
$wheelup::wheeldown
F1::
Run, C:\Documents and Settings\Ellen\My Documents\TypeTool3WinMan.pdf
Return
#ifWinActive

How can I make AutoHotkeys's functions stop working as soon as the .exe is closed?

I'm testing AutoHotkeys as a way to block user's usage of Ctrl, Alt and Windows Key while an application is running. To do this, I compiled the code:
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
into an .exe using the compiler that comes with AutoHotkeys.
My problem is that normally when I close the .exe file (either by code using TerminateProcess(,) or manually) the keys are not released immediately. The Windows Key, for example, may take something like 10 seconds to be finely "unlocked" and become able to be used again, and for me this is unacceptable.
So I got two questions:
Is there a way to fix this problem? How can I make the keys to be released as soon as the .exe is closed?
Would there be any improvement if I tryed to get the same functionality by code? Or if I create the hooks by myself I would get the same problem I'm having with AutoHotkeys?
Thanks,
Momergil
AutoHotkey has a built-in command ExitApp for terminating your scripts.
This example makes Esc your termination hotkey:
Esc::ExitApp
It seems like the delay you are experiencing might be related to how long it's taking the process to close.
You could try making the hotkeys conditional with the #If command*
(i.e. they are only blocked when Flag = 1).
Then you can have the script quickly change the context just before ExitApp by using OnExit. The OnExit subroutine is called when the script exits by any means (except when it is killed by something like "End Task"). You can call a subroutine with a hotkey by using the GoSub command.
Flag := 1
OnExit, myExit
Esc::GoSub, myExit
#If Flag
LAlt::return
LCtrl::return
x::return
#If
myExit:
Flag := 0
Exitapp
* The #If command requires Autohotkey_L.
The other option that will be more verbose, but work for AHK basic, is the hotkey command.
Another option is to have AutoHotkey run the target application, and upon application exit, AutoHotkey exits as well. Here's an example with Notepad. When the user closes Notepad, the script gracefully exits.
RunWait, Notepad.exe
ExitApp ; Run after Notepad.exe closes
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
I would use winactive to disable these keys. In this example the modyfier keys are disabled for "Evernote". As soon as you switch to another program the keys are restored and when you switch back to Evernote the modifier keys are disabled again.
SetTitleMatchMode, 2 ; Find the string Evernote anywhere in the windows title
#ifWinActive Evernote
LAlt::return
RAlt::return
LControl::return
RControl::return
RWin::Return
LWin::Return
#ifWinActive