最近一直还Ubuntu的桌面背景动态变化的只有那么2个目录而烦恼,网上找了下资源,发现一xml脚本生成工具。这是因为要动态改变需要一个xml脚本语言,本perl脚本用于生成xml脚本文件。具体步骤:
1.将新的背景图片和本.pl文件放到名为background(自己定义)的文件夹中;2.将本目录cp到/usr/share/backgrounds目录中,需要sudo;3.cd background; 4.sudo perl getBackgroundXML.pl -t 1500 -d . -o change.xml,生成.xml文件;5.右击桌面->更改桌面背景->添加->找到该目录->显示所有文件->选择change.xml 6. OK
getBackgroundXML.pl 源码如下:
#!/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=STDOUT]
#
# -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 : Quner612@qq.com,QuNengrong@genomics.cn
# Company : BGI
#
# =cut
# #============================================================================#
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 );
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";
}