0

I am a .Net developer that needs to port a small program over to Mac OS X. I have this mostly done (partly thanks to people on this site, thanks!) but have a bug that maybe people I can get help with.

I am creating a tool that sits in the status bar, that when clicked opens a window with several links or buttons. When the links or buttons are clicked they either open a website or external program. The problem is that the icon in the status bar disappears as I launch one of these external commands. Even more interesting is that the space on the status bar where the icon should be still responds; meaning that if I click on the area (even without the visible icon) it still runs the code and opens the window.

Here is the current code:

tray.m

#import "tray.h"
#import "MyView.h"

@implementation Tray
-(void) awakeFromNib{
    NSBundle *bundle = [NSBundle mainBundle];
    statusItem = [[NSImage alloc] initWithContentsofFile:[bundle pathForResource:"@icon" ofType:@"png"]];
    MyView *view = [MyView new];
    [statusItem setImage:statusImage];
    view.image = statusImage;
    [statusitem setView:view];
    [statusitem setToolTip:@"Tray App"];
    [view setTarget:self];
    [view setAction:@selector(openWindow)];
}

-(IBAction)openWindow:(id)sender{
    [trayWin makeKeyAndOrderFront:nil];
}

-(IBAction)openActMon:(id)sender {
    (void)system("open '\/Applications/Utilities/Activity Monitor.app'");
}

tray.h

#import "MyView.h"

@interface Tray : NSObject {
    NSStatusItem *statusItem;
    NSImage *statusImage;

    IBOutlet NSWindow * trayWin;
    IBOutlet NSButton *ActMon;

    void *openWindow;
}

@property (retain,nonatomic) NSStatusItem *statusItem;
-(IBAction)ActMon:(id)sender;
@end

MyView.h

@interface MyView : NSControl {
    NSImage *image;
    id target;
    SEL action;
}
@property (retain)NSImage *image;
@property (assign) id target;
@property (assign) SEL action;
@end

MyView.m

#import "MyView.h"
@implementation MyView;
@synthethize image, target, action;
-(void)mousemouseUP:(NSEvent *)event{
    [NSApp sendAction:selfself.action to:self.target from:self];
}
-(void)dealloc {
    self.image = nil;
    [super dealloc];
}
-(void)drawRect:(NSRect)rect {
    [self.image drawInRect:CGRectMake(0,0,18,18) fromRect:NSZeroRect operation:NSCompositeSourceOver];
}
@end
}

The openActMon is run when the image/button is clicked, the image is located in the trayWin Window that is opened when the icon is clicked. At this point, Activity monitor successfully launches, but the icon in the StatusBar disappears.

I have tried putting a [super setNeedsDisplay:YES] in the openActMon, but that didn't help. And I added [view setNeedsDisplay:YES] in the openActMon and it responded undeclared.

I have given all of this code because, as I said, I am not a Objective-C coder, but .Net who just needs to port something small over. Hoping that this will be helpful to others in the future. Alot of this I have hodgepodged together from different forums and sites or have gotten from some help on StackOverflow. I am hoping someone can help.

Thanks in advance!

Dan
  • 103
  • 1
  • 5

2 Answers2

2

In awakeFromNib add:

[statusitem retain];
Benoit Garret
  • 14,027
  • 4
  • 59
  • 64
jkcat
  • 21
  • 2
0

In awakeFromNib, you are allocating the NSImage into statusItem. I think you mean to allocate it into statusImage.

Lytol
  • 970
  • 6
  • 15
  • That's actually a typo. I did not copy and paste my code, but re-wrote it for this post. I am loading NSImage into statusImage. Sorry about the confusion. – Dan Jan 04 '11 at 23:05