Spotify Ad Killer

Post your working scripts, libraries and tools for AHK v1.1 and older
boom
Posts: 2
Joined: 12 Aug 2020, 06:47

Spotify Ad Killer

Post by boom » 12 Aug 2020, 08:57

I created a script which kills and restarts Spotify whenever an ad begins playing. I know there is already the iseahound's post from 2016 (https://www.autohotkey.com/boards/viewtopic.php?f=6&t=47281&hilit=spotify+ad), but I became aware of it just after I finished my version. I hope someone will find it useful.

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
}
Whenever an ad plays, the Spotify window gets the "Advertisement" as a title. This script checks every second to find a window titled "Advertisement", then checks if it has a correct ahk_class and then it kills the Spotify process, restarts it and resumes playing from the next song.

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
}
Thanks to @Noesis and @GEV for that.

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
}
I also load this script with Spotify every time, so it doesn't make sense to leave it running while Spotify is down. So, before every state check, I can look for a Spotify process and, if it isn't running, terminate the script. I use another Tray_Refresh() here to conveniently remove dead Toastify tray icon that usually stays hanging after I manually close the Spotify app:

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
}

VibinonAHk
Posts: 8
Joined: 09 Aug 2020, 22:31

Re: Spotify Ad Killer

Post by VibinonAHk » 12 Aug 2020, 22:59

i actually had an idea like this but could not immplement so yeah. amazing tho

poptart
Posts: 1
Joined: 19 Aug 2022, 01:29

Re: Spotify Ad Killer

Post by poptart » 19 Aug 2022, 01:32

This idea struck me as I was playing Minecraft with Spotify open; I skipped ads by restarting the application, but what if I could make it to where is does that automatically? I'm not a scripter myself so thank you for saving me the pain of doing it myself lol.

Post Reply

Return to “Scripts and Functions (v1)”