====== Setup different default browsers in KDE ======
If you use 2 desktops in KDE, you can use different default browsers in these desktops. I like having Firefox as default browser for Personal desktop and Google Chrome for Work.
===== Create a shell script with logic for choosing =====
Let's put it into //$HOME/scripts/smart-browser.sh//:
#!/usr/bin/env bash
# --- Configuration ---
LOG_DIR="$HOME/temp/smartbrowser_logs"
LOGFILE="$LOG_DIR/smartbrowser.log"
# ---------------------
# 1. Ensure the log directory exists
mkdir -p "$LOG_DIR"
# 2. Check and perform log rotation (clear file if not modified today)
CURRENT_DATE=$(date +'%Y-%m-%d')
# Get the last modification date of the log file
if [ -f "$LOGFILE" ]; then
# Use 'stat' command to get file modification time (platform specific, but common)
# The format specifier depends on the OS. On Linux (using GNU stat):
# %y: last modification time
# %Y: last modification time in seconds since epoch (simpler for comparisons if needed)
# We'll use the formatted date for simplicity:
LOG_DATE=$(stat -c '%Y' "$LOGFILE")
LOG_MOD_DATE=$(date -d "@$LOG_DATE" +'%Y-%m-%d')
else
# If the file doesn't exist yet, treat its date as 'old' to trigger creation/touch
LOG_MOD_DATE=""
fi
if [[ "$LOG_MOD_DATE" != "$CURRENT_DATE" ]]; then
# Log file is from a previous day (or doesn't exist). Recreate/truncate it.
# The 'true > $LOGFILE' command effectively empties the file.
echo "[INFO] Log file rotated (previous date: $LOG_MOD_DATE)." > "$LOGFILE"
echo "---" >> "$LOGFILE"
fi
# The file is now either newly created, truncated, or from today.
# 3. Get the current desktop number (KDE/KWin specific)
current_desktop=$(qdbus org.kde.KWin /KWin currentDesktop)
# 4. Log the script call details
TIMESTAMP=$(date +'%Y-%m-%d %H:%M:%S')
printf '[%s] Current desktop: %s, Arguments: %s\n' "$TIMESTAMP" "$current_desktop" "$*" >> "$LOGFILE"
# 5. Select and launch the browser based on the desktop number
if [[ "$current_desktop" == "1" ]]; then
echo "[INFO] Launching Google Chrome..." >> "$LOGFILE"
google-chrome "$@" >> "$LOGFILE" 2>&1
elif [[ "$current_desktop" == "2" ]]; then
echo "[INFO] Launching Firefox..." >> "$LOGFILE"
firefox "$@" >> "$LOGFILE" 2>&1
else
# Default to Firefox for all other desktops
echo "[INFO] Launching Firefox (default)..." >> "$LOGFILE"
firefox "$@" >> "$LOGFILE" 2>&1
fi
echo "---" >> "$LOGFILE"
exit 0
Use file //$HOME/temp/smartbrowser_logs/smartbrowser.log// to view logs.
===== Create .desktop file =====
Create a file //~/.local/share/applications/smart-browser.desktop//:
[Desktop Entry]
Version=1.0
Name=Smart Browser
Comment=Context-aware browser launcher
Exec=bash -c "$HOME/scripts/smart-browser.sh %u"
Icon=web-browser
Terminal=false
Type=Application
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https;
===== Set as default browser =====
xdg-settings set default-web-browser smart-browser.desktop
xdg-mime default smart-browser.desktop x-scheme-handler/http x-scheme-handler/https
===== Verify (optional) =====
xdg-settings get default-web-browser
xdg-mime query default x-scheme-handler/https
xdg-mime query default x-scheme-handler/http
xdg-open 'http://example.com'
xdg-open 'https://www.google.com'