View non-Mac Roman files in FileMerge

Dec 01, '09 07:30:02AM

Contributed by: peter ljunglöf

FileMerge (included with the Xcode developer tools) is a great tool for comparing different versions of text files (and PDF files, as I showed in a previous hint). But it wants the files to be Mac-Roman encoded, which is strange since everything else on a Mac uses UTF-8 nowadays. Here's how to show different file encodings in FileMerge.

This bash script uses the file utility to guess the text encoding of a file. If it is ISO-8859-* or UTF-8, then the file is transformed to Mac-Roman encoding, otherwise the file is not transformed:

#!/bin/bash
charset=`file -I "$1" | sed "s/^.*charset=//"`
case $charset in
  iso-8859* | utf-8 ) 
  iconv -c -f $charset -t MacRoman "$1"
  ;;
  * )
  cat "$1"
  ;;
esac
Create a file (e.g., convert_text_to_macroman.sh) and put it somewhere (e.g., in ~/bin). Make the script executable:
$ chmod a+x ~/bin/convert_text_to_macroman.sh
Now start FileMerge and go to the Preferences. Select the Filter tab and click in an empty row to start a new filter. Type txt in the Extension column, and put the following in the Filter column:
~/bin/convert_text_to_macroman.sh $(FILE)
Now you can try to compare two files in Latin-1 or UTF-8 encoding using FileMerge. If you want to use the filter for several different extensions, I guess you have to create one row for each extension.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091116060951378