Bash script to disable screensaver (while watching flash movies)

March 21, 2011 2 comments

My screensaver is timed to run at 5 mins of inactivity. This normally works well for me. If i am watching something on mplayer, it disables the screensaver anyway and its not a bother. However, while watching flash videos, it is very annoying. Especially now when there is so much to pick from to view online, it gets annoying to move my mouse every 5 mins. I wrote this bash script to disable the screensaver. You can enable it again by just clicking on the icon in the notification area.

PS: You may need to install libnotify or you could just comment out the line which uses “notify-send”

#!/bin/bash
SERVICE=’gnome-screensaver-command’
zennot () {
zenity –notification –window-icon=/usr/share/icons/hicolor/scalable/apps/apport.svg –text=”Screensaver is Disabled”
zenity –question –text=”Would you like to enable screensaver now?”
if [ $? -eq 0 ]
then
  killall $SERVICE
  notify-send “Screensaver Enabled”
else
  zennot
fi
}
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
  echo “42”
else
  gnome-screensaver-command -i &
  zennot
fi

Enable Screensaver
Screensaver Disabled

Categories: Bash, Linux

Fetching lyrics of songs from the terminal, an elementary web scraping bash script

February 1, 2011 1 comment

So the following will work for songs playing in rhythmbox directly. For other music players just change the first line (that is the variable “name”) to whatever it is for you. Also i am using the website http://azlyrics.com so don’t clobber them if you decide to use my script. Use the “-nc” flag with wget if you plan to use it recursively to save lyrics or something. Of course there are a lot of bugs to be worked out, like the script fails if song title consists of weird characters. And i am using the logic that the first search result is always right, which is not necessarily true and might give you wrong lyrics. I plan to write a similar code in python which will save the lyrics into the id3 rags of the mp3 itself. I’ll probably work out these bugs in that. And many thanks to the good people at stackoverflow from where i understood a lot about bash scripting.
EDIT: I added a small if loop to enable the script to take arguments from command line as well. So you can use it to search for lyrics from the command line itself. ./script “band name – song name” or just ./script to get the lyrics of currently playing song.

#!/bin/bash
if [ “$1″ ]; then
name=”$1″
else
name=`rhythmbox-client –print-playing`
fi
echo -e ‘\E[37;44m'”33[1m$name33[0m”
name1=${name/-/}
name2=${name1//  / }
searchq=${name2// /+}
link=”http://search.azlyrics.com/search.php?q=$searchq”
wget -q -U Mozilla -O 1.txt $link
awk ‘BEGIN{ RS=”<a *href *= *\””} NR>2 {sub(/”.*/,””);print; }’ 1.txt | grep /lyrics/ > 2.txt
linklyr=”`head -1 2.txt`”
wget -q -U Mozilla -O 3.txt $linklyr
cat 3.txt | sed -n “/start of lyrics/,/end of lyrics/p” | head -n-1 | tail -n+2 | sed ‘s/<br>/ /’
rm *.txt

Categories: awk, Bash, Linux, sed

Script to convert image files to cbr format in bash

January 31, 2011 1 comment

This is a simple bash script for converting all images in a folder into .cbr format (which is the standard comic book reader format). I wrote this while i was reading naruto manga. A comic book reader is much better for reading comics/manga than an image viewer and this enabled me to do just that. The script is not perfect of course, but you can tinker with it anyway you like to suit your needs.

#!/bin/bash
# get directory name to set as file name
dname=`pwd | sed ‘s,^\(.*/\)\?\([^/]*\),\2,’`
# remove any spaces from the name
name=${dname/ /}
# compress all files in folder to cbr format
file-roller -a $name.cbr *
# remove the original files
rm *.jpg

Of course there is scope for a lot of improvement, like putting in checks to ensure only image files are taken into the archive, original files of other formats are removed too and what not. For the directory name the following command can also be used.(note that i am using backticks to execute the command)

`basename `pwd“ 

But it doesn’t look as cool as the regex with sed :D. This script can also be used recursively over a large number of directories. For doing this, the method i figured was to list the directories in a text file by using a command like (modify to suit your purpose)

ls > 1.txt 

and then use the file to pick the directory paths to run the script on. For a nested directory structure, the ls command can be run multiple times on each directory till desired directory is reached. Checks will have to be placed at each step of course. Anyway, i will post a way to do this whenever i get the time to write it down. The logic seems simple enough and hence the inertia in actually testing it out :D. Happy scripting.

Categories: Bash, cbr, file roller, Linux

Hello world!

January 31, 2011 2 comments

I guess the default title serves well for my first post. This blog is more about me keeping track of the various scripts i write(mostly bash and python) than about getting something useful out there. However, i do hope i’ll be able to help some of you’ll out as google leads you here. Anyway, the here goes the first script in python:

print “Hello World!”

As you may have guessed, it prints out “Hello World!” :D. So fire up your terminal and try it out :D.

Categories: Python