0

I am playing around with the example from here; esp. I have this code:

from osax import *

sa = OSAX()

print sa.display_dialog("Python says hello!",
                        buttons=["Hi!", "Howdy!", "Duuuude!"],
                        default_button=3)

The dialog always opens in the background. How can I open it in (or move it to) the foreground?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Albert
  • 65,406
  • 61
  • 242
  • 386

2 Answers2

1

Add an activate command to give your Python process the window manager application focus.

sa = OSAX()
sa.activate()
print sa.display_dialog(...)

You can also choose to not GUIfy your Python process by targeting a faceless background app as described here.

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 1
    This no longer works on 10.10 — the Python app doesn’t activate because it hasn’t been promoted to a foreground app, then `display_dialog` runs in the background. However, targeting `OSAX()` or `ScriptingAddition()` to System Events (or another app of your choice) does work. See http://appscript.sourceforge.net/py-appscript/doc/osax-manual/04_notes.html for more. – Nicholas Riley Nov 09 '14 at 14:40
0

This works for now:

def go_foreground():
    from AppKit import NSApp, NSApplication
    NSApplication.sharedApplication()
    NSApp().activateIgnoringOtherApps_(True)
go_foreground()
Albert
  • 65,406
  • 61
  • 242
  • 386