gsettings 设置 org.gnome.desktop.background 不起作用

gsettings 设置 org.gnome.desktop.background 不起作用

我正在尝试下载每日图片并将其设置为我的背景图片:

#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"

rm -f ${PICS}/wall.jpg
rm -f ${PICS}/photo-of-the-day

# download photo-of-the-day page
wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day

# parse the url out from the file
url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`

# download the photo
wget http:$url -O ${PICS}/wall.jpg

# set the desktop background
URI=file:///${PICS}/wall.jpg
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri ${URI}

图片按预期下载,但背景实际上并未设置。奇怪的是,如果我修改 URI 以包含更多或更少的正斜杠,它就会起作用,但只会这样做一次。最终我不得不每次都以一种毫无意义的方式修改脚本,以使此部分正常工作。

这可能是什么原因造成的?

答案1

尝试以下方法:

#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"

rm -f "${PICS}/wall.jpg"
rm -f "${PICS}/photo-of-the-day"

# download photo-of-the-day page
wget "http://photography.nationalgeographic.com/photography/photo-of-the-day" -O "${PICS}/photo-of-the-day"

# parse the url out from the file
url="`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '\"' -f 2`"

# download the photo
wget "http:$url" -O "${PICS}/wall.jpg"

# set the desktop background
# only two slashes here, because the PICS var already has a leading slash
URI="file://${PICS}/wall.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"

答案2

您可以URI像 cmks 所示那样引用完整变量,或者确保file://wall.jpg被引用,如下所示:

URI="file:///"${PICS}"/wall.jpg"

这是对脚本的一个小改进。变量用于缩短命令行。文件保存到/tmp,每次系统重新启动时都会删除,因此您不必手动清除缓存。AWK 用于改进解析并减少管道。wget直接写入 AWK 以避免保存额外的文件

#!/bin/bash

# set variables to shorten lines
FILE="/tmp/photo_of_the_day"
PAGE="http://photography.nationalgeographic.com/photography/photo-of-the-day"
SEARCH="images.nationalgeographic.com.*cache.*990x742.jpg"

# get image URI directly
IMAGE=$(wget "$PAGE" -O - -o /dev/null  | awk -F'"' -v regex="$SEARCH" '$0~ regex {print $2}')

# download the photo
wget http:$IMAGE -O "$FILE"

# set the desktop background
URI="file:///$FILE"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"

答案3

我认为我发现了这个问题:即使我转到文件,右键单击,然后说“设置为墙纸...”,也没有任何反应。所以我假设文件名每次都不会改变;Linux 有某种节省成本的功能,我实际上不会刷新,因为那是相同的图像。要强制系统每次都识别出这是一张新图片,请像这样改变文件名:

#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"
RAND=$RANDOM

rm -f ${PICS}/*.jpg
rm -f ${PICS}/photo-of-the-day

# download photo-of-the-day page
wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day

# parse the url out from the file
url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`

# download the photo
wget http:$url -O ${PICS}/wall${RAND}.jpg

# set the desktop background
URI="file://${PICS}/wall${RAND}.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"

相关内容