AutoHotkey Community

It is currently May 27th, 2012, 3:50 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 51 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
PostPosted: November 4th, 2005, 9:36 am 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Hi there,

I created a very small and simple script to switch between multiple virtual desktops (just like e.g. CoolDesk does). Up to now only switching using Alt-<desktop index> is possible. Alt-0 quits the script and displays all open windows at once.
Please tell me if you encouter any bugs.
More functions coming soon (sending windows to other desktops, etc.) ...
Christian

Version history:
v1.11, 22. Nov. 2005
Fixed bug: windows are now corrrectly activated after switching/sending

v1.1, 05. Nov. 2005
Added feature: pressing Ctrl/Alt-<index> sends the active window to the desktop <index>.

v1.0, 04. Nov. 2005
It works!
Switching can be done using Alt-<desktop index>, e.g. Alt-1. Pressing
Alt-0 will exit the script and show all windows from all virtual desktops
at once.

The newest version can always be found here:

Code:
; DesktopSwitch
;
; AutoHotkey Version: 1.0.40.00 (that's at least the version I'm using)
; Language:       English
; Platform:       Win9x/NT/XP
; Author:         Christian Schüler <c_schueler@gmx.at>
; last changes:   22. Nov. 2005
;
; Script Function:
;
; A small tool for switching between multiple virtual desktops.
; Use Alt-<desktop index> (e.g. Alt-2) to switch between desktops and
; Alt-0 to quit the script, showing all windows on all virtual desktops
; at once. Currently, 4 desktops are supported, because more will start
; to confuse me...
;
; Version history:
;
; v1.11, 22. Nov. 2005
; Fixed bug: windows are now corrrectly activated after switching/sending
;
; v1.1, 05. Nov. 2005
; Added feature: pressing Ctrl/Alt-<index> sends the active window to the desktop <index>.
;
; v1.0, 04. Nov. 2005
; It works!
; Switching can be done using Alt-<desktop index>, e.g. Alt-1. Pressing
; Alt-0 will exit the script and show all windows from all virtual desktops
; at once.


; ***** initialization *****

SetBatchLines, -1   ; maximize script speed!
SetWinDelay, -1
OnExit, CleanUp      ; clean up in case of error (otherwise some windows will get lost)

numDesktops := 4   ; maximum number of desktops
curDesktop := 1      ; index number of current desktop

WinGet, windows1, List   ; get list of all currently visible windows


; ***** hotkeys *****

!1::SwitchToDesktop(1)
!2::SwitchToDesktop(2)
!3::SwitchToDesktop(3)
!4::SwitchToDesktop(4)

^!1::SendActiveToDesktop(1)
^!2::SendActiveToDesktop(2)
^!3::SendActiveToDesktop(3)
^!4::SendActiveToDesktop(4)

!0::ExitApp


; ***** functions *****

; switch to the desktop with the given index number
SwitchToDesktop(newDesktop)
{
   global

   if (curDesktop <> newDesktop)
   {
      GetCurrentWindows(curDesktop)

      ;WinGet, windows%curDesktop%, List,,, Program Manager   ; get list of all visible windows

      ShowHideWindows(curDesktop, false)
      ShowHideWindows(newDesktop, true)

      curDesktop := newDesktop

      Send, {ALT DOWN}{TAB}{ALT UP}   ; activate the right window
   }

   return
}

; sends the given window from the current desktop to the given desktop
SendToDesktop(windowID, newDesktop)
{
   global
   RemoveWindowID(curDesktop, windowID)

   ; add window to destination desktop
   windows%newDesktop% += 1
   i := windows%newDesktop%

   windows%newDesktop%%i% := windowID
   
   WinHide, ahk_id %windowID%

   Send, {ALT DOWN}{TAB}{ALT UP}   ; activate the right window
}

; sends the currently active window to the given desktop
SendActiveToDesktop(newDesktop)
{
   WinGet, id, ID, A
   SendToDesktop(id, newDesktop)
}

; removes the given window id from the desktop <desktopIdx>
RemoveWindowID(desktopIdx, ID)
{
   global   
   Loop, % windows%desktopIdx%
   {
      if (windows%desktopIdx%%A_Index% = ID)
      {
         RemoveWindowID_byIndex(desktopIdx, A_Index)
         Break
      }
   }
}

; this removes the window id at index <ID_idx> from desktop number <desktopIdx>
RemoveWindowID_byIndex(desktopIdx, ID_idx)
{
   global
   Loop, % windows%desktopIdx% - ID_idx
   {
      idx1 := % A_Index + ID_idx - 1
      idx2 := % A_Index + ID_idx
      windows%desktopIdx%%idx1% := windows%desktopIdx%%idx2%
   }
   windows%desktopIdx% -= 1
}

; this builds a list of all currently visible windows in stores it in desktop <index>
GetCurrentWindows(index)
{
   global
   WinGet, windows%index%, List,,, Program Manager      ; get list of all visible windows

   ; now remove task bar "window" (is there a simpler way?)
   Loop, % windows%index%
   {
      id := % windows%index%%A_Index%

      WinGetClass, windowClass, ahk_id %id%
      if windowClass = Shell_TrayWnd      ; remove task bar window id
      {
         RemoveWindowID_byIndex(index, A_Index)
         Break
      }
   }
}

; if show=true then shows windows of desktop %index%, otherwise hides them
ShowHideWindows(index, show)
{
   global

   Loop, % windows%index%
   {
      id := % windows%index%%A_Index%

      if show
         WinShow, ahk_id %id%
      else
         WinHide, ahk_id %id%
   }
}

; show all windows from all desktops on exit
CleanUp:
Loop, %numDesktops%
   ShowHideWindows(A_Index, true)
ExitApp


Last edited by WileECoyote on November 22nd, 2005, 2:12 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 6th, 2005, 6:38 am 
Offline

Joined: August 15th, 2005, 7:15 am
Posts: 107
Location: North Carolina
Christian... I tried it out. I am a big fan of VDM's (or to be precise, goScreen, which I've been using for about three years). Your script seems to work very nicely for me. I am interested in seeing how it develops.

I would encourage you to google for 'goScreen' and give it a try, as it may give you some ideas for features.

My thought as to the next feature would be some sort of on-screen indicator showing which 'desktop' is active. If it could also indicate what apps are visible on each desktop, that would be a plus. I'd like to put some thought into it.

Thanks,

Bob

_________________
When it comes to Binary, there are 10 types of people. Those who get it and those who don't. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Modification idea
PostPosted: November 7th, 2005, 7:28 pm 
Offline

Joined: October 8th, 2005, 9:22 am
Posts: 29
Location: Sri Lanka
Dear WileECoyote,

I'm very Interest of your script.

It is wonderful............................................

I have add some gui function to it, pls check.

Code:
;-------------------------------------------------------Begin

SetBatchLines, -1   ; maximize script speed!
SetWinDelay, -1
OnExit, CleanUp      ; clean up in case of error (otherwise some windows will get lost)

numDesktops := 4   ; maximum number of desktops
curDesktop := 1      ; index number of current desktop

WinGet, windows1, List   ; get list of all currently visible windows
;------------------------------------------------
Gosub ,GUIM
;------------------------------------------------
!1::SwitchToDesktop(1)
!2::SwitchToDesktop(2)
!3::SwitchToDesktop(3)
!4::SwitchToDesktop(4)
!0::ExitApp
;------------------------------------------------
Gui Destroy
;------------------------------------------------
SwitchToDesktop(newDesktop)
{
   global

   if (curDesktop <> newDesktop)
   {
      WinGet, windows%curDesktop%, List,,, Program Manager   ; get list of all visible windows

      ShowHideWindows(curDesktop, false)
      ShowHideWindows(newDesktop, true)

      curDesktop := newDesktop

;------------------------------------------------
Gosub ,GUIM
;------------------------------------------------
   }

   return
}

; if show=true then shows windows of desktop %index%, otherwise hides them
ShowHideWindows(index, show)
{
   global

   Loop, % windows%index%
   {
      id := % windows%index%%A_Index%

      WinGetClass, windowClass, ahk_id %id%
      if windowClass <> Shell_TrayWnd      ; do not show/hide task bar
      {
         if show
            WinShow, ahk_id %id%
         else
            WinHide, ahk_id %id%
      }
   }
}


CleanUp:
Loop, %numDesktops%
   ShowHideWindows(A_Index, true)
ExitApp

;------------------------------------------------Gui Begin
GUIM:
{
Gui, Add, checkbox, x5 y35 w60 h30 gScreen1, Screen1
Gui, Add, checkbox, x66 y35 w60 h30 gScreen2, Screen2
Gui, Add, checkbox, x5 y66 w60 h30 gScreen3, Screen3
Gui, Add, checkbox, x66 y66 w60 h30 gScreen4, Screen4

Gui, Color, 8388736
Gui, +Lastfound
WinSet, TransColor, 8388736
Gui, -Caption
Xp:= A_ScreenWidth - 126
Yp:= A_ScreenHeight - 125
Gui, Show,x%XP% y%Yp%, MultiScreen
return

Screen1:
SwitchToDesktop(1)
return

Screen2:
SwitchToDesktop(2)
return

Screen3:
SwitchToDesktop(3)
return

Screen4:
SwitchToDesktop(4)
return

GuiClose:
ExitApp
}
return
;------------------------------------------------Gui End

;------------------------------------------------------End


I like your suggetions

But "send the currently active window to another desktop" and latest modifications are not in above script.

Nuwan

Edit by Moderator: Please use the Code tags to mark your code. I did it this time for you. Thanks


Last edited by Nuwan on November 21st, 2005, 12:59 pm, edited 7 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 8th, 2005, 9:26 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
In your SwitchToDesktop function your code is defining several subroutines. That is not working. Please restructure your code. If you use syntacticall indentation these things are easy to spot.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 8th, 2005, 5:01 pm 
Offline

Joined: October 1st, 2005, 12:53 pm
Posts: 7
Hello

Look at www.Dexpot.de (english version available). It's my favourite virtual desktop prog. Perhaps it gives you some ideas. But I will track this project.

EXtes


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 9th, 2005, 1:07 pm 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Thanks for all your nice feedback!
I new version is already working (also supporting sending windows to other desktops). I will upload it as soon as possible (possibly this evening).
Christian


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 9th, 2005, 6:45 pm 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Here's the new version. Use ctrl-alt-<number> to send the currently active window to another desktop.
I had also a look at the small GUI - works fine on my computer! But I actually don't like this kind of guis, because 1. I want to use the keyboad and 2. I don't want to have another window on my desktop. But: why not integrating it (with another addition of showing the active desktop) and making it an option (maybe by adding a small small options dialog). I will have a look at this...

Concerning the code: I'm not very happy with using a global array because it provokes errors. Does anyone have a better idea? (maybe strings, but which character should be used to separate them?!)

Christian

Update - the newest version can be found on top of this topic.


Last edited by WileECoyote on November 11th, 2005, 10:26 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject: Its Nice
PostPosted: November 10th, 2005, 3:25 am 
Offline

Joined: October 8th, 2005, 9:22 am
Posts: 29
Location: Sri Lanka
Hai wile

:D
It is Nice .
But you can implement it with active(Current) desktop Indicator.

But problem is no active desktop indicator with My Gui.

I am waiting for your new version....


Nuwan


Last edited by Nuwan on November 21st, 2005, 12:07 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 10th, 2005, 4:54 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
If this latest version of the script is generally superior to the one at the top of this topic, it is probably best to edit your first post replace the old version. Otherwise, people will often not find the latest version because they don't scroll down far enough.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 11th, 2005, 10:28 am 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Yes, that's a good idea! Thank you. I already changed it and I hope that I did it right.
Christian


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 14th, 2005, 10:35 am 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hello, WileECoyote,
really nice script that, I think it will rest in my daily use, but let me direct your attention to the following:
I switch to desktop 2 (alt-2), start an application (let's say PSPad), send this application to desktop 3 (ctrl-alt-3). Now the application windows moves correctly to desktop 3, but the application's taskbar button remains in the taskbar of desktop 2!
Now we have got: desktop 2 without the PSPad window but with PSPad's taskbar button, and desktop 3 with a PSPad window without it's taskbar button!
For desktop 3 you can "create" a taskbar button by minimizing/maximizing the application window, but along with the button no awaited mimimizing/maximizing functionality comes.
Clicking the taskbar button in desktop 2 produces an appearing/disappearing PSPad title bar.
I hope my observations are correct.
In the end let me say it again, nice work that I down want to tear down with these remarks.
Carry on, I will keep an interested eye on it!
Klaus


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 4:56 pm 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Hi Klaus,
thank you for having a critical eye on the script, so I will be able to improve it.

But I (un)fortunately never observed this behaviour on my Computer and even on my Office PC it's working fine. Can you tell me some more about your operating system and installed software which might be able to influence window management (e.g. CoolDesk, goScreen, Dexpot, etc.). Maybe we can then find the source of this strange behaviour!

Christian


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2005, 8:39 am 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hi, WileECoyote,
thank you very much for answering to my post.
After a little research I can give a supplement to the described behaviour:
This weird "taskbar buttoning" occured on my office computer (XP SP2, nothing special), where - without that I knew - AltDesk (another commercial desktop switcher) was installed by our sysops.
Now I could see that this "cooperation" of two switchers may cause some trouble (switching desktops in a desktop that is switched already ...). I think it's not worth to put greater efforts onto that, neither in describing all troubles in cooperation of two switchers nor in working around of that.
I think AltDesk is a nice tool, but as a real fan of AutoHotkey, I will remove it and stay a faithful user of yours.
I hope not to have caused too much confusion.
Best wishes
Klaus
By the way: Like others before me I'd like to see a little switch indicator that tells me what happens while pressing ALT-n. Somewhere in the forum I once read about a CD-ejecting script that indicates the opening of the CD-tray by inserting a little "Eject"-JPEG that fades out rapidly. I will look for that.
Another improvement could be a systray icon that offers a little clickable desktop list when hovering over it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2005, 2:01 pm 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
Thank you for the compliment of preferring my script :-)
Since it has been requested several times, the indicator (and gui) will come in the next version. However, my focus will still be on the keyboard control, since it seems to be the most efficient way for desktop switching to me.
By the way: I'm thinking of an additional advanced desktop layout control, but this will take some time...

Greetings,
Christian


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Bugfix
PostPosted: November 22nd, 2005, 2:13 pm 
Offline

Joined: November 4th, 2005, 9:24 am
Posts: 9
fixed a small bug concerning the windows' activation


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 51 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group