I'm trying to use dmenu and wmctrl to write a script that would present to me a list of all currently open windows and then switch to the one I select. It looks something like this:

Now, I'm using the window id I get from wmctrl -l (for instance, the "0x00c0005c" visible in the screenshot on the 2nd line) to determine exactly which window has been selected (I'm not using the window class because 2 windows belonging to the same application will have the same class, but different ids).
The only problem is: wmctrl -i -a works sometimes, but doesn't work other times - and some times, it switches, but to the wrong window! It's quite random. After searching online, the only web page I found that even mentions the issue is this one. It says:
WIN as a numerical window ID doesn't seem to work consistently. Perhaps not all options support it or something crazy is going on. It used to work very well for me. Re-test.
Can anyone tell me how I can resolve or work around this issue?
I'm using version 1.07-3 of wmctrl from the official repositories on an up-to-date Arch Linux machine, and Awesome Window Manager, if it helps. Thanks
wmctrl -aand the window title, which works around the problem, as long as you don't have windows with the same title. – crater2150 Jan 8 at 17:03wmctrl -a) is inconsistent for me. It might be a bug with Awesome window manager itself. – Vicky Chijwani Jan 8 at 23:16wmctrl -i -a $(wmctrl -l |cut -c1-40 |dmenu | cut -d " " -f 1)to test this. It seems quite reliable to me, handling cases where a window is on the same desktop, different desktop, minimized etc.. Even when multiple tags are active (though that removes the multiple tagging and activates only the tag of the target window.). Anyway, is the effect of this worse, better, or the same as your script? – kampu May 22 at 7:11