Auto Hotkey WinExist does not find open cmd - autohotkey

I am testing the following piece of code with auto hot key. I want to open a cmd console if one is has not already been opened. Each time i run it a new console is created, ignoring the previous one that have been already opened.
#z::Run https://autohotkey.com ; Win+Z
^!n:: ; Ctrl+Alt+N
if WinExist("Untitled - cmd"){
WinActivate
MsgBox GUI "MyGui" already exists.
}
else{
Run cmd
MsgBox GUI "MyGui" does not exist.
}
return
Another question that i have is that when i use the following code, everything works as expected, but when i change untitled - Notepad to untitled - notepad the same thing as in the first excerpt happens, a notepad is created although a previous one is still active. Any advises on why this happen would be really helpful, thank you.
#z::Run https://autohotkey.com ; Win+Z
^!n:: ; Ctrl+Alt+N
if WinExist("Untitled - Notepad"){
WinActivate
MsgBox GUI "MyGui" already exists.
}
else{
Run Notepad
MsgBox GUI "MyGui" does not exist.
}
return

Place this at the top of your script:
SetTitleMatchMode 2
Documentation
This allows you to use commands like WinExist with a partial window name match.

Related

(Autohotkey) "Elevate" or "UAC" thingy. Before reinstalling new OS Windows, my script work perfectly, Now it can't process Excel.COM

i need some help..
does anyone know about "elevate" thingy in autohotkey?
So i'm just messed up because now my script can't run properly after reinstalling new windows. Now my script cannot process COM / Macros commands for Excel automation..
Before, it was able to activate sheet, activate window, etc.
But now it can't.
Can somebody help me with this? Thank you very much
Here is a script that will request to elevate itself if it is not run elevated:
MsgBox, % "I am" (A_IsAdmin ? "" : " not") " Admin."
Gosub, RunAsAdmin
; more code
Return
RunAsAdmin: ; run as administrator
If Not A_IsAdmin {
Run, *RunAs %A_ScriptFullPath% ; Requires v1.0.92.01+
ExitApp
}
Return
Put the first block of code in the auto-execute section at the top of your script, and the second block somewhere near the bottom, after your hotkeys and other code.
Source

How to determine when the Select File Dialog is active?

I'm trying to determine when the file selection dialog is active, but I can't.
SetTitleMatchMode,1
FileSelectFile, SelectedFile
Winwaitactive,Select File -
Msgbox,File Select Dialog is active ; This is never fired
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
MsgBox, The user selected the following:`n%SelectedFile%
Could you please tell me what I'm doing wrong?
On that thread, code execution stops on the FileSelectFile line until a file has been selected. So your code will never get past the WinWaitActive command, because by the time that command runs, the window you're trying to match isn't open anymore.
And by the looks of things, you can't interrupt that thread without breaking the FileSelectFile command's functionality.
So the exact thing you're trying to do isn't possible without true multithreading.
But maybe you'd be fine to just make a simple assumption like this:
MsgBox, % "File select will begin after you click OK"
FileSelectFile, output
MsgBox, % "File select ended"

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

Automatically reload AutoHotkey script when modified

When testing AutoHotkey scripts, I sometimes forget to reload my scripts after making changes. This leads to me accidentally testing old, outdated versions of my scripts.
Instead of manually reloading the script, I would like to have scripts automatically reload if they have been modified.
How can I make AutoHotkey reload the current script any time a .ahk file is modified?
Somewhere near start of the script, in the auto-execute section
#SingleInstance force
FileGetTime ScriptStartModTime, %A_ScriptFullPath%
SetTimer CheckScriptUpdate, 100, 0x7FFFFFFF ; 100 ms, highest priority
Anywhere in the script (usually somewhere at the bottom):
CheckScriptUpdate() {
global ScriptStartModTime
FileGetTime curModTime, %A_ScriptFullPath%
If (curModTime == ScriptStartModTime)
return
SetTimer CheckScriptUpdate, Off
Loop
{
reload
Sleep 300 ; ms
MsgBox 0x2, %A_ScriptName%, Reload failed. ; 0x2 = Abort/Retry/Ignore
IfMsgBox Abort
ExitApp
IfMsgBox Ignore
break
} ; loops reload on "Retry"
}
This is how I've done it:
#If WinActive("AHK.ahk - Notepad") or WinActive("*AHK.ahk - Notepad")
~^s::
Reload
Return
#If
Checks if the current window is the script that I want autoreloaded whenever I hit Ctrl-S.
The ~ means the default action of Ctrl-S (saving the file) is preserved, and then we simply reload it.
I'm still new at AHK but here's what I've come with.
If you're using Notepad++ to edit AHKS this will run any ahk that's open and currently in focus in Notepad++ on saving with Ctrl-S and won't effect any other file types.
This script only has to be in one running ahk to work on all ahks being modified in Notepad++.
This can be used on multiple text editor programs too like win Notepad just get rid of the ++ associated with Notepad++
~^s:: ; Saves and Runs ANY AHK open in Notepad++
Sleep 150
WinGetTitle, Title, A
Needle := "ahk - Notepad++"
IfInString, Title, %Needle%
{
StringReplace, xxx, Title,- Notepad++, , All
run %xxx%
return
}
else
return

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