Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Multiple virtual desktops


  • Please log in to reply
51 replies to this topic
WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
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- 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- sends the active window to the desktop .

v1.0, 04. Nov. 2005
It works!
Switching can be done using Alt-, 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:

; 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 <[email protected]> 
; 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


Terrapin
  • Members
  • 107 posts
  • Last active: Feb 06 2007 03:38 PM
  • Joined: 15 Aug 2005
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. :)

Nuwan
  • Members
  • 32 posts
  • Last active: Jun 24 2015 04:46 AM
  • Joined: 08 Oct 2005
Dear WileECoyote,

I'm very Interest of your script.

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

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

;-------------------------------------------------------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

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
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
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

EXtes
  • Members
  • 7 posts
  • Last active: Apr 07 2012 11:31 AM
  • Joined: 01 Oct 2005
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

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
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

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
Here's the new version. Use ctrl-alt- 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.

Nuwan
  • Members
  • 32 posts
  • Last active: Jun 24 2015 04:46 AM
  • Joined: 08 Oct 2005
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

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
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.

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
Yes, that's a good idea! Thank you. I already changed it and I hope that I did it right.
Christian

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
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

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
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

Klaus
  • Members
  • 333 posts
  • Last active: Feb 17 2015 09:31 AM
  • Joined: 12 May 2005
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.

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
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

WileECoyote
  • Members
  • 9 posts
  • Last active: Dec 02 2005 11:19 AM
  • Joined: 04 Nov 2005
fixed a small bug concerning the windows' activation