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

相信大家已经了解到了ubuntu自带的动态桌面背景了,有自己的图片集,也想让其中的图片动态作为自己的桌面背景怎么办???
背景知识参考:http://qyiyunso.blog.163.com/blog/static/3507768620107185273331/
引用:
不知大家在使用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  [input file]

   -help       print this help to screen
   -d              directory contains the jpgs
   -o          write result to a file

=head1 Example

   perl  getBackgroundXML.pl  -d pic_dir -o background.xml
   perl  getBackgroundXML.pl  --

=head1 Version

   Verion   :   1.0
   Created   :   08/18/2010 03:34:52 PM 
   Updated   :   08/18/2010 05:18:23 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 );
GetOptions(
   "help"      => \$Need_help,
   "d=s"      => \$PicDir,
   "o=s"      => \$Out_file,
);

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

#============================================================================#
#                              Global Variable                               #
#============================================================================#
my $Input_file  = $ARGV[0]  if (exists $ARGV[0]); 
$PicDir ||= '.';
$PicDir =~ s/\/$//;
$PicDir =~ s/ /\\ /g;

#============================================================================#
#                               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`;
#   print STDERR $PicDir , "\n";

   chomp( @picFiles );
#   print STDERR join( "\n", @picFiles );

   if( $PicDir =~ /^\// ){
      for ( @picFiles ){
         $_ = "$PicDir/$_";                  # get full path;
         $_ =~ s/ /\\ /g;
      }
   }
   else {
      my $curDir = `pwd`;
      chomp( $curDir );
      for( @picFiles ){
         $_ = "$curDir/$PicDir/$_";
         $_ =~ s/ /\\ /g;
      }
   }
#   print STDERR join( "\n", @picFiles );

   my $oldjpg = $picFiles[-1];
   print STDOUT 
"
   
      2010
      08
      18
      00
      00
      00
   
   \n";

   for ( @picFiles ){
      print STDOUT 
"   
      1795.0
      $oldjpg
   
   
       5.0
       $oldjpg
      $_
   \n";
   $oldjpg = $_;
   }

   print "\n";

}
脚本使用简单说明:
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 需要根据你的路径修改后在使用~~,祝大家玩得开心、用得顺手!
附件和源码下载,请见ubuntu 论坛: http://forum.ubuntu.org.cn/viewtopic.php?f=21&t=289599

你可能感兴趣的:(ubuntu / Mint下 perl 实现动态桌面壁纸 生成xml的实用脚本)