I would like to have my Spotify playlist played to me every morning at 8AM sharp. Using the Windows Task Scheduler and an AutoIt script I can have the Spotify App started and subsequently automate the steps to run my preferred Spotify playlist. I can even wake my computer up in order to start executing the task. In this article I show you how to do this.
In my previous article I introduced AutoIt – what it is, how to install it and how to get going on Windows 10.
Write the AutoIt script for Running the Spotify App
AutoIt can perform desktop actions. Such as run a program. However, Spotify is not a normal program, it is an app from the Windows App Store. Running an app turns out to be little bit different from running a regular application. Fortunately, the forum provided help with this challenge. Here is what to do:
Press the Windows key together with R.
In the dialog box type shell:Appsfolder. Then press OK. If you do not see a menu bar for the folder with the option View, the first click on Organize and select Layout | Menu bar.
Then open the View menu and select Choose details:
Select the property AppUserModelId to be shown as well.
The AppUserModelId is the identifier we need for the right App to be started from AutoIt.
Try to copy this identifier (I ended up using Copy and OCR to get the value pasted into my AutoIt script).
There are two ways to run a Windows App from AutoIt:
ShellExecute(‘shell:Appsfolder\’ & $spotifyAppId)
Run(@ComSpec & ‘ /c start “” “shell:Appsfolder\’ & $spotifyAppId & ‘”‘, ”, @SW_HIDE)
(note: $spotifyAppId is a local variable defined like this: Local $spotifyAppId = “SpotifyAB.SpotifyMusic_zpdnekdrzrea0!Spotify”
The code required for opening the Spotify App, searching for a specific playlist and starting that playlist is now quite straightforward, using x,y positions for interacting with the controls search field and “play search result”:
Create Task in Windows Task Scheduler
Now my AutoIt script to open the Spotify app and run my favorite playlist is done, I would like to create a scheduled task to run this script every morning. I use the Windows Task Scheduler app for this.
Press the Windows key and type Task:
Then open the Task Scheduler app:
Click on Create Basic Task. A wizard appears. Go through the steps – define a name, the time to run the task (every morning at 8:00 AM) and specify the type of task: run an application.
In the final step, provide details for the application to run. In my case:
- full path to the AutoIt executable : “C:\Program Files (x86)\AutoIt3\autoit3.exe”
- full path to my au3 script that should be executed : “C:\temp\RunSpotify.au3”
Press Next and Press Finish:
The task is now created and scheduled for execution, every day at 8:00 AM.
Now wait for tomorrow, and find out if the morning working experience is enhanced.
Note the checkbox “Wake the computer to run this task”. I can even use my laptop as an alarm clock, waking me with soft, sweet music.
Additionally, settings can be made for retry and ending of tasks.
Finally, from the Action menu, the task can be executed on demand – whenever you need a dose of music. This of course amounts to the same thing as just running the AutoIt script or creating a desktop shortcut for this action and executing the shortcut.
A logical next step would be to pass the name of the playlist as a parameter rather than hard coding it in the AutoIt script and then create multiple tasks for various playlists at various times during the day. Command line parameters are accessible in AutoIt scripts in the array $CmdLine, for example $CmdLine for the first (not zero based index) parameter.
Programmatically controlling Task Schedules from within AutoIt, using this library: https://www.autoitscript.com/wiki/TaskScheduler
AutoIt Forum thread on running Windows 10 Apps (rather than regular applications): https://www.autoitscript.com/forum/topic/187123-win10-run-windows-store-apps/
Schedule Windows to Sleep and Wake Automatically – https://www.groovypost.com/howto/schedule-wake-sleep-windows-automatically/#sleep (statement to put computer to sleep: Rundll32.exe Powrprof.dll,SetSuspendState Sleep)