Last active
January 16, 2023 01:27
-
-
Save Flashwalker/8c0fb799c33789820edafa912d9641ed to your computer and use it in GitHub Desktop.
Copy and set your current GTK3 theme over another's Flatpak theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copy and set your current GTK3 theme over another's Flatpak theme. | |
# It's like a raider takeover of some Flatpak theme 😁 🙃. | |
# NOTICE: some Flatpak theme must be already installed. | |
# | |
# What if you set your custom GTK3 theme for your desktop and want it for Flatpak? | |
# Use this script to takeover of another's Flatpak theme and set it! | |
USAGE(){ | |
echo "Copy and set your current GTK3 theme over the chosen Flatpak installed theme:" | |
echo " ${0##*/} [OPTIONS] choose.the.Gtk3Flatpak.Theme" | |
echo | |
echo " Options:" | |
echo " -t Theme-Name Specify theme name (Use it if you want to set other than your current theme)." | |
echo " -o Make flatpak overrides in userspace dir (set global override for env and for themes)." | |
echo " -O Make flatpak overrides in system dir (set global override for env and for themes)." | |
echo " -u Use it if your host theme is in the userspace directory (~/.themes " | |
echo " or ~/.local/share/themes), otherwise it assume to be in the system directory." | |
echo " -h This help." | |
echo " Examples:" | |
echo " Set your current theme over SomeFlatpakTheme:" | |
echo " ${0##*/} -o org.gtk.Gtk3theme.SomeFlatpakTheme" | |
echo " If you want to set another than current theme, e.g. 'Theme-Name':" | |
echo " ${0##*/} -o -t Theme-Name org.gtk.Gtk3theme.SomeFlatpakTheme" | |
echo | |
echo " Don't forget to install some Flatpak theme before run." | |
exit $1 | |
} | |
# Set variables defaults | |
override=false;userspace=false;xfce=false;fuserspace=true; | |
# Command line options | |
while getopts "ht:oOu\?" opt; do | |
case "$opt" in | |
h ) USAGE 1; ;; | |
t ) host_theme_name="$OPTARG"; ;; | |
o ) override=true; ;; | |
O ) override=true; fuserspace=false; ;; | |
u ) userspace=true; ;; | |
\?) USAGE 1; ;; | |
* ) echo "Unknown option '$opt'"; USAGE 1; ;; | |
esac | |
done | |
shift $(($OPTIND -1)) | |
# Set variables | |
user_confdir="${XDG_CONFIG_HOME:-$HOME/.config}" | |
user_themes_dir="${XDG_DATA_HOME:-$HOME/.local/share}"/themes | |
[ -d "$user_themes_dir" ] || user_themes_dir="${HOME}/.themes" | |
sys_themes_dir="/usr/share/themes" | |
if [ -z "$host_theme_name" ]; then | |
xfconf-query -V >& /dev/null | |
hasxfce=$? | |
if ! ((hasxfce == 127)); then | |
xfce=true | |
host_theme_name="$(xfconf-query -c xsettings -p /Net/ThemeName -n)" | |
elif [ -f "${user_confdir}/gtk-3.0/settings.ini" ]; then | |
host_theme_name="$(cat "$user_confdir"/gtk-3.0/settings.ini | grep -m1 "^ *\?gtk-theme-name=" | cut -d = -f 2)" | |
else | |
host_theme_name="$(gsettings get org.gnome.desktop.interface gtk-theme|cut -d \' -f 2)" | |
fi | |
fi | |
if [ -z "$host_theme_name" ]; then | |
echo "Specify you current theme with: -t Theme-Name" | |
USAGE 1 | |
fi | |
if $userspace; then | |
host_theme_dir="$user_themes_dir"/"$host_theme_name" | |
else | |
host_theme_dir="$sys_themes_dir"/"$host_theme_name" | |
fi | |
flat_theme_name="${1##*.}" | |
flat_theme_dir="$(flatpak info --show-location ${1} 2>/dev/null)" | |
if [ -z "$flat_theme_dir" ]; then | |
echo -e "${1} not installed\nAborting." | |
exit 1 | |
fi | |
# Overwrite Flatpak theme | |
expr "$flat_theme_dir" : '^/home' >& /dev/null && cp_cmd="cp" || cp_cmd="sudo cp" | |
if [ -d "$host_theme_dir" ]; then | |
if [ -e "$host_theme_dir"/gtk-3.0/gtk.css ]; then | |
$cp_cmd "$(readlink -f "$host_theme_dir"/gtk-3.0/gtk.css)" "$flat_theme_dir"/files/gtk.css | |
else | |
echo -e "No such file "$host_theme_dir"/gtk-3.0/gtk.css\nAborting." | |
exit 1 | |
fi | |
if [ -e "$host_theme_dir"/gtk-3.0/assets ]; then | |
$cp_cmd -r "$(readlink -f "$host_theme_dir"/gtk-3.0/assets)" "$flat_theme_dir"/files/; fi | |
if [ -e "$host_theme_dir"/gtk-3.0/gtk.gresource ]; then | |
$cp_cmd "$(readlink -f "$host_theme_dir"/gtk-3.0/gtk.gresource)" "$flat_theme_dir"/files/; fi | |
else | |
echo -e "No such directory ${host_theme_dir}\nAborting." | |
exit 1 | |
fi | |
# Override Flatpak stuff | |
if $override; then | |
if $fuserspace; then | |
# Override to ~/.local/share/flatpak/overrides/global | |
echo " override: env: GTK_THEME=${flat_theme_name}" | |
flatpak -u override --env=GTK_THEME="$flat_theme_name" | |
echo " override: filesystem: xdg-data/themes" | |
flatpak -u override --filesystem=xdg-data/themes | |
else | |
# Override to /var/lib/flatpak/overrides/global | |
echo " override: env: GTK_THEME=${flat_theme_name}" | |
sudo flatpak override --env=GTK_THEME="$flat_theme_name" | |
echo " override: filesystem: xdg-data/themes" | |
sudo flatpak override --filesystem=xdg-data/themes | |
fi | |
fi | |
# Set taken over theme for apps | |
for dir in ${HOME}/.var/app/*; do | |
flat_confdir="${dir}/config/gtk-3.0" | |
mkdir -p "$flat_confdir" | |
cp "${user_confdir}/gtk-3.0/settings.ini" "${flat_confdir}"/ | |
sed -ri "\:^\s*?gtk-theme-name=:s/(gtk-theme-name=).*/\1"$flat_theme_name"/" "$flat_confdir"/settings.ini | |
if $xfce; then | |
flat_xfconfdir="${dir}/config/xfce4/xfconf/xfce-perchannel-xml" | |
mkdir -p "$flat_xfconfdir" | |
cp "${user_confdir}/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml" "$flat_xfconfdir"/ | |
sed -ri '\:name="ThemeName":s/(value=)".+?"/\1"'$flat_theme_name'"/' "$flat_xfconfdir"/xsettings.xml | |
fi | |
flatpak info "${dir##*/}" >& /dev/null && \ | |
flatpak run --command=bash<<<"gsettings set org.gnome.desktop.interface gtk-theme $flat_theme_name" "${dir##*/}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment