When I log in to my laptop (or unlock after a break), I want Spotify to automatically play - but only if it was running before I closed my laptop - and I don't like it starting halfway through a song, so I want it to always start a new track on my playlist.
I figured it out and just got it working with a VBScript and the Task Scheduler... and I thought I'd share my solution. 🙂
(If you're not used to messin' with stuff like this, see Stack Overflow or ask your local neighborhood nerd.)
Open Notepad and paste in the following code:
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name='Spotify.exe'")
If colProcessList.count>0 then
Set WshShell = WScript.CreateObject("WScript.Shell")
Comandline = "C:\Users\**YOUR PROFILE NAME**\AppData\Roaming\Spotify\Spotify.exe"
WScript.sleep 500
CreateObject("WScript.Shell").Run("spotify:user:johnsmith:playlist:0rDhbjtAgoLssBmFPaBDpN")
WScript.sleep 2000
WshShell.SendKeys "^{RIGHT}"
Set WshShell = Nothing
else
'do nothing
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
- replace **YOUR PROFILE NAME** with your windows login (or otherwise adjust the Spotify.exe path as requried)
- replace spotify:user:johnsmith:playlist:0rDhbjtAgoLssBmFPaBDpN with your Spotify playlist URI. (Right-click your playlist > Share > Copy Spotify URI)
- Save with a filename extension of .VBS, somewhere with (you'll probably have to set the file type to *.* in the Save dialog for it to work)
- Open Task Scheduler (Click `Start` and type `Task Scheduler`)
- Click Create Task
- On the General tab, give the task a name
- On the Triggers tab, click NEW > Begin the task: On workstation unlock > Delay Task for 5 seconds, click OK. click NEW again > Begin the task: At Log On > Delay Task for 15 seconds, click OK.
- On the Actions tab, click NEW > Start a program > under Program/Script, paste the path and filename where you saved the .VBS file. Click OK - OK.