Occasionally I need to edit system files that belong to root and I found that it can be quite annoying as I always end up using nano in the terminal as it doesn’t seem possible to elevate the privileges of an application from Finder.  For example if I wanted to edit my /etc/hosts file then usually I’d have to drop to a shell and type:

sudo nano /etc/hosts

The problem with that is that I have a nice looking graphical operating system here and although I love the fact that it uses unix under the bonnet, I really much prefer to edit my files in TextEdit rather than a console based application like nano.  I could use the open command like this:

sudo open -a TextEdit /etc/hosts

but then I just have my default user privileges and I can’t save it, even though I used sudo!  The correct solution is actually to use:

sudo open -a /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

The reason for this is that an application in OS X is actually a folder that contains all the necessary files and resources that it uses, and the executable file itself is buried in a couple of subfolders.  Neat and tidy, but a bit of a pain to type in the terminal!

I decided I’d make my own sudo / open script which I creatively named sopen.

#!/bin/bash

if [ $# -lt 1 ]
then
  me=`basename $0`
  echo "Usage: $me [OSX Application]"
  exit
fi

myPath=/Applications:/Applications/Utilities:/Developer/Applications:$PATH
myPath=\"`echo $myPath | sed 's/:/" "/g'`\"

# Check our custom path and execute the first match
for thisPath in $myPath
do
  thisPath="`echo $thisPath | sed 's/\"//g'`"
  app=$thisPath/$1.app/Contents/MacOS/$1

  if [ -x "$app" ]
  then
    # We found it!
    shift
    sudo -b "$app" $*
    exit
  fi
done

# We didn't find anything.
echo 'Sorry, application not found.'

I saved that file to /usr/local/bin/sopen (using nano :( ) and then gave it execute permission using chmod.

chmod -x /usr/local/bin/sopen

I could have also granted the execute permission from Finder but I was in a terminal anyway.  Now, although I still need to drop to a terminal in order to edit my system files, all I need to type is

sopen TextEdit /etc/hosts

My script will look for the specified program in /Applications, /Applications/Utilities, /Developer/Applications and then continue on down the regular $PATH.   It’ll execute the first correctly-named program it finds and will pass to it any extra parameters that I specify on the command line.  So the above example will correctly sudo /Applications/TextEdit and will pass it /etc/hosts.

This is just a very simple script and it could be expanded on to perhaps work a bit more like open and use LaunchServices to find a default program to open a certain file type but I’m happy with the way it is.  Maybe others will find it useful too.  Perhaps when I get time I’ll write a Finder plugin to let me elevate from a popup menu and then I can do away with the console completely.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks