How to detect when a new song starts playing in Spotify using AppleScript?

I need help creating an AppleScript that can tell when Spotify switches to a different song. My goal is to show a notification whenever this happens.

First I tried looking at the player position to see if it was zero, thinking that meant a new track started. But this approach failed because users sometimes restart the same song or jump back to the beginning.

Now I’m thinking about making a script that runs continuously and checks the song title every few seconds to see if it changed. I’m worried this might use too much memory though. Here’s what I have so far but it’s not working:

tell application "System Events"
    -- verify both notification app and music player are running
    set appRunning to ¬
        (count of (every process whose name is "NotificationApp")) > 0
    (count of (every process whose name is "Spotify")) > 0
end tell

-- create empty string for storing song name
global previous_track
set previous_track to ""

on idle
    tell application "Spotify"
        if player state is playing then
            copy name of current track to track_name_now
            -- compare with previously stored track name
            if track_name_now is not previous_track then
                copy track_name_now to previous_track
                set performer to artist of current track
                set record_album to album of current track
                tell application "NotificationApp"
                    -- define notification types
                    set the notificationTypes to ¬
                        {"MusicTrackChange"}

                    -- set which notifications are enabled
                    set the enabledTypes to ¬
                        {"MusicTrackChange"}

                    -- register with notification system
                    register as application ¬
                        "Spotify" all notifications notificationTypes ¬
                        default notifications enabledTypes ¬
                        icon of application "Spotify"

                    -- display the notification
                    notify with name ¬
                        "MusicTrackChange" title ¬
                        track_name_now description ¬
                        performer application name "Spotify"
                end tell
            end if
            return 5
        end if
    end tell
end idle

Any suggestions on how to make this work properly would be great. Thanks!

i think the appRunning part is messing things up. try putting return 5 outside that if condition so it keeps checking. also, consider simplifying the notification section; it feels overly complex for this task.

had the same problem last week. move global previous_track inside the idle handler - having it outside looks wrong. also, you’re mixing copy and set commands in a weird way that’ll probably cause issues.

Your script’s broken because previous_track never gets initialized on the first run. It starts as an empty string, so the very first song always triggers a notification even if it was already playing. You need to set it to whatever’s currently playing when the script launches.

Add this after your global declaration:

tell application "Spotify"
    if player state is playing then
        set previous_track to name of current track
    end if
end tell

Your variable declarations in the idle handler are all over the place too. Pick either set track_name_now to or copy ... to track_name_now and stick with it - mixing them just makes things confusing. I’d go with set since it’s simpler.

That ¬ character in your System Events block is also messing up the boolean logic. Either fix the syntax or just dump that validation entirely - the script works fine without it.

Your notification registration is creating unnecessary overhead. You only need to register once, not every time a song changes. Move that registration outside the idle handler - put it right after your global variables so it runs once when the script starts.

Your script doesn’t handle when Spotify isn’t running. The tell application "Spotify" block will launch Spotify if it’s closed, which you probably don’t want. Add a simple check like if application "Spotify" is running then before accessing the current track.

You can simplify the notification a lot. Just use notify with name "MusicTrackChange" title track_name_now subtitle performer instead of all that complex description formatting. Checking every 5 seconds with idle is perfectly fine and won’t cause performance issues.

Your problem’s in the System Events block at the top. The syntax for checking if both apps are running is wrong - you’re using ¬ continuation but not combining the conditions properly. This probably kills the script before it even gets to the idle handler.

First, just remove that whole verification section and test if the core stuff works. The idle approach is fine and won’t eat up memory checking every 5 seconds. I’ve run similar scripts for months with no issues.

Make sure you saved your script as “stay open” - otherwise the idle handler does nothing. In Script Editor, go to File > Export and check “Stay open after run handler.”

Also, wrap some error handling around those Spotify commands. They’ll break if someone pauses or closes Spotify unexpectedly.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.