ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

sh/bash/dash/ksh/zsh等Shell脚本
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#1

帖子 Qunero » 2010-08-18 17:33

相信大家已经了解到了ubuntu自带的动态桌面背景了,有自己的图片集,也想让其中的图片动态作为自己的桌面背景怎么办???
背景知识参考:http://qyiyunso.blog.163.com/blog/stati ... 185273331/
不知大家在使用ubuntu时有没有注意到,ubuntu下是可以实现多张壁纸动态切换的,好像Win7下也有类似的功能(曾经使用win7一段时间)。我也是最近才注意到这点,下面是在ubuntu10.04下实验的,之前版本的应该也可以。
实现原理是使用一个xml文件来记录可供切换选择的壁纸。下面展示的是10.04中自带的一个样例。
首先说明一下,ubuntu默认的壁纸存放在/usr/share/backgrounds/目录下的,在该目录中还有一个cosmos(意思是“宇宙”)目录,cosmos里面的xml文件就是实现动态桌面壁纸切换功能的了。
现在关键是生成相应的xml 配置文件了,里面的类容比较繁杂,手动更新太麻烦了,所以想到以脚本实现。

1. perl 写成的源代码如下:

代码: 全选

#!/usr/bin/perl 
#==============================================================================#
#-------------------------------help-info-start--------------------------------#
=head1 Name

	getBackgroundXML.pl --> generate the background.xml file to change Ubuntu background picture dynamiclly

=head1 Usage

	perl  getBackgroundXML.pl  [<options>] [-d PicDir='.'] [-t TimeToLast=1795.0] [-i Interval=5.0] [-o Background.xml=STDIN]

	-help       print this help to screen
	-a          flag ,if set ,write all pictures to one xml file
	-d          directory contains the jpgs, defalt : '.'
	-t          time to change background picture, unit : seconds, default : 1795.0
	-i          interval time spend to change the two pictures, default : 5.0
	-o          write result to a file , default : STDOUT

=head1 Example

	perl  getBackgroundXML.pl  -d Windows7 -t 25 -i 5 -o Windows7.xml
	perl  getBackgroundXML.pl  --

=head1 Version

	Verion	:	2.0
	Created	:	08/18/2010 03:34:52 PM 
	Updated	:	08/18/2010 09:51:11 PM
	LastMod	:	---


=head1 Contact

	Author	:	QuNengrong (Qunero)
	E-mail	:	[email protected],[email protected]
	Company	:	BGI

=cut
#-------------------------------help-info-end--------------------------------#
#============================================================================#
use strict;
use warnings;
use Getopt::Long;

my ($Need_help, $Out_file, $PicDir, $TimeToLast, $Interval, $AllInOne );
GetOptions(
	"help"		=> \$Need_help,
	"a"			=> \$AllInOne,
	"d=s"		=> \$PicDir,
	"t=i"		=> \$TimeToLast,
	"i=i"		=> \$Interval,
	"o=s"		=> \$Out_file,
);

die `pod2text $0` if ($Need_help);

#============================================================================#
#                              Global Variable                               #
#============================================================================#
my $Input_file  = $ARGV[0]  if (exists $ARGV[0]); 
$PicDir ||= '.';
$TimeToLast ||= 1795.0;
$Interval ||= 5.0;
$PicDir =~ s/\/$//;
$PicDir =~ s/ /\\ /g;                           # replace SPACE to \SPACE ;
#print STDERR "$0 -d $PicDir -t $TimeToLast \n";
#============================================================================#
#                               Main process                                 #
#============================================================================#

if(defined $Input_file)
{ open(STDIN, '<', $Input_file) or die $!; }
if(defined $Out_file)
{ open(STDOUT, '>', $Out_file) or die $!; }

print STDERR "---Program\t$0\tstarts--> ".localtime()."\n";
	
# step 01: getBackgroundXML
&getBackgroundXML();

print STDERR "---Program\t$0\t  ends--> ".localtime()."\n";

#============================================================================#
#                               Subroutines                                  #
#============================================================================#

sub getBackgroundXML(){
#	my @picFiles = `ls $PicDir |grep .jpg`;     # it's better than glob , in case $PicDir is ~/subDir
	my @picFiles ;
	if( $AllInOne ){
#		@picFiles = glob ( "$PicDir/*/*.jpg" );
		@picFiles = `find $PicDir/ -name "*\.jpg"`;
	}
	else {
		@picFiles = glob ( "$PicDir/*.jpg" );
	}

#	print STDERR "test files:\n\t", join( "\t", @picFiles );
	chomp( @picFiles );
#=test

	if( $PicDir =~ /^\// ){
		for( @picFiles ){
#			$_ = "$PicDir/$_";                  # already full path;
			$_ =~ s/ /\\ /g;
		}

	} 
	else {                # path start with ./ OR ../ OR subDir/
		my $curDir = `pwd`;
		chomp( $curDir );
		for( @picFiles ){
			$_ = "$curDir/$_";
			$_ =~ s/ /\\ /g;
			$_ =~ s/\/\.\//\//g;                # change /./ to /
			$_ =~ s/\/[^\/]+\/\.\.\//\//;       # change path/dir/../anotherDir/ to path/anotherDir/
		}
	}
	
	#	print STDERR join( "\n", @picFiles );

	my $oldjpg = $picFiles[-1];
	print STDOUT 
"<background>
	<starttime>
		<year>2010</year>
		<month>08</month>
		<day>18</day>
		<hour>00</hour>
		<minute>00</minute>
		<second>00</second>
	</starttime>
	<!-- This animation will start now. -->\n";

	for ( @picFiles ){
		printf STDOUT 
"	<static>
		<duration>%.1f</duration>
		<file>$oldjpg</file>
	</static>
	<transition>
    	<duration>%.1f</duration>
		<from>$oldjpg</from>
		<to>$_</to>
	</transition>\n", $TimeToLast, $Interval ;
	$oldjpg = $_;
	}
	print "</background>\n";
#=cut
}
已经修改成第二版了~~说明如下:
1. 可以附件的两个脚本放入自己的用户程序文件夹中 :~/bin,如有必要将这个文件夹加入到环境变量中方便直接调用。
2. 可以用 -h 参数 查看其中的帮助文档,getBackgroundXML.pl 为主要脚本,quickXML.pl 调用前者,快速将一个文件夹中的各个子文件夹 分别 生成动态背景图的XML配置文件。
3. 快速说明: -d 指定 PicDir , 使用 -a 则将PicDir下的所有jpg放入一个xml文件中, -t 指定每个图的显示时间(默认1795s, 加上切换时间就是 半小时换一张图) ,-i 指定 切换快慢(默认5s ,有缓慢的渐变效果), -o 指定生成xml文件存放的地方,默认输出到屏幕的,最好根据图片分类,eg: -o Windows7.xml 。
4. 要处理一个文件夹下有很多不同子文件夹的,可以利用 quickXML.pl ,将其放入相应文件夹中,运行就会按照文件夹名字生成配置文件了,想将所有的生成到一个 图片控制文件 则用-a 参数。举个例子:

代码: 全选

mintqnr@mintqnr-desktop ~/Pictures/wallpaper $ cd
mintqnr@mintqnr-desktop ~ $ cd Pictures/wallpaper/
mintqnr@mintqnr-desktop ~/Pictures/wallpaper $ ls
quickXML.pl  sony      另类          晶莹剔透效果  绿色主题  花样年华
Qunero-xml   Windows7  抽象视觉效果  炫彩花卉      花卉
mintqnr@mintqnr-desktop ~/Pictures/wallpaper $ ./quickXML.pl 
---Program	./quickXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:13 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:13 2010
---Program	./quickXML.pl	  ends--> Thu Aug 19 00:58:13 2010
mintqnr@mintqnr-desktop ~/Pictures/wallpaper $ ./quickXML.pl -a
---Program	./quickXML.pl	starts--> Thu Aug 19 00:58:28 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	starts--> Thu Aug 19 00:58:28 2010
---Program	/home/mintqnr/bin/getBackgroundXML.pl	  ends--> Thu Aug 19 00:58:28 2010
---Program	./quickXML.pl	  ends--> Thu Aug 19 00:58:28 2010
mintqnr@mintqnr-desktop ~/Pictures/wallpaper $ ls
AllPictures.xml  Qunero-xml      sony      Windows7      另类      抽象视觉效果      晶莹剔透效果      炫彩花卉      绿色主题      花卉      花样年华
quickXML.pl      Qunero-xml.xml  sony.xml  Windows7.xml  另类.xml  抽象视觉效果.xml  晶莹剔透效果.xml  炫彩花卉.xml  绿色主题.xml  花卉.xml  花样年华.xml
Screenshot-wallpaper - File Browser.png
下图正在渐变中。。。
Screenshot-1.png
附件
getBackgroundXML.tar.gz
(1.97 KiB) 已下载 265 次
上次由 Qunero 在 2010-08-19 1:06,总共编辑 2 次。
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#2

帖子 Qunero » 2010-08-18 17:50

脚本使用简单说明:
1. 运行时最好使用完整路径,指明 图片所在的目录, 例如:

代码: 全选

getBackgroundXML.pl -d /home/mintqnr/Pictures/wallpaper/Windows7/ -o Windows7/background.xml
2. 默认 图片文件路径为当前目录 ,文件类型为 jpg, 默认输出结果到终端,保存需加上 -o filename;

3. 设置好后的应用方法:右键桌面->更改桌面背景->添加,在弹出对话框的右下方那里选择“全部文件”(默认是“图像”),然后找到你定义好的动态桌面壁纸的xml文件,双击添加就可以了。

4. 感兴趣的实验 :
1)加入可选参数 -t 指定切换时间,默认半小时左右。
2)优化代码,让其可移植性更好~~

5. 附件是源代码,以及几张漂亮的window7图片 ,background.xml 需要根据你的路径修改后在使用~~,祝大家玩得开心、用得顺手!
附件
Windows7_src.tar.xz
漂亮的W7 背景图,动态切换背景图的xml文件生成脚本
(3.43 MiB) 已下载 177 次
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#3

帖子 eexpress » 2010-08-18 20:22

切换有动画效果?
● 鸣学
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#5

帖子 Qunero » 2010-08-19 0:37

ubuntu1023 写了:源里的drapes不是就有这个功能么?
lz的脚本哪儿先进呢 :em02
:em06 :em06 刚刚去装了那个软件,的确可以切换桌面,不过时间控制不方便,图片添进去后要一张张删除~~我写的脚本主要利用系统自己去完成咯。生成相应的“图集控制文件” ,添加和删除批量进行 方便些~~
:em02 :em02 drapes的那种 快速切换到下一张倒是挺方便的~~
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#6

帖子 Qunero » 2010-08-19 0:40

eexpress 写了:切换有动画效果?
:em01 切换过程设置的5s ,所以有个动态切换的 渐变过程~~
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
Echol
帖子: 16
注册时间: 2010-07-24 16:02

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#7

帖子 Echol » 2010-09-10 15:46

Pain is short and joy is eternal.
BLOG: http://hi.baidu.com/mcmlxxxviii
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#8

帖子 Qunero » 2010-09-10 22:59

Echol 写了:来个java Dom4j实现的 :em04
http://hi.baidu.com/mcmlxxxviii/blog/it ... 8bff1.html
哈哈~ 厉害! 不过我才开始学习Java ,还看不太懂。 :em11 赞赞~~
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#9

帖子 eexpress » 2010-09-10 23:03

timetolast 没看出你怎么实现的过渡。。
● 鸣学
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#10

帖子 Qunero » 2010-09-10 23:19

eexpress 写了:timetolast 没看出你怎么实现的过渡。。
[-t TimeToLast=1795.0] [-i Interval=5.0] 那个TimeToLast是来显示该图片的时间,interval用来设置 图片切换过程的快慢,两个之和 就是整个 显示时间吧,在interval时间内,图片从一张 慢慢变道另一张,时间长一点 变得就慢,时间太短相当于瞬间就切换了;切换过程中两张图片渐变过程使用 系统的切换过程 自动实现的。 这个程序主要用来生成自己想要的 配置文件而已。 只是配置哦~ 至于 过度过程的话 就比较复杂了~
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#11

帖子 eexpress » 2010-09-11 0:12

系统的切换过程? 我就是不明白这个。
● 鸣学
头像
Jarson
帖子: 2371
注册时间: 2008-07-21 9:44
来自: 深圳
联系:

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#12

帖子 Jarson » 2010-09-12 22:21

强悍,这个顶一下
daidengke
帖子: 12
注册时间: 2009-09-21 19:11

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#13

帖子 daidengke » 2010-09-15 9:31

这个有bash shell 版本的吗?
Perl 不懂呀!
俺也用Java 写了个简单的,不过这个功能 用shell 是最合适的,如果是bash shell 就更爽了!
谁有呀!俺最近在学习bash shell :em03
头像
Qunero
帖子: 26
注册时间: 2009-11-21 22:49

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#14

帖子 Qunero » 2010-09-15 17:26

daidengke 写了:这个有bash shell 版本的吗?
Perl 不懂呀!
俺也用Java 写了个简单的,不过这个功能 用shell 是最合适的,如果是bash shell 就更爽了!
谁有呀!俺最近在学习bash shell :em03
bash用的不是太熟悉,只会写很简单的哦, 试一试,这个周末搞定他。 :em11 ,你也可以写一下咯~
PS : ubuntu 10.10 又加入了一组新的图集 叫:Contest的
附件
ubuntu 10.10 背景图,和我自己的图集
ubuntu 10.10 背景图,和我自己的图集
我思故我在,身在尘嚣中,思飞九霄外。
~~~~~~~~~~~~~~~~~~~~

代码: 全选

Life="Precious"
while [ "$Life" = "Precious" ]
do
    echo "Happy New Day"
    echo "Coding better Life"
    echo "Sleep for 7 Hours"; sleep 25200
done
头像
remeber
论坛版主
帖子: 13030
注册时间: 2009-01-06 15:18
来自: 人间

Re: ubuntu 下实现动态桌面壁纸 生成xml的实用脚本

#15

帖子 remeber » 2010-09-15 17:35

mark! :em05
“有一种被人抛诸脑后的过去,包含一些不该遗忘的点点滴滴。欢笑之余,我将故旧拾起,才刹见自己的薄情。人世间似乎存在着某种公平,每个人都曾被抛掷在他人的遗忘中;而今你所有哽咽难宣的话语,也正是你曾为人所期盼,却永远听不见的字句。” —赫曼 赫塞
回复