Skip to content

Instantly share code, notes, and snippets.

@Zequez
Created August 19, 2023 13:35
Show Gist options
  • Save Zequez/36bd8efdd58346d0f22f5c6c5377ddbb to your computer and use it in GitHub Desktop.
Save Zequez/36bd8efdd58346d0f22f5c6c5377ddbb to your computer and use it in GitHub Desktop.
Pause Spotify after every track
# This script was written by ChatGPT from the following prompt
# Could you write me a small script that runs on OSX that monitors the currently playing song on
# Spotify and when the song changes pauses for 30 seconds and then starts playing again?
import time
import subprocess
def run_applescript(script):
return subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
def main():
current_track = None
while True:
# Get the current track name from Spotify
script = 'tell application "Spotify" to name of current track'
result = run_applescript(script)
new_track = result.stdout.strip()
if new_track != current_track:
current_track = new_track
# Pause playback for 30 seconds
run_applescript('tell application "Spotify" to pause')
print(f"Paused for 30 seconds: {current_track}")
time.sleep(30)
# Resume playback
run_applescript('tell application "Spotify" to play')
print(f"Resumed playback: {current_track}")
time.sleep(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment