Thursday, June 14, 2007

From Safari to Firefox, and back again!

Well, I have to say, I love the new Safari 3.0 beta.

I was an avid Safari user until a few months ago. I switched to Firefox for the following reasons:

  • Asks before closing a window with tabs
  • Able to reopen tabs after crash (with the Session Manager add-on, it even saves text you were typing in forms!)
  • Allows opening a tab in a new window (but needs to refetch the page)
  • Less annoying download status (with the Download Statusbar plugin)
  • Absolutely kick-ass website debugger: the Firebug add-on.
  • Extensibility through add-ons and themes
  • Works with slightly more websites
So I switched, and suffered through the things that aren't so nice about Firefox, such as no real integration with OS X (Keychain, application launching etc), some graphical glitches and general clunkiness.

Consider me biased, once you're used to being given applications that Just Work and do the sensible thing 99.9% of the time, you become, shall we say, sensitive to less well thought-out applications. Firefox is an impressive effort, but in part due to its cross-platform requirements, it's somewhat lacking on OS X. Safari goes the extra mile for you, like slowing down Flash animations when you're not looking at them, and waiting until you look at a tab to activate animations.

And now Apple released version 3.0 beta of Safari, for free, including a Windows version! Magically, Apple's engineers have seen the things I disliked about Safari 2.0 and fixed them:

  • Asks before closing a window with tabs
  • Able to reopen tabs after crash (from the History menu)
  • Allows opening a tab in a new window, you can just drag it off the bar (without refetching!)
  • You can even move a window back to a tab bar, all with spiffy animations of course
For extra chunkyness, they added a Web Inspector as well, which more or less shows what Firebug would show. All you need to do to see it is enable the Safari debug menu and it's right there. Firebug is still slightly more useable, and it allows you to change the page while it's loaded, but still kudos to Apple for adding it!

Safari 3.0 has those little touches you get from Apple, for example extra-visible highlighting when searching for something, or resizeable text boxes for when those forms just aren't big enough. Very nice.

Heartily recommended. Download it now.

Friday, May 25, 2007

Global Warming

Here's an extremely interesting video to watch:
The Great Global Warming Swindle
It makes a case for the following two statements:
  1. Yes, the Earth is obviously getting warmer
  2. No, humankind doesn't have all that much to do with it
It shows very convincing science which indicates man-made CO2 has nothing to do with global warming. It looks like we're just in it for the ride.

But, on the other hand, check out this link for a thorough debunking of the video.

Monday, May 21, 2007

Using bash functions under sudo

Update: Now on github

Have you ever done the following?

$ type duk
duk is a function
duk ()
{
   du -k "$@" | sort -n
}

$ sudo duk /tmp
sudo: duk: command not found
duk is a function that will show you directory sizes under a given pathname (or the current directory), nicely sorted by size, largest at the bottom just above your next prompt. It's very handy, put it in your .bashrc ;-). sudo doesn't know what to do with "duk" however, since it's not a system command. Therefore, I wrote a function that is a front-end to sudo. It parses the command line you give it, and expands any functions or aliases that you call. For bonus points, it shows you the full command line as the shell receives it before you type your password. Use it as follows:
$ source sudo.bash
$ sudo duk /tmp
/opt/local/bin/sudo -- bash -x -v -c duk ()
{
   du -k "$@" | sort -n
};"duk" '/tmp'
Password:
[...]
3648    /tmp/synergy-1.3.1/lib
6184    /tmp/synergy-1.3.1
20128   /tmp/prarora
1294048 /tmp/tmp
5304016 /tmp
Looks like I'll need to do some cleaning up in /tmp. Full source follows. To use it, place a source ~/.sudo.bash (for example) in your .bashrc after copying the code to your ~/.sudo.bash, or copy the full code to your .bashrc. Then, simply use sudo as you would before. This function handles all sudo arguments. There is one extra argument, -x, which expands arguments as you, not as root. This is needed in some corner cases. (Update: New version which no longer uses sed and handles spaces in sudo options!)
# Wrap sudo to handle aliases and functions
# Wout.Mertens@gmail.com
#
# Accepts -x as well as regular sudo options: this expands variables as you not root
#
# Comments and improvements welcome
#
# Installing: source this from your .bashrc and set alias sudo=sudowrap
#  You can also wrap it in a script that changes your terminal color, like so:
#  function setclr() {
#    local t=0               
#    SetTerminalStyle $1                
#    shift
#    "$@"
#    t=$?
#    SetTerminalStyle default
#    return $t
#  }
#  alias sudo="setclr sudo sudowrap"
#  If SetTerminalStyle is a program that interfaces with your terminal to set its
#  color.

# Note: This script only handles one layer of aliases/functions.

# If you prefer to call this function sudo, uncomment the following
# line which will make sure it can be called that
#typeset -f sudo >/dev/null && unset sudo

sudowrap () 
{
    local c="" t="" parse=""
    local -a opt
    #parse sudo args
    OPTIND=1
    i=0
    while getopts xVhlLvkKsHPSb:p:c:a:u: t; do
        if [ "$t" = x ]; then
            parse=true
        else
            opt[$i]="-$t"
            let i++
            if [ "$OPTARG" ]; then
                opt[$i]="$OPTARG"
                let i++
            fi
        fi
    done
    shift $(( $OPTIND - 1 ))
    if [ $# -ge 1 ]; then
        c="$1";
        shift;
        case $(type -t "$c") in 
        "")
            echo No such command "$c"
            return 127
            ;;
        alias)
            c="$(type "$c")"
            # Strip "... is aliased to `...'"
            c="${c#*\`}"
            c="${c%\'}"
            ;;
        function)
            c="$(type "$c")"
            # Strip first line
            c="${c#* is a function}"
            c="$c;\"$c\""
            ;;
        *)
            c="\"$c\""
            ;;
        esac
        if [ -n "$parse" ]; then
            # Quote the rest once, so it gets processed by bash.
            # Done this way so variables can get expanded.
            while [ -n "$1" ]; do
                c="$c \"$1\""
                shift
            done
        else
            # Otherwise, quote the arguments. The echo gets an extra
            # space to prevent echo from parsing arguments like -n
            while [ -n "$1" ]; do
                t="${1//\'/\'\\\'\'}"
                c="$c '$t'"
                shift
            done
        fi
        echo sudo "${opt[@]}" -- bash -xvc \""$c"\" >&2
        command sudo "${opt[@]}" bash -xvc "$c"
    else
        echo sudo "${opt[@]}" >&2
        command sudo "${opt[@]}"
    fi
}
# Allow sudowrap to be used in subshells
export -f sudowrap

Saturday, January 6, 2007

A cute desktop clock for OS X

One of the reasons I love OS X so much is that it makes it so easy to do cool things with Unix. In today's post I'll show how I made a desktop clock using GeekTool and the date command. GeekTool is an OS X application that will render text files, images and shell command output into the desktop. Its website isn't really updated a lot, but it still works, and if it doesn't, its source is out there. I guess it got mostly superseded by the widgets of the Dashboard, but it renders into the desktop, which Dashboard won't do without some hacks. (Although that might change next week if/when Leopard comes out :-) ) I also used this great free font I found, Atomic Clock Radio. To set it up, I created a rounded background (using The Gimp of course), and told GeekTool to put that on the right side of my screen with a pleasing opacity (in the style tab).
Then I added the output of the command date "+%H%n--%n%M", updating every 20 seconds, in another item and told it to render with the Atomic Clock Radio font. That command renders a 24h clock with embedded newlines. The dashes looked most pleasing. Experiment, it's easy.
That's all there is to it. Simple tools with big impact, that's how I like my Unix ;-) You can get the background image I used here. It's rounded on all sides, so you can choose where you put your clock.

Tuesday, January 2, 2007

Control iTunes over SSH

So I was looking for a good way to control iTunes running on my Mini mac at home. There's some payware options, like netTunes and webRemote, and some freeware options, like zTunes and iTRC. I kind of prefer using open source if I can get it, so I am giving the payware options a miss for now. Unfortunately none of the freeware options are cross-platform and secure at the same time. On top of that, most of the them are quite buggy. So while I wait for zTunes to become faster, less buggy and more secure, I chose the third option, which is Write Your Own :-) Thanks to the wonders of Applescript, iTunes is very accessible from other programs. You can basically get information about anything in your library and perform almost all the actions that you can perform from the program itself. So I wrote a remote control in bash, to be accessed over SSH. I present to you iTunesCLI 1.0. To use, save this script where you can find it again on the system that you want to use it from, for example in your home directory or in /usr/bin. Make it executable with chmod +x Then ssh to that system and run the script:
ssh -t mysystem ./iTunesCLI (when stored in your home directory)
ssh -t mysystem iTunesCLI (when stored in /usr/bin)
The -t option is needed to make keystrokes be recognized immediately. SSH tips:
  • Use an SSH agent so you don't have to type your password every time. For OS X, try for example SSH Keychain
  • The hostname for the remote machine is most likely machinename.local. For example, my Mac Mini is hooked up to the stereo and is called mini-me. I just run "ssh -t mini-me.local iTunesCLI".
I had to come up with some nifty tricks to make it fast and pretty. Look at the script file to see bash arrays, IFS swizzling, printf builtins, tput abuse, multiline commandlines embedded in backticks and a mangled model-view-controller concept, oh my! I sprinkled it with comments in the hopes of making it maintainable :-) Enjoy!