diff --git a/pelita/ui/tk_viewer.py b/pelita/ui/tk_viewer.py index 3628e814d..f62bce8fd 100644 --- a/pelita/ui/tk_viewer.py +++ b/pelita/ui/tk_viewer.py @@ -1,4 +1,7 @@ import logging +import os +import shutil +import sys import tkinter @@ -7,6 +10,18 @@ _logger = logging.getLogger("pelita.tk_viewer") _logger.setLevel(logging.DEBUG) +def force_frontmost(): + """ This hack was discussed in https://github.com/ASPP/pelita/issues/345 + and uses Apple Script to tell OS X’s System Events to put a certain + process in the foreground, focused and at the top position in the + application stack. (I.e. cmd+tab behaves as expected.) + """ + if sys.platform == 'darwin' and shutil.which('/usr/bin/osascript'): + script = 'tell application "System Events" \ + to set frontmost of the first process whose unix id is {pid} to true'.format(pid=os.getpid()) + os.system("/usr/bin/osascript -e '{script}'".format(script=script)) + + class TkViewer: """ Initialises Tk based viewer for the game. @@ -78,6 +93,10 @@ def run(self): # schedule next read self.root.after_idle(self.app.read_queue) try: + # Try our best to get the application to the front. + self.root.lift() + force_frontmost() + self.root.mainloop() except KeyboardInterrupt: pass