Aug 15, 2009

Controlling Pandora

On Windows, there is the excellent program called OpenPandora that embeds Pandora Radio in a small window and lets you control it from the multimedia keys on your keyboard, rather than a clumsy webpage. However, no similar thing exists on Linux to my knowledge. Before I discovered OpenPandora, I wrote an AutoHotKeys hack for controlling Pandora, so I decided to use a similar method on Linux. I used a tool called xdotool which lets you send keyboard and control the mouse from a shell script. My solution has several pieces which I'll share.

The first is a webpage that can open Pandora as a minimal-looking pop-up. Here's an example:



PandoraCustomLauncher.html:

<html>
<head>
<title>Launch Pandora</title>
<script type="text/javascript">

var myWindow;
function openWindow() {
myWindow = window.open("http://pandora.com","_blank",config="height=260, width=625, toolbar=no,
menubar=no, scrollbars=1, resizable=yes,location=no, directories=no, status=no");
}

</script>
</head>
<body>
<form>

<input type="button" onClick="openWindow()" value="Launch Pandora">

</form>
</body>
</html>

The next is a script that handles launching Pandora. I used the Konqueror webbrowser, because it plays Flash most reliably on my Linux installation. (Yes... sad that should even be a factor.)

pandora.sh:
#!/bin/sh

# Store the initial active window.
PANDORA_FOCUS=`xdotool getwindowfocus`

# Launch Konqueror
konqueror file:///path/to/PandoraCustomLauncher.html
sleep 2

# Launch the Pandora Window
PANDORA_LAUNCHER=`xdotool search --title "Launch Pandora" | head -n 1`
xdotool windowfocus $PANDORA_LAUNCHER
xdotool key "Ctrl"
xdotool key "L"

sleep 7

# Find, raise, move, and focus on the window.
PANDORA_WIN=`xdotool search --title "Pandora Radio" | head -n 1`
xdotool windowraise $PANDORA_WIN
xdotool windowmove $PANDORA_WIN 0 40
xdotool windowfocus $PANDORA_WIN

# Move the scrollbars.
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Down"
xdotool key "Right"
xdotool key "Right"
xdotool key "Right"
xdotool key "Right"
xdotool key "Right"

sleep 3

#Store the initial mouse position.
PANDORA_X=`xdotool getmouselocation | awk '{print $1}' | cut -d : -f 2`
PANDORA_Y=`xdotool getmouselocation | awk '{print $2}' | cut -d : -f 2`

# Set the focus to the Flash component rather than the window
xdotool mousemove 268 198
xdotool click 1

# Restore the mouse position.
xdotool mousemove $PANDORA_X $PANDORA_Y

# Close the Launch Window
xdotool windowfocus $PANDORA_LAUNCHER
#sleep 1
xdotool keydown "Ctrl"
xdotool key "Q"
xdotool keyup "Ctrl"

# Restore the original focus
xdotool windowfocus $PANDORA_FOCUS


I made a symlink from ~/bin/pandora to pandora.sh. Now when I run "pandora" I get this:


I guess another reason I used Konqueror is it actually obeys the "location=no,status=no" settings in window.open() Javascript function, resulting in a very minimalized browser window.

Finally, the best part: pausing and skipping songs with the multimedia keys.

pause_pandora.sh
#!/bin/sh

# Store the initial active window.
PANDORA_FOCUS=`xdotool getwindowfocus`

# Find the window.
PANDORA_WIN=`xdotool search --title "Pandora Radio" | head -n 1`

if [ ! -n "$PANDORA_WIN" ]
then
echo 'PANDORA_WIN is null'
else
echo 'PANDORA_WIN is not null'
# Hit the spacebar
xdotool windowfocus $PANDORA_WIN
xdotool key "space"

# Restore the original focus
xdotool windowfocus $PANDORA_FOCUS
fi

next_pandora.sh
#!/bin/sh

# Store the initial active window.
PANDORA_FOCUS=`xdotool getwindowfocus`

# Find the window.
PANDORA_WIN=`xdotool search --title "Pandora Radio" | head -n 1`

if [ ! -n "$PANDORA_WIN" ]
then
echo 'PANDORA_WIN is null'
else
echo 'PANDORA_WIN is not null'
# Hit the right arrow key
xdotool windowfocus $PANDORA_WIN
xdotool key "Right"

# Restore the original focus
xdotool windowfocus $PANDORA_FOCUS
fi


The last step is just to set pause_pandora and next_pandora to be run when you hit the Play/Pause and Next keys. In KDE, this can be done under System Settings > Input Actions.

It's not perfect, but it will suffice until I can stream music directly into my brain.

2 comments:

Unknown said...

You can always try the Livio Radio. It's a little easier to use than the Pandora website. You should check it out, I think you would really like it. Here's the website, http://tinyurl.com/blog-jnl

Anonymous said...
This comment has been removed by a blog administrator.