Enable Ubuntu to discover Wi-Fi networks

I had tough time configuring Wi-Fi for different locations and switching between them, say home and office. I use Ubuntu jaunty, and I use to change /etc/network/interfaces file and restart eth0, eth1 accordingly. I have noticed my network monitor showing no connectivity always.

Also my current setup doesn’t discover Wi-Fi available networks. After searching through the internet I landed into this fine piece of information, which helped out of the hassles in switching between networks.

sudo apt-get update

sudo apt-get install wpasupplicant

sudo apt-get install network-manager-gnome network-manager

sudo vim /etc/network/interfaces

Comment out everything other than “lo”, ie “lookback address” entries in that file and save the file

sudo vim /etc/default/wpasupplicant

Add entry ENABLED=0 and save the file

Reboot your system.

Now your system automatically switch between networks.

Cut, Merge AVI files

To cut specifed duration from a AVI file, first you need to have mencoder installed (google will help you here). After installation type in the following command on you console (cmd in Windows).

mencoder -ss 00:02:00 -endpos 00:30:00 -ovc copy -oac copy in.avi -o out.avi

This command will cut in.avi of duration 30 minutes skipping first 2 minutes from in.avi.

To merge, use the following command.

cat 1.avi 2.avi > temp.avi
mencoder -forceidx -oac copy -ovc copy temp.avi -o out.avi

First command, simply joins those AVI files into one file, second command is the one makes the video playable by creating index.

Hope you find it useful.

How to: min-width/height max-width/height in Internet Explorer 6

We often find some of the handy CSS properties are not supported by IE6. It is not good idea to ignore IE6, as a web developer we need to maximize the reach of our website. For that we need to support a wide range of browsers.

Coming back to the main aim of the post, min-width, min-height, max-width, max-height are some of the properties with IE6 doesn’t support. So how we do with that? Here is how, we have to use IE’s expression in CSS to make it work for us.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script>
</script>
<style>
.pagewidth {
margin: 0 auto;
background-color: #CDCDCD;
height:20px;
min-width: 500px;
}
∗ html div.pagewidth {
width: expression( document.body.clientWidth < 501 ? "500px" : "auto" );
}
</style>
</head>
<body>
<div class="pagewidth">
</div>
</body>
</html>

By using * html div#pagewidth we are targeting IE specifically, then we are telling the browser to execute an expression to find a with for that particular element. document.body.clientWidth < 501 ? "500px" : "auto" is conditional statement, which test clientWidth and choose the value for width.

For min-height and max-height, you have to test with document.body.scrollHeight.

You can read more about this here.

Change/Switch Stylesheets with Javascript

Here is how you can change theme of a web page, without reloading that page. Using JavaScript, change the value of rel attribute in link element to something other than stylesheet, which will disable that link element. Now you have to add a new link element with reference to other stylesheet.

Simple!! below is the for doing the same.

sample.html :

<html>
<head>
<link id="ss_style1" rel="stylesheet" type="text/css" href="style1.css" />
<!--link id="ss_style2" rel="alternate stylesheet" type="text/css" href="style2.css" /-->
<script>
var css = "style1";
function changeStyleSheet(sss) {
    if(sss == css)
        return;
    var sel = document.getElementById('ss_'+sss);
    var cur = document.getElementById('ss_'+css);
    if(!sel) {
        sel = cur.cloneNode(true);
        sel.id = "ss_"+sss;
        sel.setAttribute("href",cur.getAttribute("href").replace(new RegExp(css),sss));
    }
    cur.setAttribute("rel","stylesheet1");
    sel.setAttribute("rel","stylesheet");
    document.getElementsByTagName('head')[0].appendChild(sel);
    css = sss;
}
</script>
<style>
</style>
</head>
<body>
<div>Hello Test!!</div>
<button onclick="changeStyleSheet('style1')">Style 1</button>
<button onclick="changeStyleSheet('style2')">Style 2</button>
</body>
</html>

style1.css:

div {
 background-color:#000;
 color:#fff;
}

style2.css:

div {
 background-color:#cdcdcd;
 color:#000;
}

Cut MP3 file on your own

Some times you might want to cut out your favorite background score of a music track for setting as ring tone on your mobile phone. There are number of tools available, most of them are shareware(you need to pay after limited usage).

If you have mp3 file, you can do it your self without wasting your money on that. You will need some knowledge about command prompt(cmd). Here is how you do.

First we need do install mplayer, lame.

Download mplayer from
Windows: http://www1.mplayerhq.hu/MPlayer/releases/win32/MPlayer-mingw32-1.0rc1.zip (or latest)
Linux: http://www1.mplayerhq.hu/MPlayer/releases/ (you will have to compile or search for binary package)

Dowload lame from
Windows: http://www.rarewares.org/mp3-lame-bundle.php (usually first Download link)
Linux: http://lame.sourceforge.net/ (you will have to compile or search for binary package)

Following instructions are targeted at Windows users, Linux users can transform it to there platform.

Extract/Unzip MPlayer-mingw32-1.0rc1.zip to C:\mplayer (or a location you desire, need to change commands accordingly)
Extract/Unzip lame3.98.2.zip to C:\lame

Assuming you are having mp3 file in the Desktop

Open command prompt
    Go to Start > Run, enter “cmd” (without quotes) and click OK

Change to the location of mp3 file
cd Desktop

Now type in the following commands (change your input file name, or you have downloaded latest path changes accordingly)

"C:\mplayer\MPlayer-1.0rc2\mplayer.exe" -ss 00:00:11 -endpos 00:00:20 -ao pcm:file=out.wav input.mp3
"C:\lame\lame.exe" -V2 out.wav output.mp3

Now first command needs some explanation, since you will need to cut a specific portion from that mp3 file.

-ss 00:00:10 specifies starting position, if you want to start from 1 minute and 3 second use -ss 00:01:03.
-endpos 00:00:20 specifies length, if you want to cut for 2 minute and 30 second use -endpos 00:02:30.

You will see a new file output.mp3 created on you Desktop, which the portion of audio you want to cut from a mp3 file.

Try it your self!!

Google time in any timezone

When you work with people across various region, you might run into problem of converting time to required timezone. Now google has time tool integrated to their search.
To now current time in any timezone simply type

time <location>
eg. time india, time sydney

you will get current time of location you have queried. Nice feature, helpful.

Running multiple Firefox instance

So you want to run multiple Firefox instance? Here are the steps to run multiple Firefox.

Note: Exit Firefox, if it is running.

First you have to create new profile. For that invoke Profile Manager.

For Windows, enter the following command into ‘Run’ window, and hit enter.

"C:\Program Files\Mozilla Firefox\firefox.exe" -profilemanager

For *nix, (rest of instruction provided only for windows, you can convert to your platform)

/path/to/firefox -profilemanager

You will see a window like this

Now click, “Create Profile” and give a name of your choice.

Start Firefox with newly created Profile.

"C:\Program Files\Mozilla Firefox\firefox.exe" -P "Porfile Name"

without -P option default profile will be used. Even now Firefox will not allow to open two profile, for that you have to user -no-remote option.

"C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P "Porfile Name"

will open an instance with newly created profile and

"C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P "Porfile Name"

will open an instance with default profile. Now you can notice two instance of Firefox running.

Do you want to run different versions of Firefox? Just copy “C:\Program Files\Mozilla Firefox” to “C:\Program Files\Mozilla Firefox Old” and install another version you want to run.

"C:\Program Files\Mozilla Firefox Old\firefox.exe" -no-remote -P "Porfile Name"

will start old version of Firefox. while all other shortcuts work with new version Firefox.

encodeURIComponent equivalent in Java

We know how helpful the function encodeURIComponent in JavaScript. This function comes handy when we want send unicode data via XMLHTTPRequest. This function actually encode all characters except alphabets, digits, – _ . ! ~ * ‘ ( ) to URL safe coding.

When we connect to some web service API through URLConnection, we might need to encode data similar way the browser encodes. This is where the requirement for an equivalent function in Java arise. Here is the class which I have worked upon, might be useful for you.

public class HttpUtil {

    static final String EX = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()”;

    public static void main(String… a) throws Exception {
        String input = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,./;'[]`~!@#$%^&*()_+-={}:\”<>?|\\ Google \u0baa\u0bb1\u0bcd\u0bb1\u0bbf\u0ba4\u0bcd \u0ba4\u0bc6\u0bb0\u0bbf\u0ba8\u0bcd\u0ba4\u0bc1\u0b95\u0bca\u0bb3\u0bcd\u0bb3\u0bc1\u0b99\u0bcd\u0b95\u0bb3\u0bcd – Google.com in English\n”;
        System.out.println(encodeURIComponent(input));
    }

    public static String encodeURIComponent(String input) throws Exception {
        int l = input.length();
        StringBuilder o = new StringBuilder(l * 3);
        for(int i=0; i < l; i++) {             String e = input.substring(i,i+1);             if(EX.indexOf(e)==-1) {                 byte[] b = e.getBytes("utf-8");                 o.append(getHex(b));                 continue;             }             o.append(e);         }         return o.toString();     }     public static String getHex(byte buf[])     {         StringBuilder o = new StringBuilder(buf.length * 3);         for(int i=0; i< buf.length; i++)         {             int n = (int) buf[i] & 0xff;             o.append("%");             if(n < 0x10)                 o.append("0");             o.append(Long.toString(n, 16).toUpperCase());         }         return o.toString();     } } [/sourcecode]