4

I have been searching all over the internet for this, going from stack overflow answer to stack overflow answer, Trying rococoa, or Class.forName ("com.apple.cocoa.application.NSApplication"); amongst other things.

The bottom line of the matter is, I cannot, for the love of god, figure out how to get my Java application to focus its self on OSX!

Let me be clear: My application has no windows (It will in the future, but sometimes it may not have any windows at all). I need a way to focus my application that does not rely on windows.

Having not found anything, I desperately decided to try a solution that relied on there being a window:

private static void BringSelfToFocus()
{
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            Window window = new JFrame("Test");
            window.toFront();
            window.repaint();
        }
    });
}

That, however, like every other futile attempt of mine, failed.

So, yes, while this is technically a duplicate question, I have tried every single other answer I could find, and for whatever reason, none of them worked.

Can anyone please lend a helping hand on this matter? Thankyou.

-Georges

Georges Oates Larsen
  • 6,812
  • 12
  • 51
  • 67

1 Answers1

6

Well! I had one final idea, and it worked! I used Applescript.

private static void BringSelfToFocus()
{
    AppleScript("tell me to activate");
}


private static String AppleScript(String script)
{
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");

    if (engine != null) {
        try
        {
            return (String)engine.eval(script);
        }
        catch (ScriptException e)
        {
            e.printStackTrace();
        }
    }
    return null;
}
tresf
  • 7,103
  • 6
  • 40
  • 101
Georges Oates Larsen
  • 6,812
  • 12
  • 51
  • 67
  • wow! I had a same problem and your solution worked perfectly! It's sad that toFront() doesn't work as it should, but glad there is this workaround. Thank you – Saeid Nourian Mar 15 '14 at 20:08
  • 1
    Has anyone had success when this is combined with https://stackoverflow.com/a/11140556/3196753? I'm trying to add this logic to `@Override public void setVisible(boolean b)` without success. My dialogs are still behind other windows most of the time. – tresf Oct 16 '18 at 21:15
  • ... I also tried `tell application \"My App\" to activate` (works from Terminal, but not from Java) as well as trying to fire it AFTER a `WindowEvent.WINDOW_OPENED` was triggered to no avail... – tresf Oct 16 '18 at 21:33
  • Answering my own question `setVisible(...)` is picky about order of operations. The `super.setVisible(...)` should be the last call in the function per https://github.com/qzind/tray/issues/367. Standard logic would place it the other way, but this seems to work well. – tresf Nov 07 '18 at 14:17
  • @tresf did you find a solution for bringing dialogs to front with `apple.awt.UIElement` set to `true`? – CamHart Jun 27 '19 at 12:33
  • I leveraged an AppleScript like this solution explains but used PID as it was more reliable than application name (e.g. running from IDE). – tresf Jun 27 '19 at 18:03
  • PID example: https://github.com/qzind/tray/pull/408/files (and yes, this is using `UIElement` flag) – tresf Jun 27 '19 at 18:05