如何在命令行更改Unity主题?实现Ubuntu 16.04明暗主题自动切换
在Ubuntu 16.04中命令行切换Unity主题并实现每日自动切换
我来帮你搞定自动切换亮色/暗色主题的需求!先从命令行手动切换说起,再一步步设置定时自动切换~
一、命令行手动切换Unity主题
Unity的主题设置主要分为三个核心部分:应用程序GTK主题、Unity Shell主题(顶部面板/启动器样式)、窗口装饰主题。我们可以用gsettings命令直接修改这些设置,完全替代Unity Tweak Tool的手动操作。
1. 先确认你的主题名称
先查看当前正在使用的亮色/暗色主题名称,执行以下命令:
# 查看当前GTK应用主题 gsettings get org.gnome.desktop.interface gtk-theme # 查看当前Unity Shell主题 gsettings get com.canonical.Unity.Interface shell-theme # 查看当前窗口装饰主题 gsettings get org.gnome.desktop.wm.preferences theme
Ubuntu 16.04默认的亮色主题是Ambiance,暗色主题是Radiance,你可以把下面命令里的主题名替换成你实际在用的。
2. 切换到亮色主题
执行以下命令一键切换:
gsettings set org.gnome.desktop.interface gtk-theme "Ambiance" gsettings set com.canonical.Unity.Interface shell-theme "Ambiance" gsettings set org.gnome.desktop.wm.preferences theme "Ambiance"
3. 切换到暗色主题
同理,执行:
gsettings set org.gnome.desktop.interface gtk-theme "Radiance" gsettings set com.canonical.Unity.Interface shell-theme "Radiance" gsettings set org.gnome.desktop.wm.preferences theme "Radiance"
二、设置每日自动切换主题
我们可以用系统自带的cron定时任务工具,让系统每天自动在指定时间切换主题。
1. 获取必要的环境变量
gsettings需要依赖用户的会话总线才能生效,先获取你的用户UID和会话总线地址:
# 查看你的用户UID(一般是1000) id -u # 查看会话总线地址 echo $DBUS_SESSION_BUS_ADDRESS
输出类似unix:path=/run/user/1000/bus,记下来后面要用。
2. 创建切换主题的脚本(可选但更整洁)
为了避免cron命令太长,我们可以把切换命令写成脚本。比如在你的用户目录下创建两个脚本:
亮色主题切换脚本 switch-light-theme.sh
#!/bin/bash # 切换为亮色主题 gsettings set org.gnome.desktop.interface gtk-theme "Ambiance" gsettings set com.canonical.Unity.Interface shell-theme "Ambiance" gsettings set org.gnome.desktop.wm.preferences theme "Ambiance"
暗色主题切换脚本 switch-dark-theme.sh
#!/bin/bash # 切换为暗色主题 gsettings set org.gnome.desktop.interface gtk-theme "Radiance" gsettings set com.canonical.Unity.Interface shell-theme "Radiance" gsettings set org.gnome.desktop.wm.preferences theme "Radiance"
然后给脚本添加执行权限:
chmod +x ~/switch-light-theme.sh ~/switch-dark-theme.sh
3. 添加定时任务
打开用户的crontab编辑界面:
crontab -e
如果是第一次打开,会让你选择编辑器,选nano就好。
在文件末尾添加以下定时规则(替换成你的UID和脚本路径):
# 每天早上8点切换为亮色主题 0 8 * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus ~/switch-light-theme.sh # 每天晚上8点切换为暗色主题 0 20 * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus ~/switch-dark-theme.sh
如果你不想用脚本,也可以直接把切换命令写进去:
# 每天早上8点切换亮色 0 8 * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus gsettings set org.gnome.desktop.interface gtk-theme "Ambiance" && gsettings set com.canonical.Unity.Interface shell-theme "Ambiance" && gsettings set org.gnome.desktop.wm.preferences theme "Ambiance" # 每天晚上8点切换暗色 0 20 * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus gsettings set org.gnome.desktop.interface gtk-theme "Radiance" && gsettings set com.canonical.Unity.Interface shell-theme "Radiance" && gsettings set org.gnome.desktop.wm.preferences theme "Radiance"
保存退出后,cron就会自动按照设定的时间执行切换操作了!
内容的提问来源于stack exchange,提问作者user152037
相关产品推荐
相关产品推荐

