UTF-8 Converter


The other day at work, I needed to batch convert about one or two hundred files formatted in MACROMAN format to UTF-8. Well, it turns out there is a command line utility to do just this called iconv. I was very pleased when I found that because it was going to save me a lot of time. Then I ran it and got confused. It turns out that iconv does convert text format, but it doesn’t write it back out to a file, it just spits the results back into the terminal window. Mildly frustrated, I decided to take matters into my own hands and write a script that would take the output and put it back into a file with the same name. These are the results:

#!/bin/bash

for f in $1/* ; do
o=`basename $f`
if file $f | grep Unicode ; then
cp $f $2
else
iconv -f MACROMAN -t UTF-8 $f >$2/$o
fi
done

I went further and added options,  a debug mode, verbose mode, and the like, and even a man page! The syntax is:

# roman_to_utf8 [options] <input> <output>

The input and output can be either directories or individual files.

#!/bin/bash

usage() {
echo Usage: $0 "[-v | --verbose] [-d | --debug] [-e | --encoding <encoding>] <input> <output>"
exit 1
}

VERBOSE=false
ENCODING=MACROMAN
DEBUG=false

while true; do
case $1 in
-v | --verbose) VERBOSE=true;;
-d | --debug) DEBUG=true;;
-*) echo "Bad option $1"; usage;;
*) break;;
esac
shift
done

SOURCE="$1"
DESTINATION="$2"

if [ $DEBUG = true ]; then
echo VERBOSE = $VERBOSE
echo SOURCE = $SOURCE
echo DESTINATION = "$DESTINATION"
echo ENCODING = "$ENCODING"
exit
fi

if [ "x$SOURCE" = x -o "x$DESTINATION" = x ]; then
usage;
fi

convert() {
INPUT="$1"
OUTPUT="$2"
FILENAME=`basename "$INPUT"`
if file "$INPUT" | grep Unicode ; then
cp "$INPUT" "$OUTPUT"
$VERBOSE && echo "Successfully copied $FILENAME"
else
iconv -s -f $ENCODING -t UTF-8 "$INPUT" >"$OUTPUT/$FILENAME"
$VERBOSE && echo "Successfully converted $FILENAME"
fi
}

if [ -d "$SOURCE" ]; then
for INPUT in "$SOURCE"/* ; do
convert "$INPUT" "$DESTINATION"
done
else
convert "$SOURCE" "$DESTINATION"
fi

exit

Please excuse the poor tabbing due to wordpress. In any case, it worked and saved me a ton of time. All you need to do is copy this script into a file and make it executable. Enjoy!

Chromium for Mac

So, I’ve stated how I feel about web browsers. Very few are devoid fatal flaws that bug me to the point of being unable to use them. To that end, I decided to take matters into my own hands by downloading and compiling my own version of Chromium.

As previously stated, Chromium has proven to be an excellent browser. It is minimal, functional, fast, expandable. The only issue I could find with it was it’s lack of conformity with the rest of Mac OS X in terms of handling selections in the location bar. The developers of Chromium decided to follow the convention of single click selects all in the location bar, following the cue of Internet Explorer. This however is contrary to the functionality of similar text fields in pretty much every other place in the Mac OS, including that of Safari. This incongruity was beyond my ability to cope, rendering the browser useless to me.

I pleaded my case, along with many others, at the Chromium developer forums, however they said it was a non-issue, and marked the bug as “WontFix.” So, I decided to make my own version with this deliberate behavior remedied. So I downloaded the source. 6.4 GB later I located the 6 offending lines of code, spent 1.5 hours making a cool Mac-themed icon, and spent 2.5 hours compiling on all 8 processors. The outcome? Success! Now, I will offer this for download, and as long as it continues to be usable and a good browser, Chromium  may very well replace FireFox for the first time in 10 years as my browser of choice.

Understanding Time Machine and Sparse Bundles

Time Machine has been one of my favorite features about the Mac. It was the most anticipated feature for me when waiting for the release of Mac OS X 10.5. It has saved my behind a few times from disk failure with nary a single lost file. Apple describes Time Machine as follows:

Time Machine works with your Mac and an external hard drive. Just connect the drive and assign it to Time Machine and you’re a step closer to enjoying peace of mind. Time Machine will automatically back up your entire Mac, including system files, applications, accounts, preferences, music, photos, movies, and documents. But what makes Time Machine different from other backup applications is that it not only keeps a spare copy of every file, it remembers how your system looked on a given day — so you can revisit your Mac as it appeared in the past.

Recently I have been switching to backing all my computers up to my server and had questions about using sparse bundles and backing up over the network. Unable to find a complete set of instructions, I have decided to share what I have learned.

Backing up with sparse bundles

What is a sparse bundle? Its a type of disk image that can have a maximum size set, but only takes up as much space as it needs to on the physical disk. Time Machine is a notorious hog of disk space. It will utilize as much as it can for backing up your files, which can be nice. Sometimes though, it can be undesirable. The beauty about backing up to sparse bundles is that if you have a large disk you are backing up to, say 2 TB, and your drive on your computer is only 200 GB, you can make a sparse bundle that is 500 GB to back up to and basically reserve the remaining 1.5 TB of the backup disk for other uses.

So how do you set up sparse bundles? Well, the first step is to create one with Disk Utility.

  1. Open Disk Utility
  2. Select File > New Blank Disk Image
  3. Set the file name to your computer name followed by an underscore and it’s primary mac address (eg: “macbook_00113bcd9e”)
  4. Set the size to custom, then choose an appropriate size. It should be at least the size of your disk, and probably half again or twice as big.
  5. You can set encryption on the file. This is so no others can access your data if your backup disk gets stolen, etc. 128 is probably good enough. It will be slower with 256 bit encryption.
  6. Keep it partitioned as an “Apple Partition Map”
  7. Select “Sparse Bundle Disk Image” as the Image Format

Now save it on whatever disk you want to use as your backup. Make sure you save it in the top-level directory of the drive. Time Machine can’t locate the sparse bundle unless its in the top-level directory.

Time Machine Error

I saw this error on my server. When I searched on Google, I couldn’t find any information about the cause or solution to the problem.

“Time Machine could not be configured. The built-in network interface could not be found.”

This error stems from the fact that Time Machine identifies the computer based on it’s built in network adapter’s mac address. Computers that have this error are either “hackintoshes” that have no built in ethernet adapter, or have damaged hardware and are using an external adapter or PCI card. In either case, I have no solution for the problem yet, but will update this post if I find it.

Scheduling

Time Machine is wonderful because of its incremental nature. It saves backups hourly. This can be a nuisance however when backing up over the network because there can be a noticeable slow down of the computer during backup. To remedy this problem, I found a program called TimeMachineScheduler that allows the interval between backups to be specified as well a times that Time Machine shouldn’t run.

© 2007-2015 Michael Caldwell