How to hide/show a specific Sidebar gadget on Windows based on a hotkey

Well, we will write a little code first. If you are not scared by that fact, go and install AutoIt. It is a great scripting language for Windows. It saves the day when you just want what you want to be accomplished and not worry about anything else. Anything that comes up to your mind, first check this thing, if you cannot find it, or it is limited in some sense, only then try implementing it wholly by yourself. Fortunately, what we are going to do is actually quite basic, so it has all that we need.

Here is my code for hiding and showing the ToDo gadget based on a hotkey.

HotKeySet("!t", "ShowHideTodo") ;Change this line

AutoItSetOption("WinTitleMatchMode", 4)

While 1
    Sleep(100)
WEnd

Func ShowHideTodo()

$hWindows = WinList("[CLASS:SideBar_HTMLHostWindow]")
If @error Then
    MsgBox(4096, "Error", "Could not find the correct window")
Else
    For $i = 1 to $hWindows[0][0]
    $size= WinGetClientSize($hWindows[$i][1])

    If $size[0] > 150 And $size[1] > 150 Then ;Change this line
        $state = WinGetState($hWindows[$i][1], "")
        If Not BitAnd($state, 2) Then
        WinSetState($hWindows[$i][1], "", @SW_SHOW)
        Else
        WinSetState($hWindows[$i][1], "", @SW_HIDE)
        EndIf
    EndIf

    Next

EndIf

EndFunc

What we are doing here is essentially giving it a classname and getting a list of handles to the windows that have this classname. These are the sidebar gadgets. So since there are probably more than one gadget on your desktop, we are getting a list of them by the WinList function, instead of using the WinGetHandle to get a handle for only one.

Next, we somehow need to identify the gadget that we want hidden/visible. I’ve thought about it a little, and I couldn’t find a better way than to discriminate them based on size. (Any other ideas?) So if the gadget is not resizable, this is a good option actually, the probability that another gadget has exactly the same size is rather low. But, if it is resizable, then it is better to give some interval instead, as I’ve done here with my ToDo gadget. ($size[0] is the width and $size[1] is the height btw.)

You could be wondering how we got the classname and size, if you haven’t used AutoIt before. It comes with another cool utility called "Window Info”. Just open it up and point to the window you want. Well, these gadgets are actually windows. So point up to the gadget you want and note these values.

Oh, and we set the hotkey here at this line.

HotKeySet("!t", "ShowHideTodo") ;this sets it to be run when ALT+t is pressed

Just change the letter to anything else you want.

 

Hmm, but what if I want to hide all of them? (AFAIK, Windows doesn’t have a hotkey for this.)

Well, then just remove these two lines from the above script, that’s it. =)

$size= WinGetClientSize($hWindows[$i][1])

    If $size[0] > 150 And $size[1] > 150 Then

 

OK, gotcha, but how will I use this thing? Well, I didn’t make a ready-to-be-used executable because everyone could be trying to hide/show something else. Thus, you need to install AutoIt, open up the customized SciTE Script Editor for AutoItv3(or any other editor actually, but this one has auto-complete features for the language used), copy and paste the code that I’ve given above, modify it to your purposes, and save it with .au3 extension. By double clicking the file you will run it, and if you want it to be always open, just add a shortcut for it to the startup folder.

Well, I guess that is it for now. Have a nice day. 🙂

Advertisements

1 Comment

  1. January 11, 2010 at 3:36 pm

    […] January 11, 2010 at 3:36 pm (Useful information, Windows) Tags: autoit, automated, contacts, desktop, floating, hide, hotkey, messenger, plus, script, shortcut, show Earlier today, I’ve made another post doing something very similar to this one. You can have a look at it here. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: