Your window manager simply doesn't have workspace 2 when you call screen.get_workspace(2)
.
Your code should work for all workspaces that actually exist. In GNOME 3 that means all workspaces that already have windows plus one empty workspace. This means that screen.get_workspace(screen.get_workspace_count() - 1)
should give you the "empty" workspace, if that was what you were looking for. Note that there's no spec that says the last workspace is empy, it's just what gnome-shell currently happens to do.
You can also ask the window manager to increase the number of workspaces on the screen, but it is under no obligation to do so: the GNOME 3 window manager will not do it.
Oh, and one thing to remember about Wnck:
At its creation, a WnckScreen object will not have fetched information from the X server. If queried immediately after its creation (via wnck_screen_get_windows() or wnck_screen_get_workspaces(), for example), the WnckScreen object will look like there are no workspaces nor windows on the screen. This information is fetched in the main event loop with an idle source, to avoid an expensive synchronous operation on startup. If no main event loop is used, or if the information is needed as soon as possible after the creation of the object, wnck_screen_force_update() can be used to explicitly fetch the information.
I don't think this is your problem (as you have a list of windows already), but I'm just double-checking as it's a nasty pitfall.