自动亮/暗模式?

自动亮/暗模式?

有没有人找到一种方法来自动在“亮”和“暗”模式之间切换,关于 Ubuntu 设置中的“窗口主题”(设置 →外貌窗口主题浅色 / 标准 / 深色)?

深入了解设置每一个早上或晚上真的很麻烦。如果能够自动地日落时切换到“黑暗”模式,并且自动地日出时切换到“灯光”模式。

答案1

对于 GNOME,存在以下 shell 扩展:夜间主题切换器

它具有相当多的选项,开箱即可使用,无需配置任何内容,而且配置也很简单!

计划选项卡的屏幕截图

GTK 主题选项卡的屏幕截图

答案2

更改主题的终端命令是:

gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark

对于 Yaru-dark 主题,以及

gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

用于 Yaru-light 主题。

现在,有一种名为 cron-job 的东西可以用于调度作业(基本上就是在特定时间重复执行某项任务)。因此,您可以编写一个 cron-job 来在指定时间执行这些命令(例如,晚上 9 点切换到深色主题,早上 6 点切换到浅色主题)。

将以下内容添加到名为的文件中script.sh

#!/bin/bash
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > lightscript.sh
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > darkscript.sh
echo "gsettings set org.gnome.desktop.interface gtk-theme Yaru-light" >> lightscript.sh
echo "gsettings set org.gnome.desktop.interface gtk-theme Yaru-dark" >> darkscript.sh
chmod 755 lightscript.sh
chmod 755 darkscript.sh

currenttime=$(date +%H:%M)
if [[ "$currenttime" > "21:00" ]] || [[ "$currenttime" < "06:00" ]]; then
  ./darkscript.sh
else
  ./lightscript.sh
fi

使文件可执行运行:

chmod 755 /path/to/script.sh

或者:

chmod +x /path/to/script.sh

在终端中运行gnome-session-properties。单击右侧的添加按钮添加一个新的启动程序,然后script.sh通过浏览选择文件,并用名称和注释保存。这将告诉 GNOME 创建lightscript.shdarkscript.sh随时通过 GUI 登录。

crontab使用以下命令添加您的工作(更改主题) :

crontab -e

并选择合适的编辑器,或者您可以转到/var/spool/cron/crontabs并使用您的用户名编辑文件。以这种方式访问​​文件需要sudo权限。添加以下两行(/path/to/替换为实际路径):

0 6 * * * /path/to/lightscript.sh
0 21 * * * /path/to/darkscript.sh

它会说:

crontab: installing new crontab

退出命令后。您还可以使用以下命令进行检查:

crontab -l

这样就可以了。上面两行代码告诉每天早上 6:00 和晚上 9:00cron执行。lightscript.shdarkscript.sh

我们正在走这条弯路,而不是仅仅添加:

0 6 * * * gsettings set org.gnome.desktop.interface gtk-theme Yaru-light

因为crontab这需要DBUS_SESSION_BUS_ADDRESS正确设置变量。

答案3

我们可以借助crontab和来实现这一点@shubhzgang回答

#!/bin/bash

# Script that auto switches Ubuntu themes to Dark or Light, 
# depending on the time of day

# Copy this script file to /usr/local/bin/my-auto-theme
# Add following lines to crontab -e
# 0 9 * * * bash /usr/bin/local/my-auto-theme light
# 0 17 * * * bash /usr/bin/local/my-auto-theme dark
# @reboot bash /usr/bin/local/my-auto-theme

set_theme() {
    if [[ "$1" == "dark" ]]; then
        new_gtk_theme="Yaru-dark"
    elif [[ "$1" == "light" ]]; then
        new_gtk_theme="Yaru-light"
    else
        echo "[!] Unsupported theme: $1"
        return
    fi
    
    # https://askubuntu.com/a/1234819/895417
    export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS
    current_gtk_theme=$(gsettings get org.gnome.desktop.interface gtk-theme)
    # echo "[.] Currently using ${current_gtk_theme}"
    if [[ "${current_gtk_theme}" == "'${new_gtk_theme}'" ]]; then
        echo "[i] Already using gtk '${new_gtk_theme}' theme"
    else
        echo "[-] Setting gtk theme to ${new_gtk_theme}"
        gsettings set org.gnome.desktop.interface gtk-theme ${new_gtk_theme}
        echo "[✓] gtk theme changed to ${new_gtk_theme}"
    fi
}

# If script run without argument
if [[ -z "$1" ]]; then
    currenttime=$(date +%H:%M)
    if [[ "$currenttime" > "17:00" || "$currenttime" < "09:00" ]]; then
        set_theme dark
    else
        set_theme light
    fi
else
    set_theme $1
fi

我们添加了脚本,@reboot这样即使 PC 在 cron 触发时间关闭,脚本也会运行。

答案4

我编写了一个脚本来实现这一点,我喜欢这个结果,它的工作方式类似于 gnome autodarkmode,带有阳光和日出时间,请看一下:

https://github.com/jackfido/nightMode

您需要连接互联网才能获取日出和日落数据,具体方法如下:

获取SunRiseAndSet.sh

#!/bin/bash

# 1.- Tihs files pretends to get the sunset and sunrise values from: https://www.timeanddate.com/sun/mexico/
# 2.- To save this values into a temporal file, called as the "yourlocation".out
# 3.- Then compares the dates; from the last modified file date to current date in format YYYYmmdd to determine if we have the today values
# 4.- If the file is for yersterday or older then we have to go for the values for today, else, do nothing 
# 
# Additional:
#       run THIS file in startup applications to run when your session start after a shotdown or reboot
#       run crontab -e to run nightMode.sh file, directly, every minute to set theme as soon as the sunset/sunrise occurss -> * * * * * ~/Public/nightMode/nightMode.sh
#       run THIS task each 3,4, 6 or 8 hours, it depends on how many times you want to check if you download the updated file, it allow us to have the values up-to-date in case of pc enters in hibernatin or freeze state during a day change

# file name and location
location="monterrey"
tmpfile=~/Public/nightMode/tmp/$location.out

# get dates from last file and current
lastUpdate=$(date -r $tmpfile +"%Y%m%d")
currentDate=$(date +%Y%m%d)

# if file doesnt exists
if [ ! -z "$lastUpdate" ]; then
    # compare file date with current date to determine if need download the values
    if [ "$lastUpdate" -lt "$currentDate" ]; then
        wget -q "https://www.timeanddate.com/sun/mexico/$location" -O "$tmpfile"

        echo "Monterrey time getted successfully"
        bash ~/Public/nightMode/nightMode.sh
    fi
else
    wget -q "https://www.timeanddate.com/sun/mexico/$location" -O "$tmpfile"

    echo "Monterrey time getted successfully"
    bash ~/Public/nightMode/nightMode.sh
fi

夜间模式

    #!/bin/bash
location="monterrey"
tmpfile=~/Public/nightMode/tmp/$location.out

SUNR=$(grep "Sunrise Today" "$tmpfile" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | head -1)
SUNS=$(grep "Sunset Today" "$tmpfile" | grep -oE '((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))' | tail -1)

# Use $sunrise and $sunset variables to fit your needs. Example:
sunrise=$(date --date="$SUNR" +%Y%m%d%H%M)
sunset=$(date --date="$SUNS" +%Y%m%d%H%M)

hour=$(date +%Y%m%d%H%M)

if [  $hour -gt $sunrise ] && [ $hour -lt $sunset ];
then
    bash ~/Public/nightMode/day.sh
fi

if [ $hour -lt $sunrise ] || [ $hour -gt $sunset ];
then
    bash ~/Public/nightMode/night.sh
fi

day.sh:

#!/bin/bash

PID=$(pgrep compiz)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

# theme2Stablish='Materia-light-compact'
theme2Stablish='Yaru'
theme=$(gsettings get org.gnome.desktop.interface gtk-theme)

if [ "$theme" != "$theme2Stablish" ];
then
    gsettings set org.gnome.desktop.interface gtk-theme $theme2Stablish
    gsettings set org.gnome.desktop.interface color-scheme 'prefer-light'
    gsettings set org.gnome.desktop.interface icon-theme 'Papirus-Light'
#    notify-send "Day theme stablished"
fi

夜晚.sh

#!/bin/bash

PID=$(pgrep compiz)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

# theme2Stablish='Materia-dark-compact'
theme2Stablish='Yaru-unity-dark'
theme=$(gsettings get org.gnome.desktop.interface gtk-theme)

if [ "$theme" != "$theme2Stablish" ];
then
    gsettings set org.gnome.desktop.interface gtk-theme $theme2Stablish
    gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'
    gsettings set org.gnome.desktop.interface icon-theme 'Papirus-Dark'
#    notify-send "Night theme stablished"
fi

然后将 nightMode 添加到 crontab:

# Create with crontab -e command:

* * * * * ~/Public/nightMode/nightMode.sh
0 */6 * * * ~/Public/nightMode/getSunRiseAndSet.sh

我开始根据此页面进行工作

https://linuxconfig.org/how-to-obtain-sunrise-sunset-time-for-any-location-from-linux-command-line

然后,在你的程序中配置自动暗模式,比如在 github 桌面、Android Studio、firefox 等中

这是我的结果:

灯光模式:

灯光模式

暗模式:

暗黑模式

问候

相关内容