Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created March 21, 2024 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darkflib/676f799872778c04140f188576ab92f8 to your computer and use it in GitHub Desktop.
Save Darkflib/676f799872778c04140f188576ab92f8 to your computer and use it in GitHub Desktop.
import subprocess
import time
def get_current_song():
"""Fetches the current song playing on Spotify."""
script = '''
tell application "Spotify"
if it is running then
if player state is playing then
set trackName to the name of the current track
set artistName to the artist of the current track
return artistName & " - " & trackName
end if
end if
end tell
'''
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True)
if result.stdout:
return result.stdout.strip()
return None
def send_notification(title, message):
"""Sends a notification to the Mac desktop."""
print(title)
print(message)
escaped_message = message.replace('"', '\\"')
script = ['osascript', '-e', f'display notification "{escaped_message}" with title "{title}"']
subprocess.run(script)
def monitor_songs():
last_song = None
while True:
current_song = get_current_song()
if current_song and current_song != last_song:
send_notification("Now Playing on Spotify", current_song)
last_song = current_song
time.sleep(3) # Check every 3 seconds
if __name__ == "__main__":
monitor_songs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment