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