1

I am having trouble writing a little program; when I run the following command from my program:

gsettings get org.gnome.desktop.background picture-uri

I get:

'file:///home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg'

But that needs to become:

/home/thamara/.config/variety/Favorites/wallpaper-2448037.jpg

Or the program will not work. Can someone please help me?

The program:

#!/bin/bash
## Blogry for GDM - Blurs your current wallpaper and puts it on your loginscreen

## Some Settings
effect='0x8' # Change this to anything you like http://www.imagemagick.org/Usage/blur/
save='~/Pictures/blur.jpg'

## Step one -  getting the current wallpaper
dir=$(gsettings get org.gnome.desktop.background picture-uri)

## Step two - Blurring the wallpaper
convert $dir -blur $effect -blur $effect -blur $effect $save

## Step three - exit (Since GDM automatically loads the new wallpaper there is no need for seting it.)
exit 0

## Links:
# http://www.imagemagick.org/Usage/blur/
  • Are you really getting ///path/to/file? I see 'file:///path/to/file' — an URI with single quotes around it. ///path/to/file would work as a file name (the extra slashes at the beginning don't matter). – Gilles Nov 16 '13 at 22:34
2

The output from the gsettings command complies with the GVariant text syntax. This parameter is a string, printed out in single quotes.

You need to remove the quotes.

dir_uri=$(gsettings get org.gnome.desktop.background picture-uri |
          sed "s/^'//; s/'\$//; s/\\\\\\(.\\)/\\1/")

You then get a URI. If the file name doesn't contain any special characters, it's enough to remove file:// at the beginning (or even file:).

dir=$(gsettings get org.gnome.desktop.background picture-uri |
      sed "s~^'file://~~; s~'\$~~")
  • Sir, You are a genious! Thank you for everything and here is a link to the completed program.. :3 [pastebin.com/0fU5tUXt] – glaasje Nov 19 '13 at 14:45
2

When I run this command on Ubuntu 12.10 I get the following:

$ dir=$(gsettings get org.gnome.desktop.background picture-uri)
$ echo $dir
'file:///usr/share/backgrounds/warty-final-ubuntu.png'

I would just cleanup the value getting stored in $dir like so:

$ dir="${dir/file:\/\//}"
$ echo $dir
'/usr/share/backgrounds/warty-final-ubuntu.png'

This will truncate the file:// from the beginning of the string. You can change this if you're getting something different. In your case:

$ dir="${dir/\/\//}"

Details

The above is using a pattern substitution, ${var/pattern/} which will remove pattern from the variable $var.

Alternatives

@jthill also had a good suggestion of using Bash's "remove matching pattern prefix` notation instead. It's a little trickier to understand, IMO, but works equally as well.

Example

$ dir="\'${dir#\'file://}"

The above is removing the prefix, \'file:// from $dir. It's replacing it with a tick, ', followed by the remainder of $dir without the 'file://.

Bash man page

If you want to read up more on these features of Bash, I'd encourage you to do so. These are features that we're using above.

excerpts from Bash man page

${parameter#word}
${parameter##word}
       Remove matching prefix pattern.  The word is expanded to produce a 
       pattern just as in pathname expansion.  If the pattern matches the 
       beginning of  the  value  of  parameter, then  the  result  of  the  
       expansion is the expanded value of parameter with the shortest 
       matching pattern (the ``#'' case) or the longest matching pattern 
       (the ``##'' case) deleted.  If parameter is @ or *, the pattern 
       removal operation is applied to each positional parameter in turn, 
       and the expansion is the resultant list.  If parameter is  an array 
       variable subscripted with @ or *, the pattern removal operation is 
       applied to each member of the array in turn, and the expansion is the 
       resultant list.

${parameter/pattern/string}
       Pattern substitution.  The pattern is expanded to produce a pattern 
       just as in pathname expansion.  Parameter is expanded and the longest 
       match of pattern against  its  value is replaced with string.  If 
       pattern begins with /, all matches of pattern are replaced with 
       string.  Normally only the first match is replaced.  If pattern 
       begins with #, it must match at the beginning of the expanded value 
       of parameter.  If pattern begins with %, it must match at the end of 
       the expanded value of parameter.  If  string  is  null, matches  of  
       pattern  are  deleted  and the / following pattern may be omitted.  
       If parameter is @ or *, the substitution operation is applied to each 
       positional parameter in turn, and the expansion is the resultant 
       list.  If parameter is an array variable subscripted with @ or *, the 
       substitution operation is applied to each member of  the  array in 
       turn, and the expansion is the resultant list.

Follow-up Question #1

The OP asked the following in comments below.

now i am having the following problem.. unable to open image '/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg''

The issue, if you'll notice, is that there are 2 tick marks at the end of the string. I have no idea why those are there, but if you'd like to get rid of the trailing tick marks you can use this sed command right after the previous substitution I gave you. I couldn't figure out a way to deal with the 2 single tick marks at the end just using Bash's substitution features.

dir=$(echo "$dir" | sed "s/''/'/")

Example

$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg''

$ dir=$(echo "$dir" | sed "s/''/'/")
$ echo "$dir"
'/home/thamara/.config/variety/Downloaded/wallbase_type_all_order_random_nsfw_1‌​00_board_1/wallpaper-2249773.jpg'
  • dir=\'${dir#\'file://} ports everywhere, I think the pattern substitution might not work in #!/bin/sh scripts. – jthill Nov 16 '13 at 3:29
  • @jthill - this was intended to be a Bash only solution given the tag on the Q is Bash. However I just tried both your solution and mine and they both worked in /bin/sh too. – slm Nov 16 '13 at 3:49
  • Yeah, I should have led off with "minor point" or even "very minor point". I didn't know bash won't shut that off even with POSIXLY_CORRECT set. dash rejects it. – jthill Nov 16 '13 at 4:11
  • @jthill - still a good point to highlight. These are some of the lesser known features of Bash, so it's always good to bring attention to them. – slm Nov 16 '13 at 4:15
  • 1
    I think that it is because of these things ---> ` <--- – glaasje Nov 16 '13 at 18:21
0

If all you want to do is eliminate the extra two // at the start of the file name, and the '' that is oddly showing up at the end of the file name you can use sed:

dir=$(gsettings get org.gnome.desktop.background picture-uri|sed -e 's/\/\/\//\//' -e "s/'//g; s/^/'/; s/$/'/")

This will replace /// with / and delete any erroneously formatted instances or duplicates of '. Finally, it will add a ' at the beginning and end of the line as to properly encapsulate the file name if it has any spaces. This solution is a bit long but it should properly do the job.

I will also echo what slm mentioned and tell you that when I ran:

gsettings get org.gnome.desktop.background picture-uri

I got a result in the format of:

'file:///usr/share/backgrounds/warty-final-ubuntu.png'
  • Sorry but this does not fix my problem.. "convert: unable to open image /home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'': No such file or directory @ error/blob.c/OpenBlob/2643. convert: no decode delegate for this image format /home/thamara/.config/variety/Favorites/wallpaper-44803.jpg'' @ error/constitute.c/ReadImage/552. convert: no images defined `/home/thamara/Pictures/blur.jpg' @ error/convert.c/ConvertImageCommand/3145." – glaasje Nov 17 '13 at 15:31
  • @glaasje I have updated my answer to work based on the error you are receiving. – John B Nov 17 '13 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.