XKCD comics as desktop wallpaper [Moved from General]

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

XKCD comics as desktop wallpaper [Moved from General]

Postby Ace_NoOne » Fri Mar 09, 2007 8:59 am UTC

Hi there,

Is there any way to set a random XKCD comic as my (Windows) desktop wallpaper (preferably with alt text)?
Regular wallpapers just become boring too soon...


PS: I hope retrieving an image directly from the website would not be considered evil hotlinking!?

User avatar
davean
Site Ninja
Posts: 2498
Joined: Sat Apr 08, 2006 7:50 am UTC
Contact:

Re: XKCD comics as desktop wallpaper

Postby davean » Sat Mar 10, 2007 12:11 am UTC

Ace_NoOne wrote:Hi there,

Is there any way to set a random XKCD comic as my (Windows) desktop wallpaper (preferably with alt text)?
Regular wallpapers just become boring too soon...


PS: I hope retrieving an image directly from the website would not be considered evil hotlinking!?


Theres a reason we list the image link directly below each comic.

And I'd be happy to help you set that up on most other OSes, but its been FAR too long since I last saw windows to be able to try even.

User avatar
Ren
Rockin' Robin
Posts: 2933
Joined: Tue Mar 06, 2007 3:06 pm UTC
Location: Kitchener, Ontario
Contact:

Postby Ren » Sat Mar 10, 2007 12:13 am UTC

Funny story: My desktop background is currently the Automatic Doors comic.

Um, I don't know how you could get alt-text with it, but I just save the pictures then set them as desktop background.
MotleyJesster (12:34:04 PM): Better than moping around being all "I do not need love, I have indie music and a wind instrument!"

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Re: XKCD comics as desktop wallpaper

Postby Ace_NoOne » Sat Mar 10, 2007 7:44 am UTC

davean wrote:Theres a reason we list the image link directly below each comic.
So it's allowed then?! I wasn't sure at first... :)

davean wrote:And I'd be happy to help you set that up on most other OSes, but its been FAR too long since I last saw windows to be able to try even.
One more reason for me to finally make the switch to Ubuntu!

Anyways, if anyone happens to know TiddlyWiki (sort of a personal notebook, all in a single, self-containing HTML file), I recently wrote a plugin that does something like this, sort of. It retrieves a random image URL from XKCD and sets it as the site's background image.
It's sort of a dirty hack (I'm not an experienced coder), but it might still be relevant here:

Code: Select all

/***
RemoteBackgroundPlugin v1.0 (2007-02-19)
 by FND

Retrieves a random image from XKCD and uses it as background image.

''N.B.:'' Do not abuse this; retrieving the images from XKCD could be regarded as hotlinking!
!!Changelog
!!!v0.9 (2007-02-18)
* initial release
!!!v1.0 (2007-02-19)
* added ability to position background image
* minor code adjustments
!!ToDo
* wait until TiddlyWiki loaded has completed before loading remote data
* extensive bug-testing (esp. non-Mozilla browsers)
!!Code
***/
/*{{{*/
setBgImg();

// set background image
function setBgImg() {
   if (img() != null) {
      document.body.style.backgroundImage = "url(" + img()[0] + ")";
      document.body.style.backgroundRepeat = "no-repeat";
      document.body.style.backgroundPosition = "center";
      document.body.style.backgroundAttachment = "fixed";
      document.body.title = img()[1];
   } else {
      alert("error fetching remote image");
   }
}

// fetch image URL
function img() {
   // get remote location
   var maxCount = 225; // current amount of comcics - 2007-02-19
   var counter = Math.ceil(Math.random() * maxCount);
   var remoteSite = "http://xkcd.com/c" + counter + ".html";
   // fetch remote image
   var XmlHttp = true;
   var source = null;
   try { // query for permission to access remote file
      netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
      XmlHttp = false;
   }
   // initiate XmlHttp
   if (typeof XMLHttpRequest != 'undefined') { // Mozilla, Opera, Safari and Internet Explorer 7
      XmlHttp = new XMLHttpRequest();
   }
   if (!XmlHttp) { // Internet Explorer 6 and older
      try {
         XmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try {
            XmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
         } catch(e) {
            XmlHttp  = null;
         }
      }
   }
   // get remote document
   if (XmlHttp != false) {
      XmlHttp = new XMLHttpRequest();
      if (XmlHttp) {
         // establish connection
         XmlHttp.open('GET', remoteSite, false);
         XmlHttp.send(null);
         // get image URL
         //source = XmlHttp.responseXML.getElementsByTagName('img')[1].src; // source data no valid XML => RexEx workaround
         var extract = XmlHttp.responseText.match(/<img src="(.+)" title="(.+)" alt/i);
         image = new Array(extract[1], extract[2]); // store URL and title value
      }
   }
   return image;
}
/*}}}*/

User avatar
shadebug
Posts: 425
Joined: Sat Jan 20, 2007 3:21 pm UTC
Location: Ceredigion/Essex, UK
Contact:

Postby shadebug » Sat Mar 10, 2007 9:57 am UTC

could you not use the rss feed or something to automatically upate the latest comic?
Heaven is for the hedonist

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sat Mar 10, 2007 10:00 am UTC

shadebug wrote:could you not use the rss feed or something to automatically upate the latest comic?
That should be possible, but I was going for a random comic, not just the latest.

However, the RSS feed could be used to ensure that the list of comics is always up to date. (My example above has the number of comics hardcoded, meaning it's hardly ever up to date... )

User avatar
shadebug
Posts: 425
Joined: Sat Jan 20, 2007 3:21 pm UTC
Location: Ceredigion/Essex, UK
Contact:

Postby shadebug » Sat Mar 10, 2007 10:17 am UTC

that's what I was talking about, a random comic should always include the latest in its choice, and always return the fourth
Heaven is for the hedonist

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sat Mar 10, 2007 10:20 am UTC

shadebug wrote:that's what I was talking about, a random comic should always include the latest in its choice, and always return the fourth
Oh sorry, I kinda missed that...

PS: Nice allusion to #221!

User avatar
shadebug
Posts: 425
Joined: Sat Jan 20, 2007 3:21 pm UTC
Location: Ceredigion/Essex, UK
Contact:

Postby shadebug » Sat Mar 10, 2007 10:27 am UTC

Ace_NoOne wrote:
shadebug wrote:that's what I was talking about, a random comic should always include the latest in its choice, and always return the fourth
Oh sorry, I kinda missed that...

PS: Nice allusion to #221!


Thank you.

Actually, i want to try out converting the comics to vector graphics, that way you could expand them properly
Heaven is for the hedonist

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sat Mar 10, 2007 10:30 am UTC

shadebug wrote:i want to try out converting the comics to vector graphics, that way you could expand them properly
That'd be great - though it sounds like a daunting task (not that I knew a lot about vector graphics)...

Keep me updated here if you manage to pull it off!

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Sun Mar 11, 2007 9:01 pm UTC

I was wondering about doing this a bit ago -- I use xkcd comics as backgrounds on my work laptop. As I see it there are two options. First, write a little app to download one and stick it on. Otherwise, make an HTML file that you could use as an Active Desktop. The latter would be my preferred way, but I can't think how to get a list of image URLs except perhaps by parsing the archive page.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sun Mar 11, 2007 9:09 pm UTC

Andrew wrote:write a little app to download one and stick it on
I've been thinking about doing that with AutoHotkey, but Windows is kinda bitchy about file formats (apparently, only BMPs can be used for the wallpaper, so you'd have to convert the images first).
Andrew wrote:Otherwise, make an HTML file that you could use as an Active Desktop. The latter would be my preferred way, but I can't think how to get a list of image URLs except perhaps by parsing the archive page.
I don't much like Active Desktop, but nevertheless:
You don't have to get a list of image URLs; you can just select a random URL (http://xkcd.com/[#].html) and extract the image URL from that. At least that's what I did with the TiddlyWiki plugin above.

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Sun Mar 11, 2007 10:25 pm UTC

Ace_NoOne wrote:
Andrew wrote:write a little app to download one and stick it on
I've been thinking about doing that with AutoHotkey, but Windows is kinda bitchy about file formats (apparently, only BMPs can be used for the wallpaper, so you'd have to convert the images first)


If you don't mind .NET you could C# that up pretty easily. It can do downloading things and image format conversion as easily as you like.

I made a screensaver version of The Crazy Dance once that did much the same thing. For that, though, I had the server pick an image for it, because I could do that and it kept it up to date. It cached the images too, that would be pretty easy to include.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Mon Mar 12, 2007 10:34 am UTC

Andrew wrote:If you don't mind .NET you could C# that up pretty easily.
I don't exactly mind .NET, but I have absolutely no experience with it...

Andrew wrote:For that, though, I had the server pick an image for it, because I could do that and it kept it up to date. It cached the images too, that would be pretty easy to include.
That's a good idea, actually; why is there no "random comic" function on xkcd.com?! ...

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Mon Mar 12, 2007 12:57 pm UTC

Ace_NoOne wrote:
Andrew wrote:If you don't mind .NET you could C# that up pretty easily.
I don't exactly mind .NET, but I have absolutely no experience with it...

Maybe I'll have a go sometime. Wednesday, maybe. Or this weekend. I don't imagine it'd take more than an hour. If I do I'll stick the source up as well in case anyone wants it.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Mon Mar 12, 2007 1:31 pm UTC

That's very generous of you, Andrew - I'm definitely looking forward to it (maybe it'll even get me interested in C#... )!

User avatar
jsd1982
Posts: 32
Joined: Mon Mar 12, 2007 8:53 pm UTC

Postby jsd1982 » Tue Mar 13, 2007 2:10 pm UTC

Hope you didn't miss this thread: http://forums.xkcd.com/viewtopic.php?p=58728#58728

I wrote a Windows Powershell (basically a .NET 2.0 scripting language) script to load the latest xkcd.com RSS feed entry, grab the img src URL and invert the colors and center it on a black background for your desktop wallpaper.

It works great for the black & white comics, let's hope Randall doesn't go *too* colorful!

If you don't want the powershell I can hack something else up in C# quite easily.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Tue Mar 13, 2007 5:22 pm UTC

jsd1982 wrote:Hope you didn't miss this thread: http://forums.xkcd.com/viewtopic.php?p=58728#58728
I did, actually - so I appreciate your notice!

jsd1982 wrote:I wrote a Windows Powershell (basically a .NET 2.0 scripting language) script to load the latest xkcd.com RSS feed entry, grab the img src URL and invert the colors and center it on a black background for your desktop wallpaper.
I'm not sure about the inverting part (altering the artist's work is heresy ;) ), but this sure sounds great.
And adding a randomizer should be fairly easy as well.

jsd1982 wrote:If you don't want the powershell I can hack something else up in C# quite easily.
From what I understand, Powershell is kinda like WSH, i.e. an interpreter for scripts. Since Powershell doesn't seem to be part of the standard installation, I'd prefer a stand-alone EXE - if that's not asking too much, that is...

Thanks!

mikoangelo
Posts: 12
Joined: Fri Mar 16, 2007 12:24 pm UTC

Bash ftw

Postby mikoangelo » Fri Mar 16, 2007 12:30 pm UTC

Come on, bash is the obvious choice here :D

Code: Select all

#!/bin/bash

# EDIT THIS TO YOUR LIKING. It's the path to the folder containing the cache of comics. Duh
CACHE=/tmp/xkcd/

function fetchurl() {
   # This function is just in case you haven't got curl.
   # It could be replaced by wget, /dev/tcp or something like that
   curl -s "$1" ||
      { echo "Network error. Fix it" >&2; exit 1; } # Error handling
}

function lastnum() {
   # Get the number of the latest comic
   fetchurl 'http://xkcd.com/' |
      awk '/<h3>Permalink to this comic: http:\/\/xkcd.com\/c/ { print int(substr($5, 18)) }'
}

# Get a random number between 1 and Number_Of_Comics
# Using the modulo operator has the possibility of
# yielding 0, so we subtract one from Number_Of_Comics,
# and add it again at the end
random=$(( ( RANDOM % ( `lastnum` - 1  ) ) + 1))

[[ "$1" == "--xkcd-true-behavior" ]] && random=4

# Fetch the random html document, find the img tag, get the url
# (it's the text between the quotes marks, hence the -F to awk)
image=$(fetchurl "http://xkcd.com/c$random.html" |
   awk -F\" '/src="http:\/\/imgs.xkcd.com\/comics/ { print $2 }' )


# We go into the cache folder
cd "$CACHE" ||
   { echo "Couldn't enter cache directory '$CACHE'. Fire up the mkdir-mobile!" >&2; exit 2; } # Error handling

# I use wget here because of the --continue flag. It makes it easy to cache.
# If the picture already has been downloaded, the wget command simply does
# nothing. Easy as 3.14...
wget --quiet --continue "$image" ||
   { echo "An error occured while downloading image" >&2; exit 1; } # Still error handling

# Now all that's left to do is to print the path to the comic
echo "${CACHE}/$(basename "$image")"


All that's left it to make a crontab entry with the command to change desktop background. This is, of course, window manager specific.

The script is here too: http://preview.thesecretpinkbox.net/files/randxkcd.sh
Somewhere at the edge of the bell curve is the partner for me (Comic #(floor(pi*100))).

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Fri Mar 16, 2007 1:45 pm UTC

Thanks, that'll certainly come in handy once I've finally made the switch to Linux!
Though for now, I'm stuck with Windows... :|

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Sat Mar 17, 2007 6:21 pm UTC

Ace_NoOne wrote:That's very generous of you, Andrew - I'm definitely looking forward to it (maybe it'll even get me interested in C#... )!

Not sure how long it took, but it's done. A couple of hours tops, I reckon.

Binary (8kB, requires .NET):
http://www.apathysketchpad.com/codefact ... hp?id=xkcd
Source code (77kB, requires Visual Studio Express 2005):
http://www.apathysketchpad.com/codefact ... kcd-source

I've even commented my code, just for you.

Let me know if it works for you.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sat Mar 17, 2007 7:32 pm UTC

That's very cool, Andrew - thanks a lot!
I really like the fact that you've even included a blacklist for disliked comics (though, of course, there are no bad xkcd comics ;) ).

One note (for posterity): It seems to retain the last-used display mode for the wallpaper (center, tile or stretch). So it's probably a good idea to manually set that to "center" before using this tool.
But given that you've commented the code, I just might give it a try and fix that myself - and while I'm at it, I'll try to figure out a way to add the alt text...

Quick update / warning: Rainlendar's "On Desktop" positioning doesn't work anymore after running xkcd Wallpaper Randomiser (xWR?) - I assume that's because xWR uses Active Desktop to display the non-BMP images, so it's most likely Rainlendar's fault.

Thanks again!


PS: Bonus points for "Known issues: Raptor attack"! :D (The program icon is nice too!)

EvanED
Posts: 4327
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Postby EvanED » Sat Mar 17, 2007 8:38 pm UTC

Ace_NoOne wrote:That's very cool, Andrew - thanks a lot!
I really like the fact that you've even included a blacklist for disliked comics (though, of course, there are no bad xkcd comics ;) ).

One note (for posterity): It seems to retain the last-used display mode for the wallpaper (center, tile or stretch). So it's probably a good idea to manually set that to "center" before using this tool.
But given that you've commented the code, I just might give it a try and fix that myself - and while I'm at it, I'll try to figure out a way to add the alt text...


That's cause there is no apparent API for changing it. You have to directly change registry keys. The ones you're looking for are HKCU/Control Panel/Desktop/Tile Wallpaper and Wallpaper Style. For "center" they should be set to 0 and 0 respectively; for tiled, 1 and 0; and for stretched, 0 and 2. (Edit: this last one used to say 2 and 0, but that's backwards.)

At least this is the case on my computer.

Here's some C# code for setting it:

Code: Select all

static void setCenter()
{
    setRegValues(0, 0);
}

static void setTile()
{
    setRegValues(0, 1);
}

static void setStretch()
{
    setRegValues(2, 0);
}

static void setRegValues(int style, int tile)
{
    RegistryKey hklu = Registry.CurrentUser;
    RegistryKey cp = hklu.OpenSubKey("Control Panel");
    RegistryKey desk = cp.OpenSubKey("Desktop", true);

    desk.SetValue("WallpaperStyle", style.ToString());
    desk.SetValue("TileWallpaper", tile.ToString());
}


Somewhat in response to this (to make it easier to port the Bash script) a friend and I put together a program that will set the wallpaper to a file you specify on the command line. There's still a couple finishing touches to put on it, but I'll post in under separate cover here soon.

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Sun Mar 18, 2007 3:34 am UTC

Ace_NoOne wrote:That's very cool, Andrew - thanks a lot!
I really like the fact that you've even included a blacklist for disliked comics (though, of course, there are no bad xkcd comics ;) ).

I've blacklisted one, because it's several times taller than my screen and looks a bit stupid as wallpaper.

Also, I've updated the programme slightly -- it wasn't checking the save folder, because I'd stupidly forgotten to. Now it does, so it's a bit faster and more bandwidth-efficient if it picks the same comic twice.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Sun Mar 18, 2007 5:18 pm UTC

Sounds good, Andrew; I'll update right away!

EvanED wrote:a friend and I put together a program that will set the wallpaper to a file you specify on the command line [...] I'll post in under separate cover here soon.
It'd be nice if you could make a quick note in here as well.
UPDATE: Never mind; I guess you mean this one...

EvanED
Posts: 4327
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Postby EvanED » Sun Mar 18, 2007 11:05 pm UTC

Ace_NoOne wrote:
EvanED wrote:a friend and I put together a program that will set the wallpaper to a file you specify on the command line [...] I'll post in under separate cover here soon.
It'd be nice if you could make a quick note in here as well.
UPDATE: Never mind; I guess you mean this one...


Yep, that's the one. Thought about posting it here but decided not to.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Mon Mar 19, 2007 7:29 am UTC

EvanED wrote:Yep, that's the one. Thought about posting it here but decided not to.
Well, I did - for posterity... :)

Updated to-do list (mostly as a reminder for myself):
  • automatically set display mode (center by default, stretch if image is larger than the screen resolution)
  • use cached images if there was a connection error
  • add comics' title and alt text
  • optional timer to change background image every x minutes

livejamie
Posts: 2
Joined: Fri Mar 23, 2007 6:46 pm UTC

Postby livejamie » Fri Mar 23, 2007 6:48 pm UTC

i just wanted to register and say thank you for this, i was looking for something just like this.

is there any way we could get a version to use on the web? like a php script?

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Fri Mar 23, 2007 7:11 pm UTC

livejamie: How exactly do you want to use this on the web - just retrieve a random image from the website? That should be rather easy:

I've taken a good look at Andrew's code, and it was quite helpful. However, while C# seems to be an interesting language, I have little experience with it.
Thus I've started developing something similar in the language I know best - AutoHotkey (a Windows scripting language). And it turned out to be quite easy - and it should be easy to do with PHP too:
  1. get the total comic count from xkcd.com (the index page always holds the latest comic - as well as a permalink, which contains the latest number and is easy to extract using RegEx)
  2. select random comic (any number between 1 and totalCount)
  3. extract image URL from that comic's page (http://www.xkcd.com/c[randomIndex].html), using RegEx as above
I haven't done anything with PHP in ages, but the only potential problem here would be the RegEx terms - which I could provide you with.


Speaking of my script though - I'm having a little trouble with image manipulation - I want to
  • convert a PNG image to BMP
  • embed a text caption in the image (e.g. adding the source URL to the bottom right of the image).
AutoHotkey supports DllCalls, so I'd probably be best off with some DLL.
During my research, I've come across ImageMagick, LibPNG and NetPBM - unfortunately, I can't really figure out how to use those...

So if anyone happens to know a thing or two about image manipulation libraries, I'd greatly welcome some help!

livejamie
Posts: 2
Joined: Fri Mar 23, 2007 6:46 pm UTC

Postby livejamie » Fri Mar 23, 2007 8:36 pm UTC

yes i'd like to be able to

Code: Select all

<img src="whatever.php/picture.jpg">
and have it be a random picture in profiles i have :)

demon
Posts: 170
Joined: Tue Feb 06, 2007 8:13 pm UTC

Postby demon » Sat Mar 24, 2007 8:52 am UTC

really sorry for the ot, but

mikoangelo:
i really like the style of that bash script:) i'm pretty good at managing a linux system but rarely have to actually shellscript anything, so getting to look at things like condensing an if then fi statement to [[ ]] && is really beneficient to me. are these homebrewed ideas or did you get them from some doc?

mikoangelo
Posts: 12
Joined: Fri Mar 16, 2007 12:24 pm UTC

Postby mikoangelo » Wed Mar 28, 2007 8:56 am UTC

demon wrote:things like condensing an if then fi statement to [[ ]] && is really beneficient to me. are these homebrewed ideas or did you get them from some doc?


man bash IYF

I have no life, hence I have the time to grok man pages ^^

You should be aware of some pitfalls in the [[ ]] && || constructs. If you want, for example, and error message if $error is non-empty, and a success message if it is, you are best off with a little curly brace-fu:

Code: Select all

[[ -n "$error" ]] && { echo "Error: $error" >&2; exit 1[b];[/b] } || echo "Success";


This will exit with code 1 if $error is non-empty, outputting its contents to stderr (hence the >&2 at the echo statement). Note the semicolon before the }. It is crucial, and must never be forgotten, lest you be sliced to death by the horrific syntax grue.

If $error is empty; the [[ ]] && { } construct (not just the [[ ]] construct) will return false, triggering the statement after the ||. If you wanted it to do something else there, just repeat the { }-fu.

Note, however, that if you want your script to exit right after this with an appropriate error code, this is better:

Code: Select all

[[ -z "$error" ]] || ( echo "Error: $error" >&2; exit 1)


This will test if $error is empty. It will return true if no error is there, and hence the script will exit with code 0 if no further lines are given (any script exits with the code of the last issued command (echo always returns true)).

If an error did occur, the || ( ) statement will fire. It will output the error to stderr. However, since echo returns true, it would cause the script to exit with 0, and ragnarok would ensue. Instead, using the () baraces instead of {} will cause it to use a subshell, so it would just return the exit code instead of stopping the entire script if `exit` is given. Using `exit 1` in the end causes the subshell to exit with 1, and hence the script afterwards (the reason for not just using {} in the first place is if you put this in e.g. a function, it'd be more readable and obvious - to me, at least.

There is no need for a semicolon before the ). Syntax grues ftw.

Hope this helped ^^
Somewhere at the edge of the bell curve is the partner for me (Comic #(floor(pi*100))).

demon
Posts: 170
Joined: Tue Feb 06, 2007 8:13 pm UTC

Postby demon » Wed Mar 28, 2007 12:11 pm UTC

yeah, thanks, that might come in handy.
and man bash is actually where i looked first when i started to learn bash. its not like i completely ignored it or whatever, i just misunderstood the part about [[ ]] - it somehow didn't occur to me that it can be used as freely as (( )). but now i do get it and will use it, thanks:]

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Mon Jul 23, 2007 8:21 am UTC

Andrew, your xkcd Wallpaper Randomiser has become non-functional.
I assume it has something to do with changes on the site*.

Can anyone confirm this?


* Three symptoms that led me to believe there has been some work done:
1. The buttons for navigating the comics look different.
2. The ATOM feed suddenly showed the three latest comics as new.
3. http://fora.xkcd.com no longer works (redirects to the front page).

User avatar
Andrew
Posts: 619
Joined: Tue Jan 02, 2007 9:59 pm UTC
Location: Manchester, UK
Contact:

Postby Andrew » Mon Jul 23, 2007 9:01 am UTC

Thanks for letting me know. I'm doing other stuff at the moment, so I'll look at this when I get to it. If anyone else wants to hack the source in the meantime, feel free -- if it's a site update then it should be nothing more than locating and updating a little regex code.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Mon Jul 23, 2007 6:18 pm UTC

I'm afraid I don't have Visual Studio installed at the moment.
However, I might take a look at the site's HTML code to figure out what's been changed - but that could take a few days, as I'm pretty busy myself these days... :(

User avatar
davean
Site Ninja
Posts: 2498
Joined: Sat Apr 08, 2006 7:50 am UTC
Contact:

Postby davean » Tue Jul 24, 2007 1:16 am UTC

Ace_NoOne wrote:Andrew, your xkcd Wallpaper Randomiser has become non-functional.
I assume it has something to do with changes on the site*.

Can anyone confirm this?


* Three symptoms that led me to believe there has been some work done:
1. The buttons for navigating the comics look different.
2. The ATOM feed suddenly showed the three latest comics as new.
3. http://fora.xkcd.com no longer works (redirects to the front page).


There *was* no atom feed before, silly.

Ace_NoOne
Posts: 60
Joined: Fri Dec 08, 2006 8:50 am UTC
Location: Germany

Postby Ace_NoOne » Tue Jul 24, 2007 5:59 am UTC

davean wrote:There *was* no atom feed before, silly.
Hehe - sorry, it was the RSS feed then. (I just saw those old items pop up in Bloglines, and went to the site to see whether it was RSS or ATOM... )
I'll switch to ATOM then.

Anyway, davean, could you elaborate on what's been changed?

ubergeek42
Posts: 136
Joined: Tue Mar 20, 2007 2:03 am UTC

Postby ubergeek42 » Tue Jul 24, 2007 5:28 pm UTC

Instead of each comic being its own html page, for example
http://xkcd.com/c112.html, they are now folders on the server eg http://xkcd.com/112/ The original links still work, but they redirect to the new style, so the second change might not be necessary...

Heres a fix(I think, I'm on linux, can't compile or test this, but I think its right.)

Code: Select all

In Form1.cs:
Line 56
-Regex RegExp = new Regex("http://xkcd.com/c(\\d+).html", RegexOptions.Multiline);
+Regex RegExp = new Regex("http://xkcd.com/(\\d+)/", RegexOptions.Multiline);

Line 115
-WebRequest Request = WebRequest.Create(String.Format("http://xkcd.com/c{0}.html", Comic));
+WebRequest Request = WebRequest.Create(String.Format("http://xkcd.com/{0}/", Comic));

User avatar
davean
Site Ninja
Posts: 2498
Joined: Sat Apr 08, 2006 7:50 am UTC
Contact:

Postby davean » Tue Jul 24, 2007 11:56 pm UTC

Ace_NoOne wrote:
davean wrote:There *was* no atom feed before, silly.
Hehe - sorry, it was the RSS feed then. (I just saw those old items pop up in Bloglines, and went to the site to see whether it was RSS or ATOM... )
I'll switch to ATOM then.

Anyway, davean, could you elaborate on what's been changed?


URL structure has been modified, we have gotten ride of apache for the main parts of the site (finally), we support better versions of syndication (Some of those RSS version's specs are really horrid, see this for an explanation). We have a random button now. Things are cleaned up so that data center redundancy can be done better for higher reliability. The custom CMS behind the site was rewritten in python (still not sure what I think of the language) in such a way as to be a lot cleaner to add features to.

Uh, oh, right, there are machine parsable data files for each comic also.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests