Window
This was tested using Python 2.7.8, with the AppActivate double trick posted by partybuddha and the sleep trick posted by eryksun :
from webbrowser import open as browse from win32com.client import Dispatch from win32gui import GetForegroundWindow from win32process import GetWindowThreadProcessId from time import sleep images=['http://serverfault.com', 'http://stackoverflow.com'] for rP in images: _, pid = GetWindowThreadProcessId(GetForegroundWindow()) browse(rP) sleep(1) shell = Dispatch("WScript.Shell") shell.AppActivate(pid) shell.AppActivate(pid) decision = raw_input('Is image ' + str(rP) + ' ok? ')
Without a 1 second sleep, the browser left focus, as if AppActivate was happening too soon.
OS X
#!/usr/bin/env python from webbrowser import open as browse from subprocess import check_output images=['http://serverfault.com', 'http://stackoverflow.com'] for rP in images: term = check_output(['/usr/bin/osascript', '-e', 'copy path to frontmost application as text to stdout']).strip() browse(rP) check_output(['/usr/bin/osascript', '-e', 'tell application "%s" to activate' % term]) decision = raw_input('Is image ' + str(rP) + ' ok? ')
This code has been tested with Python 2.7.5, so it may take some time to work with Python 3, but the main idea is to use osascript to get the very first application, and then activate it after opening the URL in a browser window.
source share