Developing with Mac OSX - II

In the previous article http://howto.oz-apps.com/2013/04/creating-menubar-menu-mac-osx-using.html we explored how to create a menu bar and items in the same and then wrote handlers to manage the same. In this one we shall explore this further

In this article we shall get the IP Address of all the (connected) interfaces on our mac. We shall then display the interfaces as a badge on the dock icon.

Getting the IP Addresses

The easiest way to get all of the IP Addresses is using

NSMutableArray * array = [[NSMutableArray alloc] 
    initWithArray: [[NSHost currentHost]addresses]];

This returns the IPv4 and the IPv6 interfaces and since we are interested in just the IPv4 interfaces we can remove the IPv6 addresses. The way to do so is very simple, all IPv4 Addresses have dots '.' as separators and all IPv6 have colons ':' as separators, so we can eliminate the IPv6 by checking if the array object has a ':' in it.

    NSMutableArray * array = [[NSMutableArray alloc] 
           initWithArray: [[NSHost currentHost]addresses]];
    int i;
    for (i=(int)[array count]-1;i>=0;i--)
    {
        NSRange range = [[array objectAtIndex:i] rangeOfString:@":"];
        if (range.location != NSNotFound)
        {
            [array removeObjectAtIndex:i];
        }
        else
        {
            NSLog(@"%@",[array objectAtIndex:i]);
        }
    }


This code will display the IPv4 addresses only, we can also add these to the menu bar as menu items using the
    for (i=0;i<[array count];i++)
    {
        [theMenu 
           addItemWithTitle:[array objectAtIndex:i] 
           action:nil
           keyEquivalent:@""];
    }
We can set up the handler by setting it in the action: using the @selector() like copying the IP address to the clipboard as
 [theMenu addItemWithTitle:[array objectAtIndex:i] 
    action:@selector(saveToClipboard:) 
    keyEquivalent:@""];
and the function saveToClipboard is declared as
  -(void)saveToClipboard:(id)sender
  {
    [[NSPasteboard generalPasteboard]clearContents];
    [[NSPasteboard generalPasteboard]setString:[sender title] 
         forType:NSPasteboardTypeString];
  }
We save the IP Address to the clipboard as a string. Which can then be used from another application via the paste command.

Using the Badge on the Dock Icon

We can also indicate the number of IPv4 Interfaces we found by setting the badge on the Dock Icon. This can be set using the NSDockTile object and setting the badgeLabel on it.
    NSDockTile *tempTile = [NSApp dockTile];
    [tempTile setBadgeLabel:[NSString stringWithFormat:@"%d items",[array count]]];

Making use of all this

With the information in the two articles, you can put these together and create an Mac OSX app that sits in the menubar and shows you the IP Address and simply selecting it can copy it to the clipboard ready for pasting in any other app where the IP Address is required. The same has been made into a Mac OSX app and there is a previous article on that here, surprisingly written on 13th April 2011 exactly 2 years ago. You can download the DMG from here.

Comments

Popular Posts