-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-wallpaper-xml
executable file
·164 lines (140 loc) · 4.79 KB
/
generate-wallpaper-xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
#=======================================================================================================================
# Description
# Script to generate an XML file for wallpaper rotation in Gnome 3/Unity. See
# https://yktoo.com/en/blog/post/2012/11/17-ubuntu/gnome-3-xml-wallpaper-creation/ for details.
# Synopsis
# generate-wallpaper-xml [options] <pictures_dir> <xml_file>
# Author
# Dmitry Kann, https://yktoo.com/
# License
# Public domain
#=======================================================================================================================
. "$(dirname "$(realpath "$0")")/common.sh"
# Setup vars
def_duration_static=1795
def_duration_transition=5
USAGE_INFO="XML wallpaper list generation script. Written by Dmitry Kann, https://yktoo.com/
Usage: $0 [options] pictures_dir xml_file
Options:
-d <number> Override default display duration ($def_duration_static seconds)
-s <mode> Sort the files according to <mode>, whose values are:
name Sort by file path and name
shuffle Randomize file order
-t <number> Override default transition duration ($def_duration_transition
seconds). If 0, no transition is used
-v Verbose output
-w Set the generated XML file as current wallpaper
pictures_dir Path to directory with pictures
xml_file Path to the output XML file"
#-----------------------------------------------------------------------------------------------------------------------
# Parse the command line
typeset -i duration_static=$def_duration_static
typeset -i duration_transition=$def_duration_transition
b_set_wallpaper=0
b_verbose=0
sort_command='cat'
args=$(getopt -o d:s:t:vw -- "$@")
[ $? -eq 0 ] || usage
eval set -- $args
for i; do
case "$i" in
-d)
duration_static=$2
[[ "$duration_static" -le 0 ]] && usage "Display duration should be greater than zero"
;;
-s)
case $2 in
name)
sort_command="sort"
;;
shuffle)
sort_command="shuf"
;;
*)
usage "Invalid sort mode: $2"
;;
esac
;;
-t)
[[ -z "$2" ]] && usage "Transition duration not specified"
duration_transition=$2
;;
-v)
b_verbose=1
;;
-w)
b_set_wallpaper=1
;;
--)
shift
break
;;
esac
shift
done
# Check the parameters
dir_wallpapers="$1"
file_output_xml="$2"
[[ -z "$dir_wallpapers" ]] && usage "No wallpaper directory specified"
[[ -d "$dir_wallpapers" ]] || err "Directory $dir_wallpapers does not exist"
[[ -z "$file_output_xml" ]] && usage "No output XML file specified"
# Verbose output
if [[ $b_verbose -ne 0 ]]; then
log "Display duration: $duration_static sec"
log "Transition duration: $duration_transition sec"
fi
# Canonicalize directory and output file name (make it absolute)
dir_wallpapers="$(realpath "$dir_wallpapers")"
file_output_xml="$(realpath "$file_output_xml")"
# Write XML file header
cat << EOF >"$file_output_xml"
<background>
<!-- Wallpaper definition file. Generated on $(date) by $0 -->
<starttime>
<year>2000</year>
<month>01</month>
<day>01</day>
<hour>00</hour>
<minute>00</minute>
<second>00</second>
</starttime>
EOF
# Iterate through image files
unset last_img_file
i_count=0
while read img_file; do
# Insert transition statement if it's not the first image
if [[ -n "$last_img_file" && "$duration_transition" -gt 0 ]]; then
cat << EOF >>"$file_output_xml"
<transition>
<duration>$duration_transition</duration>
<from>$last_img_file</from>
<to>$img_file</to>
</transition>
EOF
fi
# Insert 'static' image statement
cat << EOF >>"$file_output_xml"
<static>
<duration>$duration_static</duration>
<file>$img_file</file>
</static>
EOF
last_img_file="$img_file"
((i_count++))
# Trick below to avoid piping that would limit the scope of $i_count to the subshell
done << EOF
$(find "$dir_wallpapers" -type f \( -iname '*.jpeg' -o -iname '*.jpg' -o -iname '*.png' \) -print | $sort_command)
EOF
# Verbose output
[[ $b_verbose -ne 0 ]] && log "File $file_output_xml generated; $i_count wallpaper files found"
# Write XML file footer
echo '</background>' >>"$file_output_xml"
# Set wallpaper file as current, if needed
if [[ $b_set_wallpaper -ne 0 ]]; then
gsettings set org.gnome.desktop.background picture-uri "file://$file_output_xml" &&
gsettings set org.gnome.desktop.background picture-options "zoom" &&
# Verbose output
( [[ $b_verbose -ne 0 ]] && log "Wallpaper URI set to file://$file_output_xml" )
fi