Automatically skip tracks in Spotify Mac App

If you listen to a lot of music while working like I do you might find this one useful and it should work out of box on a Mac without any software installs necessary!

Here is a simple python script you can run to automatically skip a track that matches a criteria

I’m using this on EDM radio shows that have good music but sometimes have just a little bit too much talking like:

  • Armin van Buuren – A State of Trance
  • Above and Beyond – Group Therapy

The main catchall is looking for a duration_seconds < 90 which is likely not a song

Feel free to adapt the code in # ADD SKIP CONDITIONS HERE!

If you haven’t done this before simply paste the below code into a file called spotify-skip.py in say your Downloads folder and run it in the terminal (look for Terminal.app in Spotlight search or your Applications folder) and run python3 spotify-skip.py like you see below.

Here it is in action as Spotify works through a playlist and you get to listen to maximum music 🎶

Enjoy and keep on jammin!

https://github.com/johnculviner/spotify-auto-skip/blob/main/spotify-skip.py

Code

import subprocess
import time

last_track_name = ''
skipped_push = False

def get_current_track():
    applescript = '''
    tell application "Spotify"
        if it is running then
            if player state is playing then
                set trackName to name of current track
                set trackArtist to artist of current track
                set trackAlbum to album of current track
                set trackDuration to duration of current track
                return trackName & "|" & trackArtist & "|" & trackAlbum & "|" & trackDuration
            else
                return "NOT_PLAYING"
            end if
        else
            return "SPOTIFY_NOT_RUNNING"
        end if
    end tell
    '''
    process = subprocess.Popen(
        ['osascript', '-e', applescript],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    output, error = process.communicate()
    if process.returncode == 0:
        return output.decode().strip()
    else:
        return None

def skip(seconds):
    applescript = f'''
    tell application "Spotify"
        set player position to (player position + {seconds})
    end tell
    '''
    subprocess.Popen(['osascript', '-e', applescript],
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE)

def skip_track(track_name):
    print(f"Skipping track: {track_name}")
    applescript = 'tell application "Spotify" to next track'
    process = subprocess.Popen(
        ['osascript', '-e', applescript],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
    process.communicate()

def poll_spotify(poll_interval=2):
    global last_track_name, skipped_push

    while True:
        track_info = get_current_track()
        if track_info == "SPOTIFY_NOT_RUNNING":
            print("Spotify is not running.")
            continue
        elif track_info == "NOT_PLAYING":
            continue
        elif track_info is None:
            print("Error retrieving track information.")
            continue

        parts = track_info.split('|')
        if len(parts) != 4:
            print("Unexpected track format", track_info)
            continue

        track_name, track_artist, track_album, track_duration = parts
        duration_seconds = int(track_duration)

        # Reset the push-skip flag if the track has changed.
        if last_track_name != track_name:
            last_track_name = track_name
            skipped_push = False

        if "(Messages" in track_name:
            skip_track(track_name)
        elif "Intro (" in track_name:
            skip_track(track_name)
        elif "(Push The" in track_name and not skipped_push:
            print(f"Skipping 30 seconds for track: {track_name}")
            skip(30)
            skipped_push = True
        elif duration_seconds < 90:
            skip_track(track_name)
                
            
        time.sleep(poll_interval)

if __name__ == '__main__':
    try:
        poll_spotify()
    except KeyboardInterrupt:
        print("\nExiting...")

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Cute Blog by Crimson Themes.