登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Q超越兔子的蜗牛O--逸云沙鸥Linux

飘飘何所似,天地一沙鸥;落霞与孤鹜齐飞,秋水共长天一色~~

 
 
 

日志

 
 

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

2010-08-18 18:12:45|  分类: Ubuntu笔记 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
相信大家已经了解到了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  <options> [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   :   Quner612@qq.com,QuNengrong@genomics.cn
   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 
"<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 ){
      print STDOUT 
"   <static>
      <duration>1795.0</duration>
      <file>$oldjpg</file>
   </static>
   <transition>
       <duration>5.0</duration>
       <from>$oldjpg</from>
      <to>$_</to>
   </transition>\n";
   $oldjpg = $_;
   }

   print "</background>\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
  评论这张
 
阅读(836)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018