Want more features on Pastebin? Sign Up, it's FREE!
Guest

wallpaper-add-to-choices

By: ckruslicky on Aug 4th, 2012  |  syntax: Perl  |  size: 2.25 KB  |  views: 306  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. # can likely change from ubuntu- to custom- as Precise seems to use all in dir
  7. # (would need to create an XML shell using ubuntu- or precise- as example)
  8. my $XML_FILE = q(/usr/share/gnome-background-properties/ubuntu-wallpapers.xml);
  9.  
  10. usage() unless @ARGV && -f $ARGV[0];
  11. add_file(@ARGV);
  12. exit;
  13.  
  14.  
  15. # replace @ARGV to automagically edit file(s) inline
  16. sub add_file {
  17.    my ($image_fn, $optional_name) = @_;
  18.    @ARGV = ($XML_FILE);
  19.    # set to .bak or ~ if you want a backup, but both files will then be read,
  20.    # nearly doubling the choices when changing the background setting
  21.    local $^I = '';
  22.    while(<>) {
  23.       print;
  24.       next unless is_edit_boundary($_);
  25.       print filename_to_xml($image_fn, $optional_name);
  26. #       print STDERR filename_to_xml($image_fn, $optional_name);
  27.    }
  28. }
  29.  
  30.  
  31. # add new entry up front
  32. sub is_edit_boundary {
  33.    local $_ = shift;
  34. #    print STDERR $_;
  35.    return unless /^\s*<wallpapers>\s*$/;
  36.    return 1;
  37. }
  38.  
  39.  
  40. # "scale" worked beautifully before Precise, but now seemingly stuck
  41. # with "zoom" which is a shame, especially when moving files across
  42. # different screen sizes
  43. sub filename_to_xml {
  44.    my ($filename, $name) = @_;
  45.    $name = $filename unless $name;
  46.  
  47.    my $str = <<EOT;
  48.   <wallpaper>
  49.     <name>__NAME__</name>
  50.     <filename>__FILENAME__</filename>
  51.     <options>zoom</options>
  52.   </wallpaper>
  53. EOT
  54.    $str =~ s/__FILENAME__/$filename/;
  55.    $str =~ s/__NAME__/$name/;
  56.    return $str;
  57. }
  58.  
  59.  
  60. sub usage {
  61.    die join("\n", (
  62.       'USAGE',
  63.       '[sudo] '.$0.' filename [name]',
  64.       '',
  65.       'Adds filename to list of choices when changing desktop picture.',
  66.       'Because filename is a file that should be available to all, it makes',
  67.       'sense to have it in the default location: /usr/share/backgrounds/',
  68.       '',
  69.       'Optionally add a name that is shown in the desktop choice, default',
  70.       'is to use the filename itself as the label.',
  71.       '',
  72.       'EXAMPLE',
  73.       'sudo mv duk.jpg /usr/share/backgrounds/',
  74.       "sudo wallpaper-add-to-choices /usr/share/backgrounds/duk.jpg 'Duck with Fly'",
  75.       '',
  76.       'NOTE',
  77.       'By default the ubuntu config file being edited is owned by root.',
  78.       '', @_,
  79.    ))."\n";
  80. }
clone this paste RAW Paste Data