I feel like some of these terms need to be cleared up a bit:
ahk_exe is the name of the executable file (i.e. .exe) that the process is associated with. Normally, just using the run command generically with the name of this file would do nothing, as AHK has no idea which folder this .exe is in. This just happened to work with the Windows Calculator app [calc.exe], since that was a program that Windows allows you to run anywhere you want. The solution is to tell ahk which folder the .exe is in so that it can run it.
If you want to test whether or not a specific program parameter for the run command will work properly, you can open up a command prompt window and try the command, replacing AHK's run with the Commandline's start [ex: run calc.exe becomes start calc.exe].
Additionally, ahk_pid is not a constant number that persistently associates with a program. In fact, every time you run a program, even if it is the same exact program, Windows will assign a new PID to it every time. Futhermore, because a new one is assigned after the program is launched, we cannot use it to initially launch a program.
Important!: This code immediately below is unsafe and can corrupt your installation of Teams. Please see Update 1 for more info and a better alternative.
Here is a solution that launches the Teams app (Teams.exe) from the default installation location in C:\Users\[YOUR USER NAME HERE]\appdata\local\Microsoft\Teams\current\Teams.exe
run "%LOCALAPPDATA%\Microsoft\Teams\current\Teams.exe"
Hope this was helpful. If you have any additional questions, please lmk
Update 1:
While looking back at my original solution, I found out that directly running the Teams.exe file was not a good solution, as it has the potential to corrupt the installation of Microsoft Teams (which happened to me). As such, in order to run Teams the way the developers intended it to, I am opting to run the Program's shortcut file (i.e. the "Microsoft Teams.lnk" file in the %AppData%\Microsoft\Windows\Start Menu\Programs\ folder). Upon inspecting this .lnk file, I found that it contained additional logic and calls to MS Teams' Update.exe file that help prevent these issues from occurring. So, the new Run code to start MS Teams would be
run "%AppData%\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk"
With that out of the way, we can now integrate this new run command back into OP's program. From what I could understand of the original code, these were the objectives OP was trying to code (if any of these are incorrect, please lmk and I can update):
- If Teams is running and the currently active window, minimize it.
- If Teams is running, but not the active window, make it the active window
- If Teams is not running, run it and make the active window once it opens.
So, we can effectively boil down the logic to be something like this (in pseudocode):
If Teams is running
If Teams is the Active Window
Minimize Teams
Else (i.e. Teams is NOT the Active Window)
Make Teams the Active Window
Else (i.e. Teams is NOT running)
Run Teams
Wait until Teams is done opening
Make Teams the Active Window
Let's start with the code to detect if Teams is running. Currently, the source code uses this to try to detect is Teams is active: If WinExist("Teams ahk_class ApplicationFrameWindow"). Ignoring whether or not this syntax is correct (I've never seen both a WinTitle and ahk_class parameter used in the same "" block for WinExist), using the ahk_class, at least in this case, might not be the best option available. In the the Windows Spy screenshot that OP kindly provided, we can see the ahk_exe of Teams.exe would be much more specific than the ahk_class of Chrome_WidgetWin_1. As such, can use If WinExist("ahk_exe Teams.exe") instead for this program to detect if MS Teams is already running.
The next thing I want to touch on is that when Teams is "closed" i.e. by clicking the X button in the upper right corner of the screen or even when closed using the WinClose command, it's default behaviour actually minimizes it to the tray, from where the WinActivate command can be used maximize it, similar to how it would be if it was instead minimized to the taskbar by clicking the - button instead. As such, depending on personal preference, you can have Teams either minimize to the taskbar or minimize to the tray and still work the same functionally with the rest of the program. Although this was not a requested or mentioned feature by OP, I feel like this is still a useful behaviour to mention/ include in this response.
Next, the original code that the original script was using to Activate a non-active but still running MS Teams was as follows:
Else
{
WinGet, winState, MinMax
If (winState = -1)
{
WinRestore
WinActivate
}
}
While the additional checks and logic that OP implemented could be useful in some scenarios, I feel like it could effectively be shortened and replaced with just a simple
Else
WinActivate
The last change I made that deviated from the original script was just the bit that ran Teams if it was not already open. I feel like I've already discussed the changes enough above, but in summary: run ahk_exe Teams.exe was replaced with run "%AppData%\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk"
Final Code:
F11::
If WinExist("ahk_exe Teams.exe")
{
IfWinActive
WinMinimize ;Use this if you want to minimize Teams to the Taskbar
;WinClose ;Use this if you want to minimize Teams to the Tray
Else
WinActivate
}
Else
{
run "%AppData%\Microsoft\Windows\Start Menu\Programs\Microsoft Teams.lnk"
WinWait, Teams
WinActivate
}
return
This was a bit longer than usual, but I hope this was helpful. Once again, if you have any Questions/ Comments/ Concerns, or just want more clarification, feel free to ask.