skip main content

Posts Tagged ‘development’

Lookup – A simple Mac utility that wraps ldapsearch

posted by Duncan at 11:53 am on April 7th, 2009

I work on a Mac at work, and it has always been a bit slow searching for other staff members in our global address book. Like other big organisations we have some directory services which we can use LDAP to interrogate. The seemingly standard way for apps to poll these services is asynchronously every second or so, so that you’re getting results back quickly in the background. In reality this can be clunky if your network is slow, or the directory is very big.

I wanted something simpler, and a gui that gave me was suited to my work requirements. Lookup displays glance-able contact details straight away, letting you copy, paste and drag this data around. It also has simple shortcuts like double clicking the contact to start a new email to that person.

Lookup does not poll asynchronously, it does one request per search and just looks through some key attributes. It seems to work very well, and has been customised for my workplace, but is generic enough for someone to tweak it to suit their companies needs.

Lookup

It wraps ldapsearch, which is a command line app that comes free on your mac. When I say wraps, it’s not a complete wrapper, but wraps enough to create an app of this kind. Ldapsearch app did everything I wanted in search terms, and gave me a chance to use NSTask and NSPipe which I know will be useful in the future.

You can get the source over at Github. Why not try and tweak it to work for your company. You never know, just compiling it and updating the settings in the preferences menu may be enough.

Just to be clear, this app does NOT give you access to the BBC address book, just incase you thought I’d gone mad.

expires_in Rails FileStore fragment caching in about 6 lines

posted by Duncan at 2:32 pm on April 3rd, 2009

I have been building a Rails app that creates RSS feeds based on a lot of screen scraping behind the scenes. I really needed a way to cache the data object that feeds those specific pages. In rails you can do page, action and fragment caching, using many different methods, and storing that cache data in many different locations.

After deciding I needed Fragment caching, it turns out that if you need your cache data to magically expire over time, you need to be using memcached (I couldn’t), other wise you have to roll you own solution, based on cache sweeping, or in-code testing of whether your content has expired.

I’m using FileStore for caching (cached files live on the file system) and I have no database, and all the feed data comes from external feeds that change over time, so unless I fetch it all again, I don’t know.

What I needed was the ability to have a expires_in parameter whilst using FileStore. So this is what the code below does in surprisingly few lines. The code is an amalgamation of many posts I found doing almost the same thing, but this is what works for me.

First we extend FileStore:

# stick this in a file somewhere in your path e.g lib/fs_extend.rb
class ActiveSupport::Cache::FileStore
  def read(name, options = nil) 
    ttl = 0
    ttl = options[:expires_in] if options.is_a?(Hash) && 
                                         options.has_key?(:expires_in)
    fn = real_file_path(name)
 
    return if ttl > 0 && File.exists?(fn) && (File.mtime(fn) < (Time.now - ttl))
    File.open(fn, 'rb') { |f| Marshal.load(f) } rescue nil
  end
end

And then in your controller I have:

class FooController < ApplicationController 
  def do_stuff
    key = 'someuniquekey'
    expire_fragment(key) unless 
        @data = read_fragment(key, :expires_in => 1.hour) || 
                                  write_fragment(key, some_complex_data)
 
    respond_to do |format|
      format.xml
    end 
  end
end

This has reduced many 5 second requests to 32ms requests, so hurrah!

Replicating Apples embossed text in a Cocoa App

posted by Duncan at 11:22 am on March 11th, 2009

[update] Thanks to the chaps below in the comments, my in-experience with cocoa is shown. This whole post could be replaced with this code:

 [[myTextField cell] setBackgroundStyle:NSBackgroundStyleRaised];

If you look at the text in the task or status bar on of a Mac app, you will see that the text has the appearance of being embossed slightly. It is essentially a textField with some neat attributes associated with it.

Apple embossed text

This effect does not come for free when you want to use your own NSTextField somewhere else in your Apps window (actually, I think it does on the iPhone?). To recreate this effect is quite simple. You just need to create a NSAttibutedString and associate the attributes below to it.

// Create the white shadow that sits behind the text
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[NSColor colorWithDeviceWhite:1.0 alpha:0.5]];
[shadow setShadowOffset:NSMakeSize(1.0, -1.1)];
// Create the attributes dictionary, you can change the font size
// to whatever is useful to you
NSMutableDictionary *sAttribs = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:
    [NSFont systemFontOfSize:11.0],NSFontAttributeName,
    shadow, NSShadowAttributeName,
    nil] autorelease];
// The shadow object has been assigned to the dictionary, so release
[shadow release];
// Create a new attributed string with your attributes dictionary attached
NSAttributedString *s = [[NSAttributedString alloc] initWithString:@"82 results found"
    attributes:sAttribs];
// Set your text value
[myTextField setAttributedStringValue:s];
// Clean up
[s release];

I’m sure there are many other ways to achieve this effect, and I’d be interested in any easier versions, but this works for me.

Experiments in Cocoa #2 TellyBox

posted by Duncan at 3:26 pm on February 23rd, 2009

After my first foray into Cocoa development, which involved Radio, I decided to take what I had learnt and move into TV. Tellybox is the result. It’s a Cocoa desktop app that’s a wrapper for the BBC iPlayer live and catchup service for a day. I’m quite pleased with how it turned out.

[BY THE WAY] This is a personal learning project, and not affiliated with the BBC in any way. It (and I) only use publicly available information, in order to try and inspire people to just build cool stuff. If you think you could do better or have ideas in improving the app, please let me know. I’d love to hear from you.

Download TellyBox via it’s github project page.
TellyBox - A Mac app that lets you watch the telly

Below I’ve briefly noted the various features, and what areas of Cocoa I had to learn to implement them. I ‘ve added a long list of resources at the bottom, which have and continue to, prove very useful when developing in Cocoa.

1. Select your Favourite Station – You can select which channel you’d like to watch from the Watch menu. This involves reading in some .plist configuration data and displaying it in an NSMenu. The menu is drawn with code, and once understood, meant building and manipulating all the other menus was a breeze.

Tellybox station selection

2. Choose your viewing size – You can choose from a list of 5 sizes to view from. These sizes can be chosen from the View menu or the shortcuts 1-5. This sizes as well as the location of the display window are remembered when you quit the application. The resizing is using NSWindows setFrame:display:animate: to make it look pretty and is using NSUserdefaults to manage the remembering of the size and origin of the window.

Tellybox viewing sizes

3. The day schedule – Here you get a list of what’s on the day you are watching, and what is available to watch again. It also marks where you are in that day. I still need to work on updating this schedules as currently it only loads when you first start viewing, so won’t change automatically over time. The schedule data comes from BBC Programmes via XML and is parsed using NSXMLDocument. It uses the same caching policy as the BBC site.

Tellybox schedule

4. Growl Support – The app has Growl Notification support. If you have growl installed you will get notifications about what you’re watching. This was implemented with the growl documentation and uses the Growl Application Bridge.

Growl Support

5. Preferences – You can change a few preferences at the moment. One is the default Network that gets shown when you start the application. The other is whether you would like to receive automatic updates and how often you would like to check whether they’re available. The former uses NSUserdefaults and Cocoa Bindings. The latter uses the popular and amazing Sparkle update framework.

Tellybox - Preferences

Here’s a list of useful resources I’ve used to get to this point:

Finally a few things that don’t work so well. Well one at the moment really, and that’s guidance. I haven’t quite worked out how to get the iPlayer guidance stuff working in Tellybox, which means if the show you want to watch again has a guidance rating then the only way you can watch it, is to go to the web version. I’ll keep trying though.

I think that’s it. Hope you like it. I’m really enjoying Cocoa development at the moment and look forward to my next personal project.


back to the top