WinActivate on specific criteria (include / exclude windows) - autohotkey

I'm stumped on how to best re-write my hotkey. What I want to do is simple. I have a script that switches to File Explorer IF it's currently open Else it will open a new file explorer window.
Here's the catch - It's hard to "identify" if the Explorer window is open. I used to just do this by using WinActivate, 00123456 because that's the name of the folder (eight-digit random number). But the problem with that is that these are support ticket numbers. And that number will also be within the title of my current Chrome session and sometimes other windows too (Firefox, Snagit, notepad, etc). So it will inadvertently switch to those other programs. So that led me to exclude in this way:
Ticket_Out := clipboard
xclude:= Ticket_Out . " | Salesforce - Google Chrome"
WinActivate, % Ticket_Out, , % xclude
That's handy except for it only excludes 1 window at a time. How could I implement something that excludes a second application? OR --- another thought, how could I just say: look for Explorer.exe with window title "00123456" ?

You can use ahk_exe to filter by the process name too:
WinActivate, ahk_exe explorer.exe %Ticket_Out%
Another way would be to look for the window class - since depending on the Windows version Explorer can use either CabinetWClass or ExploreWClass, you can use a group, as shown in the docs:
; Define the group: Windows Explorer windows
GroupAdd, Explorer, ahk_class ExploreWClass ; Unused on Vista and later
GroupAdd, Explorer, ahk_class CabinetWClass
; Activate any window matching the above criteria and the title
WinActivate, ahk_group Explorer %Ticket_Out%

Note on CherryDT's answer the two WinActivate examples are missing a comma between the WinTitle, and WinText parameters...
Should be....
WinActivate, ahk_exe explorer.exe, %Ticket_Out%
Second example (when using GroupAdd)
WinActivate, ahk_group Explorer, %Ticket_Out%
Caught me out for awhile.
(I would comment on the answer, but as a new user don't have 50 reputation)

Related

Autohotkey script to perform action when Microsoft Edge is active

I'm trying to write an script in order to perform actions when Microsoft Edge windows being active. I tried these scripts:
#IfWinActive Microsoft edge
~$l::
KeyWait,l,T0.25
if (ErrorLevel)
{
send,^l
sleep,100
send,{Delete}
}
return
But it doesn't recognize the windows. I tried different names such #IfWinActive, ahk_class Microsoft Edge and #IfWinActive Microsoft Edge.exe too. but neither of them worked.
Using Window Spy on a new Edge tab shows me this:
Of the three main options that you would use for an #IfWinActive statement (WinTitle, ahk_class, or ahk_exe), the ahk_exe would likely be the best option for creating a script that will always work in MS Edge, and only in MS Edge based on what the Windows Spy displays.
Based on this, I created this generic script to check if a hotkey was triggered in Edge
#IfWinActive, ahk_exe msedge.exe
^q::MsgBox Hotkey Triggered in Edge (msedge.exe)
#If
^q::MsgBox Hotkey Triggered in a different program
Incorporating your original script into this gives:
#IfWinActive, ahk_exe msedge.exe
~$l::
KeyWait,l,T0.25
if (ErrorLevel)
{
send,^l
sleep,100
send,{Delete}
}
return
For more info about #IfWinActive, see the docs.
Also relevant: Post on AHK forums on how to use the #IfWinActive with program names, ahk_exe, and ahk_class.

How to find OneNote windows 10 in AutoHotKey's if winexist?

I'm trying to build a script that allows me to open and use OneNote (The store app not OneNote 2016) with a hotkey, but I would like to use the same hotkey to switch the the app from another window. I have accomplished this with many other programs, but never with windows store app. Here is the code I'm trying to use.
#If WinExist("OneNote ahk_class ApplicationFrameWindow", "OneNote")
{
WinActivate,
}
Else
Run, C:\Users\ChrisCochrun\Documents\OneNote
#If
Unfortunately it's not detecting that the window exists and it'll only just launch a new instance of onenote. I've looked for answers, but I'm having trouble making it so that AHK actually sees the window already running and jumping to it.
Thanks very much for any help at all!
If the window's title doesn't start with "OneNote", you need to use
SetTitleMatchMode 2.
The #If-directive is only used for creating context-sensitive hotkeys and hotstrings.
F1:: ; or a hotkey of your choise
SetTitleMatchMode 2
If WinExist("OneNote ahk_class ApplicationFrameWindow", "OneNote")
WinActivate
Else
Run, C:\Users\ChrisCochrun\Documents\OneNote
return
rshfit::
SetTitleMatchMode 2
If WinExist("OneNote for Windows 10")
WinActivate return
;If I use rshift to activate Onenote for Windows 10

autohotkey to focus / open on google chromium

I have this autohotkey script that opens chrome if its not already on windows, and if it is, cycles through tabs within it. It also puts that chrome window on top of any other windows (e.g. excel docs, word docs, etc)
IfWinNotExist, ahk_class Chrome_WidgetWin_1
Run, chrome.exe
if WinActive("ahk_class Chrome_WidgetWin_1")
Send ^{tab}
else
WinActivate ahk_class Chrome_WidgetWin_1
Return
I can't seem to figure out how to get this to work for google chromium though. Both exe names are "chrome.exe", so I'm not sure what the run commands is if there's an overlap.
Also, I ran winSpy but still am not 100% sure what ahk_class name is. Below is the information from winSpy
I ended up settling for a different solution
After 2 days of tweaking and testing some workflows, this is what I settled with. I ran a combination of both phrase-express (any macro program works here) and autohotkey here, so I can have an extremely flexible layout
F1 → Binded to WIN+1 key
F2 → Binded to WIN+2 key
F3 → Binded to Win+4 key
F4 → Binded to Win+4 key
For F5 to F7 keys, I used autohotkey
F5::
IfWinNotExist, ahk_class Chrome_WidgetWin_1
Run, chrome.exe
GroupAdd, kjexplorers5, ahk_class Chrome_WidgetWin_1 ;You have to make a new group for each application, don't use the same one for all of them!
if WinActive("ahk_exe chrome.exe")
GroupActivate, kjexplorers5, r
else
WinActivate ahk_class Chrome_WidgetWin_1 ;you have to use WinActivatebottom if you didn't create a window group.
Return
F6::
IfWinNotExist, ahk_class ConsoleWindowClass
Run, cmd.exe
GroupAdd, kjexplorers6, ahk_class ConsoleWindowClass ;You have to make a new group for each application, don't use the same one for all of them!
if WinActive("ahk_exe cmd.exe")
GroupActivate, kjexplorers6, r
else
WinActivate ahk_class ConsoleWindowClass ;you have to use WinActivatebottom if you didn't create a window group.
Return
F7::
IfWinNotExist, ahk_class QWidget
Run, anki.exe
GroupAdd, kjexplorers7, ahk_class QWidget ;You have to make a new group for each application, don't use the same one for all of them!
if WinActive("ahk_exe anki.exe")
GroupActivate, kjexplorers7, r
else
WinActivate ahk_class QWidget ;you have to use WinActivatebottom if you didn't create a window group.
Return
F5 to F7 uses the same variant autohotkey, I just changed up the groupnames, the .exe files , and the ahk_class names.
This is how I organize the structure of my windows taskbar
So I press
F5 (3 times), and it pushes each chrome window I have up to the top of each of my 3 monitors.
F6 and I can quickly pop out whatever command prompts I have open, one command prompt for gulp commands, and one for git, independent of any IDE.
F7 twice to quickly add some new flashcards
I can restructure F1 F2 F3 F4 to whatever app I'm currently using. Anything that goes here I only keep one window per app at a time. Like I only run one firefox window (to watch tutorial youtube videos), only one running application of PHPstorm, etc.
Demonstration of F6 key in action (command prompt)

What is ahk_class? How can I use it for window matching?

The AutoHotkey Beginner Tutorial includes a sample script to demonstrate window specific hotkeys and hotstrings.
#IfWinActive ahk_class Notepad
::msg::You typed msg in Notepad
#IfWinActive
#IfWinActive untitled - Paint
::msg::You typed msg in MSPaint!
#IfWinActive
The second example is fairly self-explanatory: Check for a window named "untitled - Paint". It's the first example's use of ahk_class that has me confused.
I haven't been able to find any explanation of the variable in the AHK documentation. According to an AHK forum post, ahk_class is the name of a window as given by Windows Spy, the post didn't go into any more detail.
Would there be any difference between using ahk_class Notepad and Untitled - Notepad? Would the second example work if replaced with #IfWinActive ahk_class Paint?
What is ahk_class and how can I use it for window matching?
From https://autohotkey.com/docs/misc/WinTitle.htm#ahk_class
A window class is a set of attributes that the system uses as a template to create a window. In other words, the class name of the window identifies what type of window it is.
In other words you can use it to identify windows of the same type, if you open notepad the title will be Untitled - Notepad if you save it to temp.txt the title will become temp - Notepad. ahk_class on the other hand will always remain Notepad.
The second example will work if you replace it with #IfWinActive ahk_class MSPaintApp because that's the class of mspaint.
Usually you find ahk_class using Window Spy and then use it in your scripts. If you don't have Window Spy you can use the following hotkey:
#1::WinGetClass, Clipboard, A ; Will copy the ahk_class of the Active Window to clipboard
After you found it you can use it in any place you can use window title for example instead of writing WinActivate, Untitled - Notepad you can write WinActivate, ahk_class Notepad.
Check this article. Ahk_class is the class that is given to you when you use the WindowSpy tool. This tool should be in the same folder as your AutoHotkey executable.

Solution to a "dynamic"/"unstable" ahk_class name?

In AutoHotKey, ahk_class NAME has been a good identifier for programs. Typically, I now use it in the following two ways:
Simply calling the most recently called active instance:
; Pressing Win+Shift+X will bring up (or switch to) the Windows Journal
Application
#+x::
IfWinExist ahk_class JournalApp
WinActivate ahk_class JournalApp
else
Run C:\Program Files\Windows Journal\Journal.exe
return
Grouping windows with the same ahk_class and looping through them:
GroupAdd, FIRE, ahk_class MozillaWindowClass
; Pressing Win+3 will go through all open Firefox windows one by one.
#3::
IfWinExist ahk_class MozillaWindowClass
GroupActivate, FIRE, r
else
Run firefox
return
However, there are certain programs that do not have a stable ahk_class name. For example, the ArcMap.exe should
display the following information in the "Windows Spy" window of AHK:
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Untitled - ArcMap
ahk_class Afx:012F0000:b:00010003:00000006:001C0BB0
Any idea how could I refer to this application that has dynamic ahk_class?
I am aware that there is also something called ahk_exe. However, I did not find it compatible with the method GroupAdd,
GroupActivate or IfWinExist.
You definitely are on the right track trying to incorporate ahk_exe. GroupAdd (and every other command receiving a WinTitle parameter at that) is compatible with ahk_exe in AHK_L; this sample code works perfectly on my machine:
GroupAdd, notepad, ahk_exe notepad.exe
Run, notepad.exe
WinWaitActive, ahk_group notepad
Sleep, 1000
WinClose, ahk_group notepad
I suspect you're not using the most recent version of AHK. Get it here and have another try.