Category Archives: Mac

Ping with timestamp

For some reason standard ICMP utilities (e.g. ‘ping’) do not support formatting the output in a way that makes it easy to do further processing. The following Perl script should work on any Mac or Linux system. It wraps the standard ping command and formats the output as comma separated values that can be directly imported into for instance Microsoft Excel.


#!/usr/bin/perl

use strict;
$| = 1;

my $host = $ARGV[0];
open PING, "ping $host |" or die "Error :$!";

while() {
 if($_ =~ m/.*seq=(\d+).*time=([\.0-9]*).*/) {
  my($s,$m,$h,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  my $dt = sprintf("%4d-%02d-%02d,%02d:%02d:%02d", $year+1900, $mon+1, $mday, $h, $m, $s);
  print "$dt,$1,$2\n"
 }
}

Download script.

Version control a Rails application with SVN

I have been looking at Git for version control but so far I am using SVN. Whenever I set up a new Rails application and add it to my version control system I do the following to ignore files that should not be version controlled.

  1. Copy the entire project to a folder within my SVN working folder hierarchy
  2. Remove all superfluous files:

  3. rm tmp/*/*
    rm log/*
    rm `find . -name '.DS_Store'`
    rm `find . -name '*.sqlite3'`
    rake db:migrate VERSION=0

  4. Add all files (svn add *)
  5. Set properties on the Rails folders to ignore certain files

  6. svn propset svn:ignore "*.log" log/
    svn propset svn:ignore "*" tmp/sessions tmp/cache tmp/sockets
    svn propset svn:ignore "*.rb\n*.sqlite3" db
    svn propset svn:ignore ".DS_Store" .

  7. Make an initial commit (svn ci -m “Initial import”)

Bye bye, Remote Desktop Client

Having to manage a bunch of Windows boxes made me download and install Microsoft’s Remote Desktop Client almost as soon as I switched back to using Mac last summer. First I used the current version but as soon as the beta of the Remote Desktop Client v2.0 came out I started using that. A couple of weeks ago I started getting this notification:

Remote Desktop Client - Out of date

However, there is no file to download. Apparently, the application timed out on March 31st but we have yet to learn from Microsoft when the proper version is due.

CoRD to the rescue. CoRD is a SourceForge hosted project to create a free remote desktop client for Mac – and it rocks.

Unless Microsoft comes up with some extremely useful features in their client, I won’t be switching back to RDC.

Riding the rails on Leopard

Mac OS X 10.5 comes with support for Ruby on Rails but the included releases were the stable releases at the time Leopard was frozen. Ruby hasn’t changed much but Rails has progressed quite a lot during that time. To make sure that your installation is up to date, do the following:

sudo gem update --system
sudo gem install rails
sudo gem update rake
sudo gem update sqlite3-ruby

This will (currently) bring your Rails version up to 2.0.2. Noteworthy changes to Rails 2.x include:

  • Sqlite3 is now the default database driver. MySQL is still available out of the box but sqlite3 makes it even quicker to set up a test project.
  • It is even more obvious that REST is preferred over SOAP for inter-machine communication under Rails.

For an extremely simple RoR solution, type this in the terminal:

$ rails TimeReport
$ cd TimeReport
$ script/generate scaffold project number:integer name:string
$ rake db:migrate
$ script/server

Then open your web browser and go to http://localhost:3000/projects. Congratulations, you have written your first Rails application.

Install wget on Mac OS X

Mac OS X comes without support for wget but this limitation is easily fixed. Start off by signing up to Apple Developer Connection and download XCode 3.0 – it’s a free but rather big download. Then open a terminal window and type:

ftp ftp://ftp.gnu.org/gnu/wget/wget-latest.tar.gz
tar -xvzf wget-latest.tar.gz
cd wget-{version}
./configure
make
sudo make install

Replace {version} with the version of wget that was downloaded (shown when unpacking the archive).

Vanishing icons on Mac OS X

Every once in a while the icons on my desktop suddenly almost disappear, leaving just the text underneath and a very tiny border where the icon should be.

Lost icon problem

The first time this happened I was quite perplexed and just couldn’t understand what I had done or how I could get the icons back. I tried rebooting but it was of no good. Then I realised that the only icons that were affected were shortcuts from Safari. Apparently, the icon cache gets messed up and needs to be fixed. The solution is simple:

  • From the Safari menu, select “Reset Safari”
  • Mark the option to remove all website icons and click “Reset”
  • Finally, Finder needs to be reset. Either log out and back in or press option-command-escape and relaunch Finder.

Lost icon solution

Fix Perl locale warning on Mac OS X 10.5

With the default settings on a Mac OS X 10.5 system, perl scripts will issue a warning that the locale has not been set properly. The warning will look similar to this:


perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LC_ALL = (unset),
LANG = "UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

To fix this issue, create a folder in you home directory called .MacOSX (if it doesn’t already exist). Create a text file in that folder, calling it environment.plist and giving it the following content:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM
"file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>LANG</key>
<string>UTF-8</string>
<key>LC_ALL</key>
<string>C</string>
</dict>
</plist>

You then need to log out and back in again for the changes to take effect.

Text encoding for Mac OS X mail application

This is not really an issue with Mac OS X but rather with the inability of Microsoft Outlook to treat text encodings properly. You are affected by this if you write emails in the mail application on Mac OS X and notice that replies to emails you have sent comes back with the replied text being all garbled. Apparently this stems from a couple of bugs in Microsoft Outlook that prevents it to treat the text encoding properly.

A simple fix to this issue is to set the mail applications to use Unicode encoding for outgoing emails. While this can be done manually for each and every email you send (Message -> Text Encoding -> Unicode (UTF-8)) it is better to change the mail application of Mac OS X to use Unicode as default encoding. To do this, open a Terminal window and type:

defaults write com.apple.mail NSPreferredMailCharset "UTF-8"

Ruby on Rails doesn't quite work on Mac OS X

Ruby on Rails is gaining in popularity when it comes to creating web sites. And no wonder, the features within the framework seems ideal for an agile development process. So naturally, I wanted to try it out on my new Macbook Pro.

Since I wanted control on the installation process, I followed the guide on www.rubyonrails.com for OS X. Everything went fine until I got the following error:

dyld: NSLinkModule() error
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Reason: image not found

To fix this problem, issue the following command:

sudo install_name_tool -change \
  /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib \
  /usr/local/mysql/lib/libmysqlclient.15.dylib \
  /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

With this change I seem to be having a fully working Rails environment.