Monday, April 4, 2011

Quick script to copy text to Android mobile

Hi All,

I wrote a small script to quickly copy some text from linux computer to an android mobile.
To use it,
  • Install Barcode Scanner in your android mobile from here (This is a must app for any android mobile)
  • Copy the below code to a file named barcode.sh and give it executable permissions,
  • Create a Custom application launcher in the top panel to launch the saved script.
Now when you want to copy some text,
  • Using mouse, select the text or url you want to copy.
  • Click on the launcher you created.
  • Scan the barcode displayed on the screen using your android mobile. :-)

#!/bin/bash

#Following function is copied from http://www.unix.com/shell-programming-scripting/105600-shell-uri-encoding.html
#@ uri_escape - convert STRING to hex-encoded string
#@ and assign to optional VAR or _URI_ESCAPE
#@ Author: Chris F.A. Johnson, 2009-03-25
##
uri_escape ()
{
local string x;
string=$1;
var=${2:-_URI_ESCAPE};
while [ -n "$string" ]; do
case $string in
[_a-zA-Z0-9]*)
printf -v x "%s%c" "$x" "$string"
;;
*)
printf -v x "%s%%%X" "$x" "'$string"
;;
esac;
string=${string#?};
done;
eval "$var=\$x"
}

data=`xclip -o`
uri_escape "$data" data
uri="http://chart.apis.google.com/chart?cht=qr&chs=350x350&chl=$data"

#Replace eog with your image viewer application if you are not using gnome desktop.
eog "$uri" &> /dev/null &