I often find myself loading test files from my local filesystem into
Firefox with "file://" URLs. Unfortunately Firefox on OS X
(or at least my local build of 3.1) is maddeningly inconsistent about
recognizing an argument as a filesystem path or a URL. So for example "ffz
test.html" ("ffz" is my launch script) usually opens "file:///Users/cbartley/Dev/mozilla/test.html"
(which I want) but sometimes tries to open "http://www.test.html/"
(which doesn't even make sense).
It seemed sensible to switch to simply supplying a full file URL on the
command line so there's no confusion. The downside is that file URLs
need to be absolute and constructing an absolute file path on the fly
is a pain. Fortunately, I can make the computer do this work for me
with a handy bash function. The "fileurl" function
below takes a path to a file (usually a relative path) and prints the
corresponding "file://" URL for it. So for example:
fileurl ../test.html
might print
file:///Users/cbartley/dev/mozilla/test.html I can invoke my test build with something like
ffz `fileurl ../test.html`
and have it reliably open the file every time. I haven't yet rolled fileurl into the ffz script, but that's the logical next step. I should also acknowledge that there may well be better ways to do this than the approach I've used -- I am no bash expert. Finally, for all those other amateur bash programmers out there, the magical Google phrase for finding documentation on the weird things you can do with variables in bash is "variable mangling". # Given a path to a file (relative or absolute), print a "file://" URL for
# that file.
fileurl()
{
# Split the directory name out of the argument path.
# "/dir/subdir/file" ==> "/dir/subdir"
# "dir/subdir/file" ==> "dir/subdir"
# "subdir/file" ==> "subdir"
# "file" ==> "."
TEMP="/$1" # Hack: Prepend a slash so there's at least one
TEMP="${TEMP%/*}" # Chop off the trailing "/file"
TEMP="${TEMP#/}" # Remove the leading slash if it's stil there
DIRNAME="${TEMP:-.}" # If DIRNAME is empty, set it to "." # Get the base file name from the argument path.
BASENAME="${1##*/}" # Remove everything up to the last slash, inclusive # Convert the directory name to an absolute path.
ABSDIRNAME=$(cd "$DIRNAME"; pwd) # Echo the file URL built from the components.
echo "file://$ABSDIRNAME/$BASENAME"
}
fileurl ../test.html
might print
file:///Users/cbartley/dev/mozilla/test.html I can invoke my test build with something like
ffz `fileurl ../test.html`
and have it reliably open the file every time. I haven't yet rolled fileurl into the ffz script, but that's the logical next step. I should also acknowledge that there may well be better ways to do this than the approach I've used -- I am no bash expert. Finally, for all those other amateur bash programmers out there, the magical Google phrase for finding documentation on the weird things you can do with variables in bash is "variable mangling". # Given a path to a file (relative or absolute), print a "file://" URL for
# that file.
fileurl()
{
# Split the directory name out of the argument path.
# "/dir/subdir/file" ==> "/dir/subdir"
# "dir/subdir/file" ==> "dir/subdir"
# "subdir/file" ==> "subdir"
# "file" ==> "."
TEMP="/$1" # Hack: Prepend a slash so there's at least one
TEMP="${TEMP%/*}" # Chop off the trailing "/file"
TEMP="${TEMP#/}" # Remove the leading slash if it's stil there
DIRNAME="${TEMP:-.}" # If DIRNAME is empty, set it to "." # Get the base file name from the argument path.
BASENAME="${1##*/}" # Remove everything up to the last slash, inclusive # Convert the directory name to an absolute path.
ABSDIRNAME=$(cd "$DIRNAME"; pwd) # Echo the file URL built from the components.
echo "file://$ABSDIRNAME/$BASENAME"
}