Jun 10, 2008

TextMate: Working with Comments

In a recent email exchange with Allan Odgaard, creator of TextMate, I asked if he had any suggestions for tips that would make for a good screencast. Allan mentioned that on the TextMate IRC, there are often questions about working with comments. And with that, he shared a list of tips that you’ll find in the video that follows.

There are five tips on working with comments, including toggling comments on/off, commenting a subset of a line, inserting comment blocks and a short section on how to insert todo lists inside a comment block.

Summary of tips:

  • Apple-/ Togggle comment
  • Alt-Apple-/ Comment a block and comment subset of a line
  • “head” tab-key Insert comment header at top of file
  • Control-Shift-B Insert comment banner (top of declaration, function, etc)
  • “todo” tab-key Insert todo block

Note: In order for these features to work, you will need to make sure the Source and TODO bundles are enabled within TextMate (see the video for more information).

The music in the video is J.J. Cale and the song: Call Me the Breeze.
Click to hear a longer clip of J.J. Cale: [audio:/2008/jj.mp3]

Sponsor: Give Good Food to Your Mac

I’d like to introduce (and thank) another sponsor of Mac Developer Tips: Give Good Food to your Mac is a two week promotion, coinciding with the kickoff of the World Wide Mac Developer Conference, that is offering an intriguing assortment of tools and utilities for developers. From database tools such as DBVisualizer to REALBasic cross-platform IDE to SourceGaurdian PHP encoder, there is assortment of interesting software.

To see what Give Good Food to Your Mac has to offer, click on the banner ad to the right or one of the links in this post (no, I am not paid on a per-click basis :) and spend a few minutes to check out some of the tools and utilities. There are discounts from 20 to 50% while the kitchen is open between the 9th and the 23rd of June 2008.

Here’s a quote from the creators of the promotion that sheds a little light into the name and intention:

No junk food, just healthy, tasty and innovative products. And because we are speaking ‘haute cuisine’ everybody gets to create their own combination of titles matching their own and unique taste.

Jun 6, 2008

At Least They Are Consistent…

I’ve been searching for an “ergonomic” keyboard recently and figured I owed it to myself to give one of the Microsoft split keyboards a go.

The keyboard is fine, if not unusual. I find myself spending a fair amount of time looking at the keys as I type since my index fingers have typically done this goofy crossing-over each other reaching for keys that should of been handled by another finger (alas, the split keyboard idea). If nothing else, my typing is much slower and more methodical. The jury is still out on whether this is the best solution of me to ease some of the stress of typing…

What I find rathering amusing is that MS pops up the following warning during the install process:

If you’ve spent any amount of time working on an MS platform, or even if not and you’ve read about the frustrations, you’ll understand the irony behind this dialog and why as a Mac user, it’s amusing.

For what it’s worth, I’ve decided to make Friday’s posts things that are not as much tips as something more on the humorous side. If you have ideas and suggestions, drop me a note.

Jun 3, 2008

Using a String as a File in Ruby

The ability to treat a string as a file can often be quite handy. Generally this most useful when classes/methods expect a file object as an input type. With Ruby, you can use the StringIO library to use a string when a file is expected, thus allowing operations such as read, write, rewind, etc.

In the lines below, the code after the #=> shows the results when executing the script. Follow the code to see several examples of a string using operations that are typically reserved for files.

require 'stringio'
str = StringIO.new %{This is a test of a string as a file. \r\n
                     And this could be another line in the file}

# Get a line
str.gets # => "This is a test of a string as a file. \r\n"

# Get the next 18 charaters
str.read(18) # => " And this could be"

# Seek to new position and read 7 more characters
str.pos = 59  # => 59
str.read(7) # => "another"

# Rewind the file and overwrite some of the existing text
str.write("Here's how to use")

# Rewind again and output the new contents
str.read # => "Here's how to use a string as a file.
     \r\n And this could be another line in the file"
str.eof? # => true

Any easy way to work with a string in a method that is expecting a file is to create a new StringIO object and pass the result to the method requiring a file type. For example:

  some_method(StringIO.new("Your string here"))

Jun 2, 2008

Using a Stack with Ruby

Although an array within Ruby supports all the operations you need for working with a stack (push, pop, etc), the problem is, Ruby does not offer a “formal” stack object and therefore the flexibility of the Ruby array can present problems. For instance, even if you intend to treat an array as a stack, nothing will stop you (or another developer using your code) from inserting or deleting anywhere within the stack.

We can work around this by creating a simple Stack class as follows:

class Stack

  def initialize
   @the_stack = []

  def push(item)
    @the_stack.push item

  def pop

  def count

Here is how you might use the stack:

  stack = Stack.new

If need be, you could also add a few convenience methods such as clearing the stack or looking at the last element (without popping it off the stack):

  def clear

  def look

Give this a try if you need to work with an array as a stack and want to play within the rules of how a stack is implemented as a traditional data structure.

May 30, 2008

MacDevTips Widget: Version 1.0

For several months I’ve been meaning to build a Dashboard widget. There are several reasons for this endeavor – First, learning new tools and languages is a hoot. Second, widgets are your friend and won’t be going away anytime soon (read, it’s always good to keep up with technology). Finally, I’m all about sharing interesting code, tips and tricks through collaborative (open source) development. Read on for more about that last point.

I plan for this to be an on-going series, with each new post offering a new version of the widget. With each new release I will include all code in the form of a Dashcode project, which will offer a means for you to import the project, poke around to better understand how to build widgets and modify the code as you see fit. Future releases (and the accompanying blog post) will include descriptions, code snippets, screenshots and videos (screencasts) demonstrating concepts from visual layout to code examples to debugging.

Version 1.0 looks as follows, it’s quite simple, from both the feature perspective and development point of view – an image across the top, a listing of recent blog posts, and an option to get more information (clicking the little in the lower right flips the widget to show the reverse side).

I want this to become a community built widget. I’ll run with the first few releases, however, to take this widget to the next level here are a few things where I would welcome some expertise:

  • Artwork for a logo (see the Top 50 Widget List for some nice examples)
  • JavaScript / Dashcode expertise to help create unique & interesting functionality

All contributors will be acknowledged on the back of the widget. For example, as of release 1.0, the flipside looks as follows:

Depending on how this project progresses, and the support I can rally around extending the widget, at some point it may make sense to move this to Google Code as a community project. We’ll worry about that another day…

Without further ado, let me introduce the inaugural MacDevTips widget. The files included in the 1.0 release:

You have two options to give the widget a try:

  1. Install the widget on your Dashboard (download, unzip and double-click the widget)
  2. Run the widget from Dashcode (download, unzip, open and run the project within Dashcode)

In the next post on this widget I’ll demonstrate through a screencast how I created the widget in Dashcode. I’ll also bump the release to 1.1 by adding code to resize the widget.

It’s always good to have a goal, and for this widget I think we should shoot for landing in the top 50 of the Apple Dashboard Widget repository. With the right mix of features and an open source approach to enable others to learn/build widgets, I have no doubt we’ll reach that goal sooner rather than later.

If you’d like to get involved in building an upcoming release of the MacDevTips widgets, drop me a note and we’ll get things rolling!

May 29, 2008

Comparing Directories

The typical use of diff from within a terminal (using the default Bash shell) is to compare files. However, with a little slight of hand, known a process substitution, we can use diff to compare the contents of two directories.

The syntax looks as follows:



Commands represents a single command (such as find) or a piped list of commands. The end result of using process substitution is that the commands act as a file. In the first example, the process substitution results in what looks like a file that can be fed into another command. The second example is the opposite, the process substitution enables one or more commands to act as a file for input. Let’s look at how we can use process substitution for comparing directories.

Let’s say we want to compare the contents of the two directories above. The screenshot was captured from Finder, and in this case, provides a concise view of each directory. However, if the directories are not as conveniently located (in the same sub-directory) or have many more files, or if you need to compare directories from within a terminal, then you could run the following from a terminal:

$ diff <(find tmp1) <(find tmp2)

Here we use the find command to walk through the directories. Using the process substitution, the results of the find command look as though they are files, which enables us to feed both into diff.

You can learn more about process substitution on the Bash man page at the Apple Developer Connection.

May 28, 2008

TextMate: HTML Tag Completion

The HTML bundle in Textmate offers a wide selection of features for formatting, working with tags and previewing within a browser. One of the options that offers great mileage is Insert Open/Close tag with Current Word. If you have been working with TextMate and HTML for any length of time, this tip won’t be new. However, if you are new to either, I think you’ll find this little trick quite helpful.

The tip is all about working with tags, essentially offering an automated means to insert matching tags and intelligently placing the cursor based on the tag type. Once you get used to using this trick, you’ll spend a lot less time trying searching for that unmatched tag in your source, resulting in valid HTML the first time around.

The music in the video is J.J. Cale and the song: Call Me the Breeze.
A longer clip of J.J. Cale jamming: [audio:/2008/jj.mp3]

Click the image to see more about about JJ and the CD (at Amazon)

May 22, 2008

Chm Viewer – Chmox

If you’ve ever bumped into a Microsoft Compiled HTML help file (the giveway is the .chm extension) and resorted to starting up a virtual machine in order to run an instance of Windows to view the file, there’s a better way…a chm viewer for Mac OS X.

Chmox is an open source application that displays chm files on Mac OS X. Viewing a file using chmox is as simple as downloading and extracting a dmg, pointing at a chm file and you are good to go.

Chmox is written in Cocoa (Objective-C) and uses both WebKit, and chmlib. If you are looking to dig a little deeper into how to work with chm files, Matthew Russotto’s site is also worth a visit. Matthew has included information on the chm file format, which you can find here.

A good way to learn more about how chmox works is to spend some time viewing the source files, which you can peruse on the chmox project at sourceforge.

May 21, 2008


I recently bumped into a clever tool for soliciting debugging help. Pastebin is a web-app where you upload a snippet of code for others to review. Viewers can makes changes and suggestions to the code in a separate window. There are some handy features including syntax highlighting and the option to callout specific lines of code.

Here’s how it works. First, you paste a block of code into a textbox, and choose the language for syntax highlighting. Enter a name and click Send.


Here is what the code looks like once uploaded to Pastebin.

Pastebin generates a URL that you can use to point others at your code block. People can make suggestions on your code, including calling out a specific line using @@. See the example below where I entered a comment and made a minor code change.

Once the change is saved, Pastebin updates the code block to look as follows:

One additional trick you can do with Pastebin is create a sub-domain for your own specific debugging needs. For example, if I type in http://macdevtips.pastebin.com, I get a private area for working with code snippets. This area is still viewable by anyone (who knows or guesses the sub-domain), however, posts are not shown in the “Recent Posts” section at the homepage of Pastebin (it’s easiest to understand by simply giving it a try).

Pastebin is a nice tool when you need another set of eyes to look at your code.

May 13, 2008

Reading Email as Plain Text

Well, I haven’t given up on the mission to determine how to capture a screenshot of the login screen in Mac OS X. Read more here. In fact, I’ve been able to create a popup login window with a user name and capture a screenshot, however, it’s not exactly what I’m looking for. More to come…

In the meantime, I want to share a tip if you tire of email messages in iMail where the body of the message has been created in rich text. I’ve got a trick for you to turn email that looks like:

to email that looks like this:

Ah yes, good old plain text. Here’s the trick, from a terminal type the following:

defaults write com.apple.mail PreferPlainText -bool TRUE

That’s all there is to it. And of course, if you change the TRUE to FALSE, you’ll be back in the world of colorful, formatted messages.

May 9, 2008

Working as Root

Root is the uber system account. Although handy at times (for example when installing applications), it’s generally recommended that root not be used as your everyday login. What follows are some options for working as root.

If you need to run a command as root, you can use the sudo command. For example, to run the script for daily system maintenance, you can run this command (the $represent the terminal prompt):

$ sudo periodic daily

Now that’s all well and good, however, it’s generally applicable only for one command. What if you are in a terminal and want to login as root to do some larger scale maintenance or system work? Try this:

$ sudo -s

This command will enable the root account and update your prompt as shown in the figure below:

The above screenshot assumes you are using the bash shell and have admin rights on the account you are logged in with. The $ typically represents a user account whereas # represents root.

The last option is to create/enable a root account, thus you can login (when restarting/rebooting) as root. This approach offers the most flexibility and power, however, use with caution as there are no limits to what (damage) you can do.

Root account option #1: Enter the following from a terminal:

$ dsenableroot

You’ll be prompted for your (current) password, as well as the new root password.

Root account, option #2: Enter the following from a terminal:

$ sudo passwd

You’ll be prompted for the same series of passwords as above.

Root account, option #3: Follow the steps below:

- Start the Directory Utility application (/Applications/Utilities/Directory Utility)
- Click the lock icon in the lower left corner (to unlock it)
- From the Edit menu (across the top of screen), select Enable Root User

To wrap up this post I was planning to show you how the login window changes when a root account is enabled. Unlike other accounts on your system where a name is typically displayed as one of the login options, the root account is not listed as one of the options (for security reasons).

However, getting a screenshot of the login screen is a considerable feat. This is a great segue to a post coming next week where I’ll show the tricks that I tried for capturing the elusive login screen and I will pose an intriguing challenge for you…

May 7, 2008

Dashcode 2, Beta

If you are interested in working with the next beta of Dashcode, although there is no public announcement of the beta, it is available, read on for the details…

If you are not familiar with Dashcode, it’s a development tool created by Apple for building Dashboard widgets. The 1.0x release was bundled with Leopard. Dashcode is an impressive application, with drag-n-drop support and a no-coding option for creating a widget. Of course, as with any “no coding required” statement there is the unstated disclaimer that there are limitations on how far you can take a widget without digging into some code. And on that note, Dashcode offers excellent support for both writing and debugging code that make up a widget (CSS, HTML and JavaScript).

To give you an idea of what you can do with Dashcode, the widget below is an RSS feed of this blog that was created with version 1.0x.

You can try Dashcode 1.0 if you are running Leopard by installing the developer tools on the Leopard install DVD.

Interested to try the 2.0 Beta? There isn’t a download solely for Dashcode, however, if you download the iPhone SDK, Dashcode is one of many tools included in the download. If you do not have an Apple ID, follow the link near the bottom of this page to download the iPhone SDK, where you’ll find an option to create an ID.

Important note: if you import a 1.0 project into the beta, be aware that you might be able to work in the project with 1.0 if you save the project. To get around this, save any imported project with a new name (and/or location). An additional suggestion is to install the iPhone SDK in a different directory than the default /Developer. I opted to install the beta into /Developer/Beta such that I can use both versions of the tools.

In a screencast to follow, I’ll show you how I created the widget above with Dashcode 1.0.

May 6, 2008

Converting Images Types

I continue to find the Preview application to be a handy little tool. It’s not that Preview supports an enormous feature set for working with images, as much as each time over the last few months that I’ve look to the Previewer to help me out, it did.

The first time this came about I simply needed to resize an image to post on this blog – from the Tools menu, click Adjust Size and save the file, that’s it, done (and of course it can preserve the aspect ratio). My most recent encounter was when I needed to convert a file type from JPG to GIF. Again, a few clicks and it’s done – open an image, from the file menu choose the Save As option and from the dialog box select the image type. The figure below shows the supported file types; notice you can even save an image as a PDF.

By no means a full-fledged image editing program, and never intended to be, Previewer is still much more than its name suggests.

May 5, 2008

Google App Engine, Free Preview

Google App Engine provides an opportunity to leverage Google’s infrastructure for server side web applications. The platform is built on a Python runtime, includes persistent storage as well as the capability to integrate with existing Google applications (think Google Maps, Gmail…).

The App Engine SDK is an open source project that is hosted on Google code, you can can access the project here. A download of the SDK is available for Mac here.

The App Engine SDK includes a web-server application that provides a means to emulate the App Engine services from within a local development environment. Once deployed, applications can be hosted on the appspot.com domain, or your own domain.

The video below is a good introduction to Google App Engine from a recent CampFire One event:

One of the more intriguing aspects to services like the Google App Engine is that you focus on the application, not the hosting/scaling. Should you build a killer app, your focus still lies on the application itself, not on how to massively scale (which would be a good problem to have). From the App Engine homepage

This is a PREVIEW RELEASE of Google App Engine. For now, account registrations are limited to the first 10,000 developers, and applications are restricted to the free account limits.

Hurry and give it a go…