TL;DR:
Code: Select all
#Persistent
SetTimer, CheckState, 1000
return
CheckState:
WinGet, id, List, Advertisement
Loop, %id%
{
this_id := id%A_Index%
WinGetClass, this_class, ahk_id %this_id%
if (%this_class% = Chrome_WidgetWin_0)
{
Kill_Ad()
break
}
}
return
Kill_Ad()
{
WinGet, active_id, ID, A ; Get the ID of an active window
Process, Close, Spotify.exe
Run, "%A_AppData%\Spotify\Spotify.exe", , hide
WinWaitActive, ahk_exe Spotify.exe ; Wait for Spotify to get focus
Send {Media_Next}
Sleep 500 ; If the Media_Next doesn't get triggered, increase this value
WinActivate, ahk_id %active_id% ; Reactivate the window we were previously using
return
}
This is the essential functionality right here, but I use Toastify along with the main app, so I've added that and a couple of other things. If you're interested, read ahead.
The goal is not to wait for the app to properly exit because we want to make the restart as fast as possible. Hence, we have to kill the process. Killing the process leaves the app's icon hanging in the tray, and they mount up every time we restart the app. Luckily, there is the Tray Refresh function from @Noesis on this topic: https://www.autohotkey.com/boards/viewtopic.php?p=95533#p95533 - I use that to remove dead tray icons. I just had to replace ControlNN := "ToolbarWindow322" with ControlNN := "ToolbarWindow323" to make it work on Win10 v1903:
Code: Select all
Tray_Refresh()
{
WM_MOUSEMOVE := 0x200
HiddenWindows := A_DetectHiddenWindows
DetectHiddenWindows, On
TrayTitle := "AHK_class Shell_TrayWnd"
ControlNN := "ToolbarWindow323"
IcSz := 24
Loop, 2
{
ControlGetPos, xTray,yTray,wdTray,htTray, %ControlNN%, %TrayTitle%
y := htTray - 10
While (y > 0)
{
x := wdTray - IcSz/2
While (x > 0)
{
point := (y << 16) + x
PostMessage, %WM_MOUSEMOVE%, 0, %point%, %ControlNN%, %TrayTitle%
x -= IcSz/2
}
y -= IcSz/2
}
TrayTitle := "AHK_class NotifyIconOverflowWindow"
ControlNN := "ToolbarWindow321"
IcSz := 32
}
DetectHiddenWindows, %HiddenWindows%
Return
}
As I mentioned, I use Toastify (https://github.com/aleab/toastify) so I kill and run it along with the main app:
Code: Select all
Kill_Ad()
{
WinGet, active_id, ID, A ; Get the ID of an active window
Process, Close, Spotify.exe
Process, Close, Toastify.exe
Run, "%A_AppData%\Spotify\Spotify.exe", , hide
Run, "%A_ProgramFiles%\Toastify\Toastify.exe", , hide
WinWaitActive, ahk_exe Spotify.exe ; Wait for Spotify to get focus
Send {Media_Next}
Sleep 500 ; If the Media_Next doesn't get triggered, increase this value
WinActivate, ahk_id %active_id% ; Reactivate the window we were previously using
return
}
Code: Select all
CheckState:
Process, Exist, Spotify.exe
if !ErrorLevel ; Spotify is not running
{
Sleep 3000
Tray_Refresh() ; To clean the Toastify icon from the tray
ExitApp ; Terminate this script
}
else
{
WinGet, id, List, Advertisement
Loop, %id%
{
this_id := id%A_Index%
WinGetClass, this_class, ahk_id %this_id%
if (%this_class% = Chrome_WidgetWin_0)
{
Kill_Ad()
Tray_Refresh()
break
}
}
return
}
Here is the whole script:
Code: Select all
#Persistent
SetTimer, CheckState, 1000
return
CheckState:
Process, Exist, Spotify.exe
if !ErrorLevel ; Spotify is not running
{
Sleep 3000
Tray_Refresh() ; To clean the Toastify icon from the tray
ExitApp ; Terminate this script
}
else
{
WinGet, id, List, Advertisement
Loop, %id%
{
this_id := id%A_Index%
WinGetClass, this_class, ahk_id %this_id%
if (%this_class% = Chrome_WidgetWin_0)
{
Kill_Ad()
Tray_Refresh()
break
}
}
return
}
Kill_Ad()
{
WinGet, active_id, ID, A ; Get the ID of an active window
Process, Close, Spotify.exe
Process, Close, Toastify.exe
Run, "%A_AppData%\Spotify\Spotify.exe", , hide
Run, "%A_ProgramFiles%\Toastify\Toastify.exe", , hide
WinWaitActive, ahk_exe Spotify.exe ; Wait for Spotify to get focus
Send {Media_Next}
Sleep 500 ; If the Media_Next doesn't get triggered, increase this value
WinActivate, ahk_id %active_id% ; Reactivate the window we were previously using
return
}
Tray_Refresh()
{
WM_MOUSEMOVE := 0x200
HiddenWindows := A_DetectHiddenWindows
DetectHiddenWindows, On
TrayTitle := "AHK_class Shell_TrayWnd"
ControlNN := "ToolbarWindow323"
IcSz := 24
Loop, 2
{
ControlGetPos, xTray,yTray,wdTray,htTray, %ControlNN%, %TrayTitle%
y := htTray - 10
While (y > 0)
{
x := wdTray - IcSz/2
While (x > 0)
{
point := (y << 16) + x
PostMessage, %WM_MOUSEMOVE%, 0, %point%, %ControlNN%, %TrayTitle%
x -= IcSz/2
}
y -= IcSz/2
}
TrayTitle := "AHK_class NotifyIconOverflowWindow"
ControlNN := "ToolbarWindow321"
IcSz := 32
}
DetectHiddenWindows, %HiddenWindows%
Return
}