skip main content

Posts Tagged ‘cocoa’

nextKeyView and moving between NSTextfield and NSButton

posted by Duncan at 3:57 pm on May 26th, 2010

This caught me out today, so I thought I’d share to save someone else scratching their head. The problem I was having was thus. In interface builder I had a window with an NSTextField and an NSButton. I want to be able to tab between them. I did what I thought was correct:

  1. Set the intialFirstResponser for the NSWindow to be my NSTextField
  2. Set the NSTextField nextKeyView to be the NSButton
  3. Set the NSButton nextKeyView to be my NSTextField

But this was not working in the app! It Turns out that in the Apple > System Preferences > Keyboard there is a setting that allows you to press Tab to move keyboard focus between:

  • Text boxes and lists only
  • All controls

I think the top one is set by default. Once changed to All controls everything worked as expected.

Using Cocoa to keep an app window always on top

posted by Duncan at 4:39 pm on April 11th, 2009

Sometimes you want your application to keep one of it’s windows always-on-top. A chat application like Adium for example, allows you to keep your conversation window always-on-top so you can follow the conversation whilst still using other applications. In the TellyBox application I wrote, this functionality is also very useful, as you can watch tv whilst still working in other applications.

So the basic implementation was fairly simple. You just use the windowDidResignMain notification, and then re-set the window level a more fitting one. Below I have also wrapped around a preference setting:

- (void)windowDidResignMain:(NSNotification *)notification
{
  // It's always nicer if the user has a choice
  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultAlwaysOnTop"] == YES) {
    [[self window] setLevel:NSFloatingWindowLevel];
  } else {
    [[self window] setLevel:NSNormalWindowLevel];
  }
}

My TellyBox application allows you to go fullscreen via the existing Flash application embedded in a webkit view. Using just the basic implementation meant that when going into fullscreen mode the frame of the original window remained in view. To fix this I added this extra bit of code, which gets used via the firing of another useful notification windowDidBecomeMain :

- (void)windowDidBecomeMain:(NSNotification *)notification
{
  [[self window] setLevel:NSNormalWindowLevel];
}

which sets the window level back to it’s default value of 0 when it becomes the main window i.e when you select it.

I hope this little snippet helps people trying to achieve the same effect in their apps.

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.

TellyBox 1.4, Twitter, Scheduling .. plus the coolest icon

posted by Duncan at 3:35 pm on March 22nd, 2009

[note] if you are updating and have the current app in your dock, you may need to remove it, and then re-add it again to get the new icon to appear straight away

I’ve just released a new version of TellyBox, version 1.4. This version pulls in all the new features from sister app RadioAunty, plus a few news ones specific to TellyBox.

If you didn’t know, TellyBox is a Mac app that allows you to watch live and catchup BBC Television (Uk users only I’m afraid). It uses the BBC iPlayer to actually display the telly, but wraps it all up into a native Mac application.

TellyApp screenshot

Like I mentioned above, new features include Twitter support and scheduling as per RadioAunty. I recommend reading this post to get more information on these. As well as them, the new features specific to TellyBox are:

  1. I have removed the default channel entry from the preferences. The app now just remembers what channel you last watched.
  2. The close button, and zoom buttons (red and green) now work.
  3. A brand new app icon (designed by David Wilson)

I’m really pleased with the icon. I know David spent a lot of time designing it, and for a first application icon, I think it’s awesome. I also knew the effect I wanted. I really liked the idea of the icon being a mini telly showing you exactly what you are watching in the main screen. It seems Mac icons can make or break an app, so I’m hoping this one can only help.

It’s actually quite a simple effect to achieve. It’s just about building layers. I use CGWindowListCreateImage to take a screen grab of what’s playing over time. When I have a new grab, I then construct the dock icon, using the grab at the bottom, then a version of the app icon with a transparent screen to give the glass effect, and finally the channel logo. If you minimize the app, or change windows using spaces, then the icon will change to the BBC test card.

Download TellyBox from the project page on Github, or if you already have the app installed, then you should get a request to update automagically.

RadioAunty feature update – twitter, scheduling and much more

posted by Duncan at 7:50 pm on March 14th, 2009

In my on going Cocoa learning I have been working hard on updates for RadioAunty. ( If you didn’t know, RadioAunty is Mac app that allows you to listen to live and catchup BBC Radio ). This means the feature list is now quite compelling, so I decided to give and overview of the features to date.

1. Select your Favourite Station – Choose from all the BBC National radio stations. You can listen to them in Normal RealPlayer or the much higher quality ACC streams. Make your decision via the preferences screen.

RadioAunty select a Station

2. Minimise the radio – I didn’t think people generally stare at the player whilst they are listening, so you can now minimise it like you can with iTunes, using ^ cmd Z or the window menu. You can also tweak the width whilst minimised. You will also see that the window title shows the current show playing. This updates along with the schedule as time goes on. More on this next.

RadioAunty minimized

3. View the schedule – In the Schedule menu, you will see the day schedule for the station you are listening to (using data from BBC programmes). You will see a tick next to the show you are currently listening. You will also see that some shows are clickable. This is stuff you can listen to, that is available to catchup in iPlayer, it also displays how long left that show is available in iPlayer for. This schedule updates as you listen throughout the day.

RadioAunty schedule menu

4. Twitter support and other preferences – The preferences window gives you the chance to change some of RadioAunty’s settings and turn on others. From the top, we have updates. Turning this on allows you to receive updates to the application when they become available. Next is default station. This allows you to choose a default station to open with (I think this may be removed next release, and the app will just remember what you last listened to. Next is audio quality. It’s best to stick to high quality, but those streams are only available in the UK, so if you are listening abroad you will have to choose normal quality. Finally Twitter support. Opting in means that as you listen, details about what you are listening to will be sent to the @radioandtvbot account on Twitter. If you supply your twitter username, this will be added to the tweet instead of the default, which is your computer login FullName. The screenshot below next shows you what gets sent.

RadioAunty twitter support
radioandtvbot

5. A nice dynamic app icon – The wonderful Tim Broom made the current icon. The icon changes, showing the network you are currently listening to.

Nice Icon

6. Growl support – If you use Growl, then RadioAunty will notify you when a show starts playing. If you choose the twitter support it will also notify you when this information is sent to twitter.

Growl Support

Next then. I’m going to be transferring the twitter and scheduling stuff over to TellyBox (A Mac app that allows you to watch live and catchup BBC Television). So watch out for the updates. Then I think lastfm integration is on the cards. It’s be nice to scrobble what tracks you are listening to.

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.

Experiments in Cocoa #1 RadioAunty

posted by Duncan at 9:47 pm on December 12th, 2008

[UPDATE] I’ve updated the app to now use the new higher quality live streams. Yay, no more RealPlayer for the majority.

I’ve seem to have spent a lot of time reading about Cocoa without actually building anything of substance. So to put a change to that I have built RadioAunty.

1. RadioAunty – This is a Cocoa Application that lets you listen to the radio, BBC radio, on your desktop. You can view the current schedule and select listen again shows, as well as simply changing the station via the menu bar and via the Dock. You can also set preferences to decide which should be your default Station to start with, and whether you would like to receive updates to the application when they are available (very important). If you have listened to BBC radio via Safari, then RadioAunty should work just fine for you.

Download RadioAunty from it’s project site

RadioAunty

There are now many *.frameworks that come as part of the Apple Developer Tools and I found it pretty daunting to start with. So to make life easier for myself, I decided to start small and build bigger as I go along. This is why I started with the simple idea of RadioAunty. Oh and it’s Leopard only because it seemed silly to have to learn old ways of doing things as well as new.

RadioAunty is at a high level not that complex. It basically embeds an already existing webpage into a desktop app. It seemed like an ideal starting point. Once I had got that working (Thanks Apple, it takes about 3 lines of code!), the next thing was to make use of other classes and functionality to actually learn something. The app as it stands uses only uses these Cocoa classes (I’ve left the boring ones out):

I’ve used @property quite a lot (properties are a bit like Ruby :attr_accessor) to automagically create getter and setters. I have also use key-value bindings, which once I understood them were amazing, and allow you to chop huge chunks of code from your source. I use a stations.plist to store the radio station data, but hope to pull that from the web in the future, meaning that it could play any radio station and not just the BBC’s. I also use delegation a lot whenever I can.

As I only started the app on Tuesday, I’ll continue making it more polished, that and I have a few other ideas, but I wanted to get something out for people to use as quickly as possible, so first main thing to add (apart from the radio) was auto-updating. This is taken care of by Sparkle 1.5b6.

Sparkle was super simple to implement. Their wiki documentation worked great, and I was amazed that something I always think is cool, and take for granted was so smooth to implement in this application. If you’re building OSX applications then this should surely be a must.

I have a few other projects in the pipeline, that incorporate more frameworks for me to learn, so stay posted. I’m progressing well with is an app called LeaveFrom, that does what is says on the tin really. It uses Core-Data which again had a bit of a learning curve, but I’m getting on ok. Can my old brain take all this new fun information.


back to the top