2011年2月21日 星期一

GNOME動態桌布 (Wallpaper Slideshow)

上上禮拜重灌了我的Macbook Pro,也順便重灌了ubuntu 10.10。來澳洲很久沒有動腦筋寫程式了,我想寫寫簡單的shell script或許能提神醒腦一下;剛好email收到台南二寮的一些美麗照片,gnome從2.28以後(好像??)就可以動態變更桌布啦,只是他會去讀取某一個.xml檔。所以理論上就是準備好圖片,準備好讓gnome讀取的xml就搞定了。

雖然網路上已經有許多同好也寫了一些script或是程式,不過總覺得沒有自己寫來來得爽快。我的這隻程式功能非常簡單,就是只會在當前工作目錄找所有的jpg以及png檔,然後在當前目錄下建立一個xml,這個xml可以放置在任何地方,唯有圖片本身路徑不能任意移動(這好像是廢話@_@)。

  1. #!/bin/bash
  2. #############################################################
  3. # Filename: makeGnomeBackground.sh
  4. # Date: 2011/02/20
  5. # Purpose: changing background dynamically
  6. # Usage:
  7. #
  8. # Description:
  9. # Author: maxsolar(Jim T. Tang@IES, Academia Sinica)
  10. #############################################################
  11.  
  12. ###########################
  13. ## pre-defined variables
  14. ###########################
  15. # check if there are jpg or png files in current working dir.
  16. find `pwd -P` -maxdepth 1 -name "*jpg" -o -name "*JPG" > filelist.txt 2>/dev/null
  17. find `pwd -P` -maxdepth 1 -name "*png" -o -name "*PNG" >> filelist.txt 2>/dev/null
  18. [ $(cat filelist.txt | wc -l) -eq 0 ]  && {
  19.     echo "****************************************"
  20.     echo "No .jpg nor .png files found. Try other dirs. "
  21.     echo "****************************************"
  22.     exit 1;
  23. } || {
  24.     cat filelist.txt
  25.     echo "-------------------------------"
  26.     echo "$(cat filelist.txt | wc -l) Pictures were found."
  27. }
  28. year=$(date +"%Y" )
  29. month=$(date +"%m")
  30. day=$(date +"%d")
  31. output=$(basename "`pwd`" /).xml
  32.  
  33. ##main method
  34. read -p "Duration time between pictures?  (default=5) " duration1
  35. [ -z $duration1 ] && {
  36.     duration1=5
  37. }
  38.  
  39. read -p "Duration time within a picture?  (default=600) " duration2
  40. [ -z $duration2 ] && {
  41.     duration2=600
  42. }
  43.  
  44. ## part 1
  45. cat <<HERE > $output
  46. <background>
  47.   <starttime>
  48.     <year>$year</year>
  49.     <month>$month</month>
  50.     <day>$day</day>
  51.     <hour>00</hour>
  52.     <minute>00</minute>
  53.     <second>00</second>
  54.   </starttime>
  55. <!-- This animation will start at midnight. -->
  56. HERE
  57. ## part 2
  58. declare -a picArray=(`cat filelist.txt`)
  59. declare -i loops=$(cat filelist.txt | wc -l)
  60. for (( i=1; i<="$loops"; i++ )) ; do
  61.     ##The Last Loop
  62.     if [ $i -eq $loops ]; then 
  63.         cat <<HERE >>$output
  64.   <static>
  65.     <duration>$duration2</duration>
  66.     <file>${picArray[$i-1]}</file>
  67.   </static>
  68.   <transition>
  69.     <duration>$duration1</duration>
  70.     <from>${picArray[$i-1]}</from>
  71.     <to>${picArray[0]}</to>
  72.   </transition>
  73. HERE
  74.     else
  75.     ## in formal loops
  76.         cat << HERE >>$output
  77.  <static>
  78.     <duration>$duration2</duration>
  79.     <file>${picArray[$i-1]}</file>
  80.   </static>
  81.   <transition>
  82.     <duration>$duration1</duration>
  83.     <from>${picArray[$i-1]}</from>
  84.     <to>${picArray[$i]}</to>
  85.   </transition>
  86. HERE
  87.     fi
  88. done
  89. echo "</background>" >> $output
  90.  
  91. echo \"$output\" is made.
  92.  
  93. ## remove temparary file
  94. rm filelist.txt

這個script會讀取當前目錄下的所有jpg/png檔(不分大小寫),然後提問:
1.圖片間切換時間,直接按下enter預設5秒;
2.每張圖片的時間,直接按下enter預設600秒。輸出的xml檔名預設就是該目錄的名稱,建立成功會有提示出現。

在桌面空白處按右鍵->Change Desktop Background->Add

切換到想要gnome讀取的那個資料夾之後,選擇All Files,就可以看到該xml檔。

選取剛剛做好的xml檔之後,就可以在桌布預覽的地方看到我們剛剛的作品。

網路上也有幾篇好玩的可以看看:
Create Your Own GNOME Background Slideshow
[分享] Gnome動態桌布
自動換桌布的shell script