Fixing Python on Mac OS X

My Macbook has been upgraded through several versions of Mac OS X and the account has migrated over 2 machines. This has led to multiple versions of Python being installed on the system. I never noticed until I tried to use Mercurial and it fell over complaining about the versions of Python things.

Looking in the folder /Library/Python shows that I’ve got 4 different versions installed, 2.3, 2.5, 2.6 and 2.7. Running Python from the command line launches version 2.5. The fix, courtesy of this stack exchange, is to run the following in terminal:-

defaults write com.apple.versioner.python Version 2.6

There’s a bit more information in the linked answer if the above doesn’t help. It did the trick for me though, so happy days!

How Sublime Text 2 saved me from madness

Let me begin this post by cutting straight to it. Sublime Text is awesome. I’ve had it installed on my computer for ages and cracked it out every now and again to edit some simple text documents, but never really used it in anger. Recently I’ve been working on a project  on Linux which is composed of various bits of technology written in different languages. I was spending my days bouncing between a combination of C++, Pro/C, Perl scripts and PL/SQL, hopping backwards and forward between various command line tools and IDEs. It was becoming a bit of drag as having to constantly switch modes all the time was breaking my stride and slowing things down.

Sublime Text has solved my woes. Fundamentally its a text editor that allows you to have projects linked to a set of folders on your file system. It works on Mac, Windows and Linux and has an unlimited free trial. Text editors aren’t anything special on its own, but Sublime is backed up by a very fast fuzzy search system, that allows you to open any file in your project and browse to specific lines or functions with a couple of key presses. Did I say it was fast already? It’s worth restating as it blazingly fast compared to other editors I’ve used with similar features.

Next there’s the Build Systems feature. This is was the real game changer for me. Out of the box, Sublime can run Make files, Ant scripts and a bunch of other build types. Adding new build systems is done by writing a small piece of JSON, that describes a command to run and specifies it’s parameters. This has allowed me to write custom build rules that compile my make files, compile and run database scripts as well as launching a suite of test systems written in C++, Perl and PL/SQL.

Sublime Text supports a custom plugin system and there’s a vibrant plugin development community producing a host of useful add-ons. Need syntax hi-lighting for an obscure scripting language, chances are someone’s written a plugin for it. Want to integrate git commands or integrate directly with Github, yep, there’s plugins for that too. Finding new plugins is a snap through Package Control and the integrated package manager means new plugins can be installed in seconds.

It took me about an hour to put together a complete system that was customised the way I wanted. I can build, test and deploy every aspect of the system I’m working on all from within a single tool. Less time typing repetitive command on the command line, means fewer mistakes and allows me to focus on what’s important, the code.

If your not convinced yet, or just not sure that Sublime can really help you, I’d recommend watching a few of the tutorial videos from Tuts+. Their Perfect Workflow in Sublime Text 2 series is free, and showcases a ton of features that I can’t do justice here.

 

Embed links in a UILabel

Screenshot from the KILabelDemo application.

You often see apps with links, hashtags and usernames embedded in labels. I’m thinking of the many Twitter and Facebook clients out there in particular. I wanted to achieve the same effect in a client app I’m writing but there didn’t seem to be a nice way to do it with the native UIKit controls. Yes, I know it’s possible to get close to what I need with UITextView, but this isn’t easy to extend with new link types and introduces problems with interaction when its in a table cell. I tried a few open source UILabel replacements that I found and they all seemed a bit buggy or at least wouldn’t work how I expected them to.

I’ve ended up writing my own UILabel replacement, which you can find here on Github. KILabel is an extension of UILabel that adds the ability to automatically hi-light URLs, twitter style @usernames and #hashtags. It also provides a simple way to respond to users tapping on any of these using blocks. To use it just include the source code in your project and set the custom class of any labels in your nibs to KILabel.

KILabel uses ARC and TextKit so it’s iOS 7 only. It overrides UILabel’s text rendering to use a NSLayoutManager, NSTextContainer and NSTextStorage for rendering text. TextKit gives simple access to all sorts of text rendering operations which allow things like this to be implemented. There’s a useful TextKit primer here for those that are interested and my source code is quite heavily commented so it shouldn’t be too difficult to understand what’s going on.

I plan on extending KILabel as I need to, but if anyone else finds it useful and has any feature requests, please pop them in the comments.

First Post

Ah, the obligatory Hello World! post. Yes, this is the first post I’ve written on this new blog. After a long absence on the blogging and social networking scenes I’ve decided it’s time to start again. Why? Well, I’m on a new adventure into the world of self employment and I need somewhere for shameless self promotion.

This site is where I will sharing my thoughts on software development and the technology industry in general. I will also showcase some of my personal projects from time to time and include articles and tutorials on things that I’m doing. I’m hoping to post something at least once a week, so check back another time and there should be more here. In the meantime, follow me on Twitter, Google+ and all the other usual places for more immediate, but no doubt less meaningful updates.