Browser remote control -- TV-like flipping online video channels

Donating my script-let. We use this for watching evening news online and youtube videos while sitting on the living room couch (or in bed).

Because of time difference some channels can be offline, or just boring, so this script is a convenient way of flipping through our favorite broadcasts without a keyboard.

Code:

#!/bin/bash
# Flips Firefox URLs just like TV channels.
# Operated by wireless USB remote control or through command line.
# Script must be called with command line parameter + or -
# Flips URL in current Firefox tab to the next/previous (+/-) channel from
# a list of pre-selected URLs (links) stored in URL_INDEX_DIRECTORY.
#
# To create a list of favorite channels, drag links from Firefox’s URL bar into the
# URL_INDEX_DIRECTORY. Channels will be presented alphabetically.
# For added convenience you can create a link to URL_INDEX_DIRECTORY on
# your desktop and name it “Firefox Channels” or whatever you prefer. This way
# you can drag browser URLs there instead of opening your ~/Documents/... folder.
# To remove channels simply open your channel folder and delete them.
#
# To activate the remote control, in Linux Preferences create two new Keyboard Shortcuts:
#
# Name: Channel UP
# Command: /home/<User Name>/Documents/firefox-control.sh -
#
# Name: Channel DOWN
# Command: /home/<User Name>/Documents/firefox-control.sh +
#
# Click on “shortcut” and press an available key on the remote control.
# Assign any 2 adjacent macro keys, or the multimedia next/previous keys.
# Should work with any remote. I tested with “Wireless USB PC Remote Control Mouse”
# available on Amazon for $7, ASIN: B001M56DI0, Item model number: SNX_PC-RC1
#
# You may locate this script anywhere you like, but make it executable.
# You may also modify the paths defined below to suit your preferences.
# Please install “xdotool”, available through Synaptic Package Manager.
CURRENT_CHANNEL_FILE="/home/$USER/Documents/firefox-control-channel"
URL_INDEX_DIRECTORY="/home/$USER/Documents/firefox-control-channel-index"
if [ ! -d "$URL_INDEX_DIRECTORY" ]; then
mkdir "$URL_INDEX_DIRECTORY"
echo -e [Desktop Entry]\\nName=Link to Google\\nType=Link\\nURL=http://www.google.com/ > "$URL_INDEX_DIRECTORY"/Google.desktop
fi
# Change current channel up/down based on command line argument -/+
CHANNEL_NUMBER=$(($(sed -n '1p' $CURRENT_CHANNEL_FILE)${1}1))
# Was at channel 0, cycle down to the end of the URL index.
if [ "$CHANNEL_NUMBER" = -1 ]; then
# Get zero-based file/channel count
CHANNEL_NUMBER=$(($(ls -1 "$URL_INDEX_DIRECTORY"/*.desktop | wc -l)-1))
fi
# Get URL for current channel
OIFS=$IFS
IFS=$'\n'
array=($(ls -A "$URL_INDEX_DIRECTORY"/*.desktop))
IFS=$OIFS
CHANNEL_URL="${array[$CHANNEL_NUMBER]}"
# Reached the end of the URL index, cycle back up to the top.
if [ "$CHANNEL_URL" = "" ]; then
CHANNEL_NUMBER=0
CHANNEL_URL="${array[0]}"
fi
echo Channel $CHANNEL_NUMBER \> "$CHANNEL_URL"
# Save current channel #
echo $CHANNEL_NUMBER > $CURRENT_CHANNEL_FILE
xdotool windowactivate $(xdotool search --name "Mozilla Firefox")
# Click on Firefox's URL bar to take focus away from Adobe Flash.
# This is ugly code, but Adobe Flash steals keyboard focus thus preventing
# Firefox from responding to keyboard commands, such as the following Ctrl+F4.
# Clicking on a different tab (and other locations) takes focus away from Flash
# allowing the browser to operate normally.
# If mouse click in this location causes problem, coordinates can be modified:
# Run “xdotool getmouselocation” to obtain a more appropriate screen location.
xdotool mousemove 500 50 click 1
# Close current tab in Firefox.
# This is to prevent many tabs playing flash video simultaneously.
# Firefox will not close the last tab, instead produces an error
# message and closes. So two tabs or more works better.
xdotool key ctrl+F4
# Open the URL
#xdg-open "$CHANNEL_URL"
firefox "$CHANNEL_URL"
#/*