Skip to content

Instantly share code, notes, and snippets.

@GuiltyDolphin
Created June 20, 2016 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuiltyDolphin/ef87dd1f3d62b8c8ba623950fe3f6e6d to your computer and use it in GitHub Desktop.
Save GuiltyDolphin/ef87dd1f3d62b8c8ba623950fe3f6e6d to your computer and use it in GitHub Desktop.
Simple script for updating Cinnamon wallpapers
#!/usr/bin/env perl
# Change Cinnamon desktop background.
# USAGE: change-wallpaper.pl $HOME/Pictures/Wallpapers/...
# change-wallpaper - update Cinnamon wallpapers
# Copyright (c) 2016 Ben Moon
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
my $USAGE = "USAGE: change-wallpaper.pl file";
sub exit_usage {
print "$USAGE\n" and exit 1;
}
my @cs = @ARGV;
exit_usage if @cs == 0;
my $walldir = "$ENV{HOME}/.wallpapers";
mkdir $walldir unless -d $walldir;
# Initialize wallpaper info (track current and next wallpaper)
my $finfo_path = "$walldir/.wall-info";
unless (-f $finfo_path) {
open my $fh, '>', $finfo_path;
print $fh "";
}
sub update_wall_info {
my $current = shift;
open my $fh, '>', $finfo_path;
print $fh "$current\n";
}
sub update_wallpaper {
my $path = shift;
print STDERR "no such file $path\n" and exit 1 unless -f $path;
`gsettings set org.cinnamon.desktop.background picture-uri "file://$path"`
}
sub next_wallpaper {
my $paper_info = do { open my $fh, '<', $finfo_path; <$fh>; };
chomp (my $current = $paper_info);
my @papers = glob "$walldir/*";
my @sorted = sort @papers;
if ($current eq '' || $current eq $sorted[$#sorted]) {
$current = $sorted[0];
} else {
while (my $path = shift @sorted) {
if ($path eq $current) {
$current = shift @sorted;
last;
}
}
}
update_wallpaper($current);
update_wall_info($current);
}
while (my $command = shift @cs) {
if ($command =~ /^-(n|-next)$/i) {
next_wallpaper();
exit;
}
# Load all specified files (glob) into the .wallpapers directory
if ($command =~ /^(-lw|--load-wallpapers)$/i) {
my @files = @cs;
# my $load_path = shift @cs or exit_usage;
# my @files = glob $load_path;
foreach my $file (@files) {
my $base = `basename $file`;
`cp $file $walldir/$base`;
}
exit;
}
exit_usage if @cs > 1;
update_wallpaper($command);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment