This Python script monitors specified applications' active windows (such as Microsoft Office and Teams) and changes the default browser based on which application is in focus. Google Chrome is set for monitored applications, while Edge is used for everything else.
- Monitors specified applications' active windows using Windows events.
- Switches the default browser based on the focused application.
- Efficiently detects application focus changes without constant polling.
- Allows customization of browsers and applications to monitor.
- Python 3.x
- Required Python Libraries:
psutil
- SetUserFTA utility - https://kolbi.cz/blog/2017/11/10/setdefaultbrowser-set-the-default-browser-per-user-on-windows-10-and-server-2016-build-1607/
Written for use on Windows 11, but should also work on Windows 10.
-
Set up the environment:
pip install psutil
-
Modify the list of applications to monitor by adjusting the APPS constant in the script.
-
Customize the browsers by modifying the BROWSERS constant. (The first browser is set for monitored apps.)
-
If DEBUG_MODE is set to True, the script will print debug messages. Set to False to disable (default).
-
Run the script:
python script_name.py
- Monitors the active window of specified applications: Microsoft Teams, Outlook, and Chrome by default.
- If one of the specified applications is active for more than 5 seconds, the default browser is set to Chrome. (Chrome must be in the monitored list so that while in Chrome, the default browser isn't reset to Edge.)
- If any other application is active for more than 5 seconds, the default browser is set to Edge.
- The script uses
SetUserFTA
command to change the default browser. Ensure you have the necessary permissions to change file type associations.
- The
SetUserFTA
command and its functionality are not included in the script. Ensure you have this utility available and adjust the paths as needed. - Microsoft added a setting to Outlook (probably Teams soon as well) to set hyperlink open preferences (choices: system default browser or Edge). For this script to work as expected, you'll want to set this to the system default browser.
To turn the script into a standalone Windows executable, you can use PyInstaller
. This will allow you to run the application without requiring a Python environment.
-
Install
PyInstaller
:pip install pyinstaller
-
Navigate to the directory containing your script and compile it:
pyinstaller --onefile script_name.py
- The
--onefile
flag ensures the output is a single executable file.
- The
-
Once the compilation process is complete, you'll find the standalone executable in the
dist
directory within your current directory.