Well I connect to my server using a socket so I can use the local socket if I want to. I would have to see the purpose and point of a specific script to comment on how I would know if I opened a query window or not. Bottom line is that you use the /query command to open a query window. You could make an alias that does some checks then opens a query window with the /query command and put that alias in the "command to be performed when you double click the nicklist".
I monitor incoming queries and my input:?: event. I guess you could set a timer to check for query windows but that sounds pretty silly.