Juste envie de partager un petit script bash que j'ai écris pour switcher la configuration du bureau (XFCE) à partir d'une certaine heure. L'idée c'était de pouvoir changer automatiquement à partir d'une certaine heure les icones, le style d'affichage, le fond d'écran, la coloration du terminal et le thème des fenêtres en une seule fois sans avoir à cliquer partout dans le menu Apparence et gestion des fenêtres.
Voici le script que j'ai fait, c'est assez rudimentaire mais ça marche:
Code : Tout sélectionner
#!/bin/bash
# Version 1.0
# Date création: 2021/03/04
# For crontab management:
PID=$(pgrep xfce4-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
# For changing theme depending of the hour of the day
current_date=$(date '+%H') # Find the current hour
ref_date=20 # Hour used as reference for switching color
# Define the list of workspace to be changed
listWorkSpace=$(xfconf-query -c xfce4-desktop -lv | awk '{print $1; }' | grep HDMI.*last-image)
if ((${current_date#0} < ${ref_date#0})) ; then
#echo "$current_date < $ref_date ; Using Theme bleu"
echo "It's $current_date , it's still early, let's set the blue theme"
xfconf-query -c xfwm4 -p /general/theme -s Futurist-hackerist # Change Window theme
xfconf-query -c xsettings -p /Net/IconThemeName -s Abyss-DEEP-Suru-GLOW # Change Icon theme
xfconf-query -c xsettings -p /Net/ThemeName -s Mint-Y-Dark-Teal # Change Appearance Style
cp /data/Linux_Projects/Themes/TERMINAL_CYAN ~/.config/xfce4/terminal/terminalrc # Changing terminal col
# Changing backgrounds
for i in $listWorkSpace; do
xfconf-query -c xfce4-desktop -p $i -s /data/Linux_Projects/Themes/sele_ring_center.jpg
done
exit 0
else
echo "It's past $ref_date, now it's time for setting the red theme"
xfconf-query -c xfwm4 -p /general/theme -s Futurist-hackerist_Red # Change Window theme
xfconf-query -c xsettings -p /Net/IconThemeName -s Abyss-BLOOD-Suru-GLOW # Change Icon Theme
xfconf-query -c xsettings -p /Net/ThemeName -s Mint-Y-Dark-Red # Change Appearance Style
cp /data/Linux_Projects/Themes/TERMINAL_RED ~/.config/xfce4/terminal/terminalrc # Changing terminal col
# Changing backgrounds
for i in $listWorkSpace; do
xfconf-query -c xfce4-desktop -p $i -s /data/Linux_Projects/Themes/sele_ring_center_red.jpg
done
exit 0
fi
# TO ADD:
# Changer l'icone du menu
Code : Tout sélectionner
SHELL=/bin/bash
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 * * * * ./SwitchTheme.sh >> /data/logSh.txt
et après 20h, ce thème:
(pour des soucis logistique, j'ai modifié la valeur dans le script et la fréquence d’exécution du script dans cron pour pas à avoir à attendre 20h pour faire le screenshot).
Bon, partager un script sans l’expliquer c'est pas hyper intéressant. Donc voici quelques explications sur le cheminement et les problèmes que j'ai rencontré pour faire ce script. Ça pourra peut être aider d'autres membres ayant envie de se lancer dans la création de script.
1. Rassembler les éléments de chaque configuration.
Pour mon bureau "Bleu" sous XFCE, j'utilise:
- le pack d'icone Abyss-DEEP-Suru-Glow disponible ici: https://www.gnome-look.org/s/Gnome/p/1333376
- le thème de fenêtre Futurist-hackerist, disponible ici: https://www.xfce-look.org/p/1320508/
- le style Mint-Y-Dark-Teal et une coloration du terminal en cyan
Pour la configuration "Rouge", je vais donc colorer tous les éléments cités ci-dessus.
Pour le pack d’icône, c'est facile puisque qu'il existe un pack Abyss-BLOOD-Suru-Glow. Pour le style, j'utiliserai Mint-Y-Dark-Red, là aussi rien de compliquer.
Par contre pour le thème des fenêtres, Futurist-Hackerist n'existe que en bleu cyan, j'ai donc du manuellement recolorer tout le bleu en rouge. Pour ça j'ai fait une copie du thème qui se nomme Futurist-Hackerist_Red et j'ai utilisé Image Magick et la fonction "convert" qui avec les arguments "-fill" et "-opaque" permet d'utiliser une couleur pour en remplacer une autre. Une petite loop pour appliquer les changements à tous les fichiers et voilà:
Code : Tout sélectionner
#!/bin/bash
cd /data/Linux_Projects/Themes/Futurist-hackerist_Red/xfwm4
list_files=$(ls *.xpm)
for i in $list_files;
do
convert $i -fill "#5d000d" -opaque "#007aa6" $i
convert $i -fill "#47000a" -opaque "#003b51" $i
echo $i
done
exit 0
Code : Tout sélectionner
convert sele_ring_center.jpg -fuzz 10% -fill "#5d000d" -opaque "#d2d2d2" sele_ring_center_red.jpg
Code : Tout sélectionner
cp /data/Linux_Projects/Themes/TERMINAL_CYAN ~/.config/xfce4/terminal/terminalrc
Les thèmes Futurist-hackerist et Futurist-hackerist_Red dans /usr/share/themes
Pour les fichiers terminalrc et les fonds d'écrans, je les ai mis sur ma partition /data/ afin de pouvoir les manipuler plus facilement si besoin.
2. Remplacer les différents élément du bureau: xfconf-query
Cyrille avait fait une présentation de l'utilisation de xfconf-query pour remplacer le fond d'écran dans ce tuto: viewtopic.php?f=12&t=15738
J'ai repris cette commande pour changer les paramètres. Avec "xfconf-query -c xsettings -lv", on peut obtenir les valeurs à modifier pour changer les icônes et le style
Exemple:
Code : Tout sélectionner
xfconf-query -c xsettings -p /Net/IconThemeName -s Abyss-DEEP-Suru-GLOW
xfconf-query -c xsettings -p /Net/ThemeName -s Mint-Y-Dark-Teal
Code : Tout sélectionner
xfconf-query -c xfwm4 -p /general/theme -s Futurist-hackerist
Code : Tout sélectionner
$ xfconf-query -c xfce4-desktop -lv | awk '{print $1; }' | grep HDMI.*last-image
/backdrop/screen0/monitorHDMI-A-0/workspace0/last-image
/backdrop/screen0/monitorHDMI-A-0/workspace1/last-image
/backdrop/screen0/monitorHDMI-A-0/workspace2/last-image
/backdrop/screen0/monitorHDMI-A-0/workspace3/last-image
/backdrop/screen0/monitorHDMI-A-1/workspace0/last-image
/backdrop/screen0/monitorHDMI-A-1/workspace1/last-image
/backdrop/screen0/monitorHDMI-A-1/workspace2/last-image
/backdrop/screen0/monitorHDMI-A-1/workspace3/last-image
Code : Tout sélectionner
#!/bin/bash
listWorkSpace=$(xfconf-query -c xfce4-desktop -lv | awk '{print $1; }' | grep HDMI.*last-image)
for i in $listWorkSpace; do
xfconf-query -c xfce4-desktop -p $i -s /data/Linux_Projects/Themes/sele_ring_center_red.jpg
done
exit 0
3. Test conditionnel suivant l'heure:
Maintenant, je vais appliquer le thème rouge et bleu selon l'heure:
Code : Tout sélectionner
#!/bin/bash
current_date=$(date '+%H') # Find the current hour
ref_date=21 # Hour used as reference for switching color
if ((${current_date#0} < ${ref_date#0})) ; then
echo "It's $current_date , it's still early, let's set the blue theme"
else
echo "It's past $ref_date, now it's time for setting the red theme"
Sinon vous aurez des erreurs pour des heures inférieurs à 10h. Plus de détails ici: https://stackoverflow.com/questions/247 ... oken-is-08
Ensuite, j'ai rendu le script executable:
Code : Tout sélectionner
sudo chmod +x SwitchTheme.sh
Pour cron, je me suis ajouté en tant qu'utilisateur dans le fichier /etc/cron.allow
Puis j'ai édité le crontab avec "crontab -e"
Code : Tout sélectionner
SHELL=/bin/bash
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 * * * * ./SwitchTheme.sh >> /data/logSh.txt
En fait, c'était due à une erreur du display. En gros, de ce que j'ai compris, cron ne s'execute pas dans le même environnement que l'environnement de l'utilisateur, donc pour lui dire où effectuer les opérations, il faut rajouter ces deux lignes dans le script:
Code : Tout sélectionner
PID=$(pgrep xfce4-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
Code : Tout sélectionner
~$ env -i sh -c ./SwitchTheme.sh
Voilà. Je crois que j'ai tout dit. Je pense que j'aurais pu déclarer les variables à modifier au début, où même ajouter un argument au script pour définir l'heure de bascule. c'est perfectible mais pour le moment ça marche. Si vous avez des idées pour améliorer le script, où d'autres astuces à partager, je suis preneur