Simply put, I want Ctrl+Alt+T to activate the Windows Terminal window. Previously I've used this:
^!T::
if WinExist("Windows PowerShell")
WinActivate
else
Run, wt
Return
But this doesn't cut it anymore, because the Windows Terminal changes its title when I am using Posh Git.
So I need to activate the window on whatever window that has the process name "WindowsTerminal.exe".
I've tried this but for some reason it does not recognize the correct window:
^!T::
if WinExist(ahk_exe "WindowsTerminal.exe")
WinActivate
else
Run, wt
Return
Your syntax for invoking WinExist with the name of a process/ exe is incorrect
Instead of:
if WinExist(ahk_exe "WindowsTerminal.exe")
You need to also include the ahk_exe part of it within the quotation marks.
So like this:
if WinExist("ahk_exe WindowsTerminal.exe")
Final Code:
^!T::
if WinExist("ahk_exe WindowsTerminal.exe")
WinActivate
else
Run, wt
Return
Solution:
^!T::
_WindowId = -1
WinGet _Windows, List
Loop %_Windows%
{
_Id := _Windows%A_Index%
WinGet, _PName , ProcessName, ahk_id %_Id%
if (_PName == "WindowsTerminal.exe")
{
_WindowId = %_Id%
break
}
}
if (_WindowId != -1)
{
WinActivate, ahk_id %_WindowId%
} else
{
Run, wt
}
Return
There's probably a shorter way to do this with AHK but I can't be bothered with that gross syntax anymore.
Related
I mean detect it without pressing a hotkey that will call the lines of code that will check it if said window is active
Below example works fine but pressing 4 is required.
4::
if WinActive("*Untitled - Notepad ahk_exe notepad.exe")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
Return
I have also tried this, but it just spams me with MsgBoxes.
Loop, 50
{
if WinActive("ahk_class #32770")
{
MsgBox, Found Notepad
}
else
{
MsgBox, Did Not Find Notepad
}
}
Thanks.
A simple code I whipped up to toggle something. I'm currently trying to make this script work on a game like Fallout4 only when it is present. Pauses automatically when it loses focus on that window.
*RButton::
*Control::
*Shift::
while (GetKeyState("Ctrl", "T") ^ GetKeyState("Shift", "T"))
{
while (GetKeyState("RButton", "P"))
{
;Some Action
}
;Some Action
return
}
return
*F8::Suspend
*F9::Exitapp
SetTitleMatchMode, 2
#IfWinActive SomeApplication.exe
;Run This Script
#IfWinNotActive SomeApplication.exe
;Suspend+Pause This Script
return
Exit:
ExitApp
Return
I'm having trouble making this part work with Fallout4, it doesn't detect it at all but works well with notepad but I would have to manually execute the part after #IfWinActive
SetTitleMatchMode, 2
#IfWinActive ahk_exe Fallout4.exe
;Run THIS Script
#IfWinNotActive ahk_exe Fallout4.exe
;Suspend+Pause This Script
All you need in this case is to make your hotkeys context-sensitive, that is, to make them only work if the defined window is active, using the #IfWinActive directive:
#IfWinActive ahk_exe Fallout4.exe
*RButton::
*Control::
*Shift::
while (GetKeyState("Ctrl", "T") ^ GetKeyState("Shift", "T"))
{
while (GetKeyState("RButton", "P"))
{
;Some Action
}
;Some Action
return
}
return
*F8::Suspend
*F9::Exitapp
#IfWinActive ; turn off context sensitivity
https://www.autohotkey.com/docs/commands/_IfWinActive.htm
I'm building a script that will pause my music if it's playing when I lock my workstation. I use spotify, which should be simple to get it's play state by inspecting the window title. When not playing anything, it's title is simply "Spotify", but when it's playing media, the window title changes to the title of the track currently playing. I can see this using the Window Spy.
I've tried finding the spotify window and reading it's title, using WinGetTitle, title, ahk_exe Spotify.exe which should write the title into the var title. This doesn't work, title is an empty string. Intriguingly however it works if the spotify window is minimized.
#L::
{
WinGetTitle, title, ahk_exe Spotify.exe
if(title != "Spotify")
{
Send {Media_Play_Pause}
}
DllCall("LockWorkStation")
return
}
This is on Windows 10. WinGetClass, c, ahk_exe Spotify.exe correctly finds the window, but the class name is Chrome_WidgetWin0 because I'm guessing the app is written in Electron. Other electron apps seem to have the same class name, just incrementing the number at the end.
What I'd love is a way of hooking into whatever windows API spotify is using to report it's current play state, as Windows 10 recognises it as a media application and adds play/pause buttons to it's tab in the taskbar, and in the windows volume control overlay.
Thanks
There are probably more than one windows of class "Chrome_WidgetWin0" belonging to the process "Spotify.exe".
Try this:
#IfWinExist ahk_exe Spotify.exe
#l::
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
DllCall("LockWorkStation")
return
#IfWinExist
EDIT: To find out whether it really works, run this test:
#IfWinExist ahk_exe Spotify.exe
#q:: ; Win+Q
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
; MsgBox, "%title%"
If (title = "")
continue
MsgBox, "%title%"
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
return
#IfWinExist
Update 05.08.16 - 12:08 AM:
Here is a video of me trying to get this to work: https://youtu.be/h9gAirlUGyI
So, I'm trying to have a key pressed once on program launch.
This is what I have:
{
Send, {F11}
}
Now when I launch the program it doesn't send the key.
Help?
flag = 0
processName := "xenia.exe"
Loop
{
If ProcessExist(processName)
{
WinGetTitle, title, ahk_exe %processName%
IfWinActive, %title%
{
if flag = 0
{
Send, {F11}
flag = 1
}
}
else
{
flag = 0
}
}
Sleep, 1000
}
ProcessExist(Name)
{
Process,Exist,%Name%
return Errorlevel
}
Per your other question and my comment there:
; This code both launches and fullscreens XENIA . . .
#Persistent
#SingleInstance, Force
Run, xenia.exe
Sleep, 500
GoSub, DoIt
return
numpad0::
GoSub, DoIt
return
DoIt:
IfWinExist, ahk_class XeniaWindowClass ahk_exe xenia.exe
{
WinActivate, ahk_class XeniaWindowClass
#IfWinActive, ahk_class XeniaWindowClass
{
Send, {F11}
}
}
return
Now, if the question is REALLY about launching xenia with a file you've clicked on, e.g., using an explorer extension, there are ways of making it work (by adding the script to the registry and/or passing the file name and path into the command line. Again, please give us details as to what you want to happen!
Really struggling with this.
I need to simply open a windows explorer window at a specified domain, wait until it is active then proceed. This is what I have so far:
#::
{
WinGet, old_active, ID, A
Run, explore C:\Users\Nathan\Documents\Test FDA
loop{
WinGet, new_active, ID, A
if(ahk_id %new_active% != ahk_id %old_active%)
{
WinMaximize, A
break
}
}
return
}
EDIT SOLVED ?>>>
DIDNT KNOW WINDOW SPY EXISTED CAME WITH IT :(((
Long time wasted, this simply works.
[::
{
Run explore C:\Users\Nathan\Documents\Test FDA
WinWaitActive Test FDA
WinMaximize A
return
}
how about this?:
"
F12::
WINDOWEXPLORER:
WinWaitActive, Windows Explorer,, 0.01
if ErrorLevel
{
Goto WINDOWEXPLORER
}
else
{
; SoundBeep 4500, 30
Return
}
"
I think you are looking for the WinWaitActive function. From the documentation:
Waits until the specified window is active
Put it after the line with Run...:
WinWaitActive, WinTitle
You will need to replace WinTitle with the title of the window that comes up after the Run command. There are other options available, such as duration to wait, titles to exclude, etc.
See the documentation for more details.