Chaging NSStatusItem menus.

  • Hello,

    How can I switch menus for an NSStatusItem programatically? So if the
    user chooses to "quit" the application from the NSStatusItem I can
    switch it's menu to be another menu with launch options.

    Thanks,
    Jeremy

    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson
  • > How can I switch menus for an NSStatusItem programatically? So if the
    > user chooses to "quit" the application from the NSStatusItem I can
    > switch it's menu to be another menu with launch options.

      I admit I've never created a status item before, but have you tried
    the -setMenu: method referenced in the documentation?

    --
    I.S.
  • I.S.,

    Surprisingly, yes. I referred to the documentation BEFORE asking here.
    That was the only method that seemed like it would work, but it
    doesn't. So, hopefully this can be done. I also tried changing the
    title to the one action I want and setting the action but that didn't
    work. So I'm stuck here. And that was the ONLY method I have tried.

    Jeremy

    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 25, 2008, at 4:26 PM, I. Savant wrote:

    >> How can I switch menus for an NSStatusItem programatically? So if the
    >> user chooses to "quit" the application from the NSStatusItem I can
    >> switch it's menu to be another menu with launch options.
    >
    > I admit I've never created a status item before, but have you tried
    > the -setMenu: method referenced in the documentation?
    >
    > --
    > I.S.
  • > Surprisingly, yes. I referred to the documentation BEFORE asking here.

      Clearly you're new here ... ;-)

    > That was the only method that seemed like it would work, but it
    > doesn't. So, hopefully this can be done. I also tried changing the
    > title to the one action I want and setting the action but that didn't
    > work. So I'm stuck here. And that was the ONLY method I have tried.

      A quick archive and google search doesn't show anything that would
    suggest this shouldn't work. That doesn't mean we haven't missed
    something, but perhaps you should post your code.

    --
    I.S.
  • I just threw together a quick sample project, and I didn't have any
    trouble switching menus.

    Simply put:

    I've got 2 menus, fooMenu and barMenu, both in IB. I have from my
    controller outlets connected to both. In the controller,

    [statusItem setMenu:fooMenu];

    and

    [statusItem setMenu:barMenu];

    both work flawlessly for me.

    If you want the sample code, contact me offlist, and I'll send it to
    you.

    -- August

    On Feb 25, 2008, at 4:24 PM, Jeremy wrote:

    > Hello,
    >
    > How can I switch menus for an NSStatusItem programatically? So if
    > the user chooses to "quit" the application from the NSStatusItem I
    > can switch it's menu to be another menu with launch options.
    >
    > Thanks,
    > Jeremy
  • August,

    If you would like to send me your code that would be helpful. But I do
    my menu bar programatically due to the actual nature of my status bar...

    I.S.,

    Here's my code to quit iTunes. barItem is the NSStatusItem, iTunes is
    the Scripting Bridge iTunes Framework, and iTunesQuit is the new menu
    I want.

    - (void)quitItunes:(id)sender
    {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [iTunes quit];
    iTunesQuit = [[NSMenu alloc] initWithTitle:@""];
    qOne = [iTunesQuit addItemWithTitle:@"Start Watching iTunes Again"
    action:@selector(beginWatching:) keyEquivalent:@""];
    [barItem setTitle:@"Start iTunes"];
    //[barItem setAction:@selector(beginWatching:)];
    [barItem setMenu:iTunesQuit];
    //[[NSApplication sharedApplication] terminate:self];
    }

    Thanks,
    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 25, 2008, at 4:44 PM, August Trometer wrote:

    > I just threw together a quick sample project, and I didn't have any
    > trouble switching menus.
    >
    > Simply put:
    >
    > I've got 2 menus, fooMenu and barMenu, both in IB. I have from my
    > controller outlets connected to both. In the controller,
    >
    > [statusItem setMenu:fooMenu];
    >
    > and
    >
    > [statusItem setMenu:barMenu];
    >
    > both work flawlessly for me.
    >
    > If you want the sample code, contact me offlist, and I'll send it to
    > you.
    >
    >
    > -- August
    >
    >
    >
    >
    >
    > On Feb 25, 2008, at 4:24 PM, Jeremy wrote:
    >
    >> Hello,
    >>
    >> How can I switch menus for an NSStatusItem programatically? So if
    >> the user chooses to "quit" the application from the NSStatusItem I
    >> can switch it's menu to be another menu with launch options.
    >>
    >> Thanks,
    >> Jeremy
    >
  • > - (void)quitItunes:(id)sender
    > {
    > [[NSNotificationCenter defaultCenter] removeObserver:self];
    > [iTunes quit];
    > iTunesQuit = [[NSMenu alloc] initWithTitle:@""];
    > qOne = [iTunesQuit addItemWithTitle:@"Start Watching iTunes Again"
    > action:@selector(beginWatching:) keyEquivalent:@""];
    > [barItem setTitle:@"Start iTunes"];
    > //[barItem setAction:@selector(beginWatching:)];
    > [barItem setMenu:iTunesQuit];
    > //[[NSApplication sharedApplication] terminate:self];

      This is a little hard to follow without further explanation, but are
    you sure barItem is a valid pointer? If it's an IBOutlet, are you sure
    it's connected? That's the very first thing that comes to mind. Does
    the title change when you tell it to change to "Start Watching iTunes
    Again"?

    --
    I.S.
  • I.S.,

    I use barItem extensively. But after "Quitting iTunes" and leaving it
    be for a while, I clicked it and it did go to the other menu. But it
    wasn't instantaneous and it seems to be stalling after a while. Also,
    how can I set it so that instead of text it is a 16x16 image?

    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 25, 2008, at 4:58 PM, I. Savant wrote:

    >> - (void)quitItunes:(id)sender
    >> {
    >> [[NSNotificationCenter defaultCenter] removeObserver:self];
    >> [iTunes quit];
    >> iTunesQuit = [[NSMenu alloc] initWithTitle:@""];
    >> qOne = [iTunesQuit addItemWithTitle:@"Start Watching iTunes
    >> Again"
    >> action:@selector(beginWatching:) keyEquivalent:@""];
    >> [barItem setTitle:@"Start iTunes"];
    >> //[barItem setAction:@selector(beginWatching:)];
    >> [barItem setMenu:iTunesQuit];
    >> //[[NSApplication sharedApplication] terminate:self];
    >
    > This is a little hard to follow without further explanation, but are
    > you sure barItem is a valid pointer? If it's an IBOutlet, are you sure
    > it's connected? That's the very first thing that comes to mind. Does
    > the title change when you tell it to change to "Start Watching iTunes
    > Again"?
    >
    > --
    > I.S.
  • Hey,

    I think I figured out why my change isn't instantaneous like I was
    complaining before. I have a subclassed menuWillOpen: which updates my
    data upon open. Maybe I need to rework my code. Is there any if
    statement that could get me around this code problem?

    Thanks,
    Jeremy
    "For a long time it puzzled me how something so expensive, so leading
    edge, could be so useless, and then it occurred to me that a computer
    is a stupid machine with the ability to do incredibly smart things,
    while computer programmers are smart people with the ability to do
    incredibly stupid things. They are, in short, a perfect match." - Bill
    Bryson

    On Feb 25, 2008, at 4:58 PM, I. Savant wrote:

    >> - (void)quitItunes:(id)sender
    >> {
    >> [[NSNotificationCenter defaultCenter] removeObserver:self];
    >> [iTunes quit];
    >> iTunesQuit = [[NSMenu alloc] initWithTitle:@""];
    >> qOne = [iTunesQuit addItemWithTitle:@"Start Watching iTunes
    >> Again"
    >> action:@selector(beginWatching:) keyEquivalent:@""];
    >> [barItem setTitle:@"Start iTunes"];
    >> //[barItem setAction:@selector(beginWatching:)];
    >> [barItem setMenu:iTunesQuit];
    >> //[[NSApplication sharedApplication] terminate:self];
    >
    > This is a little hard to follow without further explanation, but are
    > you sure barItem is a valid pointer? If it's an IBOutlet, are you sure
    > it's connected? That's the very first thing that comes to mind. Does
    > the title change when you tell it to change to "Start Watching iTunes
    > Again"?
    >
    > --
    > I.S.
  • > I use barItem extensively. But after "Quitting iTunes" and leaving
    > it be for a while, I clicked it and it did go to the other menu. But
    > it wasn't instantaneous and it seems to be stalling after a while.
    > Also, how can I set it so that instead of text it is a 16x16 image?
    ...
    > I think I figured out why my change isn't instantaneous like I was
    > complaining before. I have a subclassed menuWillOpen: which updates
    > my data upon open. Maybe I need to rework my code. Is there any if
    > statement that could get me around this code problem?

      I'm not sure how anybody can make any useful recommendations
    without posting your code. The problem exists because of your
    approach, so without actually *seeing* your approach, nobody here can
    suggest a solution. ;-)

      Just post the stuff that deals with your menu.

    --
    I.S.
  • > Also, how can I set it so that instead of text it is a 16x16 image?

      Sorry, missed this one. How about -setImage:? According to the
    docs, it "Sets the image that is displayed at the receiver’s position
    in the status bar to image."

    --
    I.S.