This Script get music now playing in iTunes App or Spotify, only Mac OS X 10.x.x
__author__ = 'Marcos Vinicius Rodrigues - https://mvrpl.com.br'
__doc__ = '''
This Script get music now playing in iTunes App or Spotify, only Mac OS X 10.x.x
'''
# pip install pyobjc
import applescript # pip install https://pypi.python.org/packages/source/p/py-applescript/py-applescript-1.0.0.tar.gz
import json
tell_iTunes = applescript.AppleScript('''
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
on Playing()
set iTunesRunning to is_running("iTunes")
set SpotifyRunning to is_running("Spotify")
if iTunesRunning then
tell application "iTunes"
set songTitle to the name of the current track
set songArtist to the artist of the current track
set songAlbum to the album of the current track
set result to songArtist & "%-%" & songTitle & "%-%" & songAlbum
if player state is playing then
return result
else
return "None" & "%-%" & "None" & "%-%" & "None"
end if
end tell
end if
if SpotifyRunning then
tell application "Spotify"
set songTitle to the name of the current track
set songArtist to the artist of the current track
set songAlbum to the album of the current track
set result to songArtist & "%-%" & songTitle & "%-%" & songAlbum
if player state is playing then
return result
else
return "None" & "%-%" & "None" & "%-%" & "None"
end if
end tell
end if
end Playing
''')
output = tell_iTunes.call('Playing').split('%-%')
print json.dumps({'Artist': output[0], 'song': output[1], 'album': output[2]}, indent=4)