I got really angry at Last.fm today so I wanted to see how hard it was to duplicate it's Scrobbler feature (the only useful feature in Last.fm).

I wrote a simple script that uses subprocess and osascript (a command line AppleScript).


In [20]:
import subprocess


def osascript(script):
    """
    This is a bridge between Python and AppleScript using the `osascript`
    comamnd line app.
    """

    process = subprocess.run(['osascript', '-e', script],
                             stdout=subprocess.PIPE)

    # Because the `subprocess.CompletedProcess` class returns a byte (followed
    # by a new line), I have to clean it a little.
    return process.stdout.decode('utf-8').strip()

In [21]:
def itunes(args):
    """This is so I don't have to repeat a bunch of code."""

    script = 'tell application "iTunes" to {0} as string'

    return osascript(script.format(args))

In [22]:
def is_running():
    """
    Here we a checking to see if iTunes is currently running.
    I'm doing this because if iTunes is closed I don't want to open it.
    """
    output = osascript('application "iTunes" is running')

    if output == 'true':
        return True
    else:
        return False

In [17]:
def is_playing():
    """This function is to check if iTunes is currently playing music."""
    output = itunes('player state')

    if output == 'playing':
        return True
    else:
        return False

In [18]:
def get_track():
    """This is the main function that get the currently playing track."""
    track = {}

    track['name'] = itunes('name of current track')
    track['artist'] = itunes('artist of current track')
    track['album'] = itunes('album of current track')

    return track

In [19]:
# `is_running()` and `is_playing()` need to be run separately, if together
# than it will launch iTunes.
if is_running():
    if is_playing():
        print('iTunes is currently playing:')
        print("{name} / {artist} / {album}".format(**get_track()))


iTunes is currently playing:
Garcia Counterpoint / Bryce Dessner / Day of the Dead

In [ ]: