Created
March 21, 2024 14:27
-
-
Save Darkflib/676f799872778c04140f188576ab92f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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