# MacOS app as background process with system tray icon and native menu…

**URL:** <https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905>\
**Category:** MacOSX and iOS\
**Created:** [June 6, 2020, 11:53am UTC](https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905 "2020-06-06T11:53:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhgriggs](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@jhgriggs](https://forum.juce.com/u/jhgriggs)\
**Post date:** [June 6, 2020, 11:53am UTC](https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905/1 "2020-06-06T11:53:54Z")

</div>

Right, so a few things going on here. My current goal is to create an app which sits in the background listening to midi input. It will have a couple of basic settings which I want to be accessible via a native menu attached to a SystemTrayIconComponent. Sounds doable.

I started by creating an audio application in the Projucer.

Here are my issues:

1. I don’t need the app to have any form of main window, and no dock icon. It will live entirely in the taskbar, with no main menu. I’m able to remove the dock icon via `Process::setDockIconVisible (false)` but this still allows the app to be brought to the foreground when the SystemTrayIconComponent is clicked, thus showing a bare bones main menu.  
Edit: this is solved by getting rid of the automatically created main document window. Duh.

2. I’m getting an assert when trying to show the native system tray menu. In my overridden mouseDown method I’m creating a PopupMenu and calling `SystemTrayIconComponent::showDropdownMenu`. The assert I’m getting occurs in `juce_mac_MainMenu::createNSMenu`, where I’m informed I’m _`"calling this before making sure the OSX main menu stuff was initialised?"`_ Hmm… I don’t even want a main menu?

3. I’m not very good at troubleshooting this myself. Someone please help 🙂

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![simon](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@simon](https://forum.juce.com/u/simon)\
**Post date:** [April 22, 2024, 8:46am UTC](https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905/2 "2024-04-22T08:46:59Z")

</div>

Hi 🙂

4 years later, I am having the exact same issue as point number 2 above. I want the menu of my SystemTrayIcon to be “macOS” like, i.e. same as other apps. I believe using the `SystemTrayIconComponent::showDropdownMenu()` is the way to do this, but there are no examples using it.

Taking the [DemoRunner example](https://github.com/juce-framework/JUCE/blob/master/examples/DemoRunner/Source/Main.cpp) and changing these lines in Main.cpp at line 70:

```auto
         m.addItem (1, "Quit");
         m.showMenuAsync (PopupMenu::Options(),
                          ModalCallbackFunction::forComponent (menuInvocationCallback, this));

```

changed to

```auto
         m.addItem ("Quit", DemoTaskbarComponent::menuInvocationCallback);
         showDropdownMenu(m);
         // and I removed the parameters of menuInvocationCallback()

```

This hits the jassertfalse in `createNSMenu()`, line 845 of juce\_MainMenu\_mac.mm.  
Looking at the JUCE code, it seems the `JuceMainMenuHandler` singleton is never instanciated, except in `MenuBarModel::setMacMainMenu()`, which I guess I should call somehow?

I am not sure I fully grasp what a MenuBarModel is and why it would be nedded for the SystemTrayIcon menu? Could someone point me to the right direction?

Thank you!

Simon

PS: not sure if this is fully related, but there was a similar issue in the past: [SystemTrayIconComponent OSX native menus broken in 5.00 working in 4.3.1](https://forum.juce.com/t/systemtrayiconcomponent-osx-native-menus-broken-in-5-00-working-in-4-3-1/26779/1) ?

---

<div class="post-metadata">

**Author:** ![simon](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@simon](https://forum.juce.com/u/simon)\
**Post date:** [May 22, 2024, 7:55am UTC](https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905/3 "2024-05-22T07:55:52Z")

</div>

Ok, just FYI if someone has the same issue, I managed to get it to work.

I guess the idea is if you are using the system tray with a native macOS menu, you should also configure the native macOS menu on the top left of the screen, fair enough.

But if like me you are not using the menus at all, only the system tray one, then you can simply instanciate a dummy MenuBarModel in your application and call `MenuBarModel::setMacMainMenu()` with it.

Something like this:

```auto
#if JUCE_MAC
class dummyMenuBarModel final : public juce::MenuBarModel
{
  public:
    dummyMenuBarModel() {
        juce::MenuBarModel::setMacMainMenu(this);
    }
    ~dummyMenuBarModel() override {
        juce::MenuBarModel::setMacMainMenu (nullptr);
    }
    
    juce::StringArray getMenuBarNames() override { return {""}; }
    juce::PopupMenu getMenuForIndex (int, const juce::String&) override { return juce::PopupMenu(); }
    void menuItemSelected (int, int) override {}
};
#endif

// ...

class myApplication final : public juce::JUCEApplication
{
  public:
    // ...
  private:
    #if JUCE_MAC
        dummyMenuBarModel model;
    #endif
};

```

Simon

---

<div class="post-metadata">

**Author:** ![simon](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@simon](https://forum.juce.com/u/simon)\
**Post date:** [May 22, 2024, 12:43pm UTC](https://forum.juce.com/t/macos-app-as-background-process-with-system-tray-icon-and-native-menu/39905/4 "2024-05-22T12:43:01Z")

</div>

Alright, new problem \>\_\<

Now it works, but I noticed 2 things:

1. The tray icon does not respond every single time it is clicked, in fact it precisely responds 50% of the time. Testing other mac apps, they do not have that problem. Checking in the code, it seems mouseDown() is only called 1 out of 3 times:

2. When exiting with Cmd+Q or with the Apple menu on the top left, everything is fine. But when exiting with the tray icon, the program hangs until it is clicked again. The exit callback calls `JUCEApplication::getInstance()->systemRequestedQuit();`, which enda the message dispatch loop by calling `shutdownNSApp();` at `juce_MessageManager_mac.mm:352`. But then the program is stuck in `[NSApp run];`  
of `runDispatchLoop()` (same file at line 335)

The two issues are clearly related, because the second does not happen everytime, depending on whether we are on the click that does not work or not.

* * *

**EDIT** : since this is a new issue I have made a new topic about it → [Message loop hanging when using SystemTrayIconComponent::showDropdownMenu](https://forum.juce.com/t/message-loop-hanging-when-using-systemtrayiconcomponent-showdropdownmenu/61517)

(there is a test project included as well)
