Engineering for the real world

My tricks with TextExpander

Having installed it a while back, I've just started to find out the joys of using TextExpander. I'm not really finding it that useful for inserting boiler plate text which is I guess it's primary use. It really does help me out with a few other things though.
I love all the services that are available in OS X, but using the special character menu is a real pain because it is so slow. Just using TextExpander I don't need to go through the special character menu, I just set the application up so that when I type gpi text expander inserts a π.

A while back, I blogged about creating Applescripts which insert references into a document. Not having to look up references made my life a lot easier, but with text expander it is now even easier. I can now just type refcarranzatorres and the reference is automatically inserted:

Hoek-Brown failure criterion - 2002 edition, 2002, E.Hoek; C. Carranza Torres; B. Corkum, Proc. NARMS-TAC Conference, Toronto, 2002, 1, 267-273, www.rocscience.com/hoek/references/H2002.pdf.

This saves a fair bit of time, but more than that it suddenly takes almost no effort to insert a reference into a document. Not only does this improve the quality of reports but any digital work can become well referenced. Even Cocoa or Applescript code written in Xcode can now include equation references as comments with almost no effort.

To round off TextExpander as one of my killer applications, I use MathMagic to create a beautifully formatted equation and then paste it straight into TextExpander. All I have to do is type eqBartonQ and a get a nicely formatted version of the famous Q equation by Barton:
image

If you haven't tried it, give TextExpander a go, it really is one of those Apps that starts paying for itself as soon as you open it and just get better and better the more you use it.



Example Cocoa code

It always seems odd how it is difficult to find examples of the application you are working on. For me, just starting to learn Cocoa, tutorials and example applications have been the key to my learning. But for some reason you can never find the right tutorial to sort out the problem you are working on.
With this post I'm releasing some of my code into the wild for those new to Cocoa development. I'm not going to claim that the quality of the code is great and it doesn't really do anything very special, but it is out there and might help a few people solve some of the more basic problems that I had to initially deal with.
For the record, the application is designed to do some basic analysis for a tunnel, estimating the Fenner-Pacher curve. Some of the key Cocoa concepts in the application are:
  • Use of Core Data
  • Use of triggerChangeNotificationsForDependentKey for automatically updating data
  • Creating a graph which automatically updates using tableViewSelectionDidChange
  • Performing mathematical and engineering calculations
The method for creating the graph is based on the delegate method which is used in Step into XCode Mac OSX Development (for OS 10.4) and XCode 3 unleashed (for OS 10.5) by Fritz Anderson. A tutorial by Tim Isted gives another method, using bindings, to create automatically updating graphs.

You can download my source code here.


Version 5 of Sketchup for a Mac

Google Sketchup is a stunning application, a true example of the power of simplicity. It may not have the power of some other 3D design applications, but the simplicity does allow for the rapid development of design models.

One problem for Mac users is that version 6 only works with versions of OSX after 10.4.1. Unfortunately Google have not posted Sketchup version 5 for download. If you are using Mac OS 10.3.9 or earlier here is a version of Sketchup 5 that you can download.

Download it here


Applescript in iWork

Applescript has the potential to significantly improve many work flows. Like all programming or scripting languages it does have a learning curve, but because of the simple structures and plain language the curve is one of the easiest languages to learn. The real power of the language is its ability to interface between different programmes allowing data to be transferred from one application to another. It is also a quick way of creating similar tools to Macros that are used in Microsoft Office.

It comes as a surprise that one of Apples flagship applications, iWork, does not fully support Applescript. Neither Pages nor Numbers have applescript dictionaries associated with them, although Keynote does have a dictionary. This means that Pages and Numbers do not have publicly documented specific Applescript abilities. The good news is that this doesn't mean that Applescript can't be used with this applications.

Using the 'application' System Events you can easily control keystrokes, and occasionally buttons within most applications. To demonstrate how this is done, the following simple script is used to insert a standard reference into the active Pages document. Applescript Studio can then be used to create an interface which allows different references to be inserted all at the click of a button.

set the_text to "Hoek-Brown failure criterion - 2002 edition, 2002, E.Hoek; C. Carranza Torres; B. Corkum, Proc. NARMS-TAC Conference, Toronto, 2002, 1, 267-273, www.rocscience.com/hoek/references/H2002.pdf"
set the clipboard to the_text
tell application "System Events"
tell application "Pages" to activate
tell process "Pages" to keystroke "v" using command down
end tell


I find this script a real timesaver because I don't have to look up references every time I need to insert them into a document. All it takes is a simple click of a button.


Learning Cocoa

Over the last few months I've been trying to learn programming using Cocoa, one of the native programming languages for a Mac. It's been a tough learning curve. Until trying to learn Cocoa I've only really developed rather basic scripts with languages such as Visual Basic, Applescript and FISH. I've never had any formal training in programming so it's all been a bit of a struggle. I decide not to learn through books, even though there are few very highly rated books out there. The internet is such a great resource it seemed obvious that there must be some great tutorials out there.

Well I have to say that it is true, there really are some great resources out there. The trouble is there is no structure to it. There is no way to find out which tutorial you should start with and where you should go from there. It took me a bit of stumbling around in the dark to try and find my way around it all. I must admit that there are a few tutorials that I have done more than once and still don't understand, and that is despite the tutorials being describe as 'for beginners'. The penny has finally dropped though, I have suddenly started to understand Cocoa and that is because I happened to find a sequence of tutorials that seems to have worked. If you want to have a go at learning Cocoa, try this sequence. I hope it will as well for you as it did for me.

- The first stage is to go through an excellent short PDF manual called Become an Xcoder. This really does require no prior knowledge of C, C++ or any other language that most manuals assume the reader has. It gives the reader sufficient knowledge to understand what is happening in all the different sections of a project as well as working with basic classes such as arrays or strings.

- The next stage I would recommend is to do Apple's tutorial. To understand fully what is going on in this tutorial you do need some knowledge of the code structures used by Cocoa, but having been through 'Become and Xcoder' you should be ready for this. The one problem with Apples tutorial is that the latest version is based on Leopard and Objective C 2.0. If you don't have leopard, you aren't out of luck, there are still some examples using older versions of Xcode on the internet. This tutorial is useful because it gives you an understanding of some of the non code related issues associated with creating an application.
The latest Cocoa tutorial on the Apple Developer site
An older version of the Apple currency converter tutorial


- The next stage I would suggest is to go to CocoaDevCentral.com and go through the tutorials on the front page. These are some excellent tutorials dealing with some of the other features of Cocoa such as core data and working with graphics. Doing this will give you a fair understanding of how easily accessible some of the features of Cocoa are.
CocoaDevCentral

- Finally I would recommend looking at a larger project and taking it appart to understand how it works. An excellent example of this is a simple calculator app prepared by Apple. Being such a simple application we all understand what it needs to do, making the methods within the application much more accessible. Having been through all of these tutorials you should be able to understand what almost every line of code in the project does. You can then start adding your own buttons and code to the calculator to try out your own skills.

- Now, the world is you oyster. It is now time to start branching out to develop the type of application that you want to do. Once source I would suggest that you have a look at is MacDevNet. It includes two excellent podcasts, CocoaCast for those wanting to do more work on the basics and LateNightCocoa, a well produced series of interviews with Cocoa developers. MacDevNet

I hope this might help out a few people who are starting out or struggling with Cocoa. For a beginner it is a very steep learning curve but it will be well worth it in the end.