AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple virtual desktops
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Fri Nov 04, 2005 9:36 am    Post subject: Multiple virtual desktops Reply with quote

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 Tue Nov 22, 2005 2:12 pm; edited 2 times in total
Back to top
View user's profile Send private message
Terrapin



Joined: 15 Aug 2005
Posts: 107
Location: North Carolina

PostPosted: Sun Nov 06, 2005 6:38 am    Post subject: Re: Multiple virtual desktops Reply with quote

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. Smile
Back to top
View user's profile Send private message
Nuwan



Joined: 08 Oct 2005
Posts: 28
Location: Sri Lanka

PostPosted: Mon Nov 07, 2005 7:28 pm    Post subject: Modification idea Reply with quote

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 Mon Nov 21, 2005 12:59 pm; edited 7 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Tue Nov 08, 2005 9:26 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail Visit poster's website
EXtes



Joined: 01 Oct 2005
Posts: 5

PostPosted: Tue Nov 08, 2005 5:01 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Wed Nov 09, 2005 1:07 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Wed Nov 09, 2005 6:45 pm    Post subject: Reply with quote

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 Fri Nov 11, 2005 10:26 am; edited 1 time in total
Back to top
View user's profile Send private message
Nuwan



Joined: 08 Oct 2005
Posts: 28
Location: Sri Lanka

PostPosted: Thu Nov 10, 2005 3:25 am    Post subject: Its Nice Reply with quote

Hai wile

Very Happy
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 Mon Nov 21, 2005 12:07 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Thu Nov 10, 2005 4:54 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Fri Nov 11, 2005 10:28 am    Post subject: Reply with quote

Yes, that's a good idea! Thank you. I already changed it and I hope that I did it right.
Christian
Back to top
View user's profile Send private message
Klaus



Joined: 12 May 2005
Posts: 193
Location: Münster, Germany

PostPosted: Mon Nov 14, 2005 10:35 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Tue Nov 15, 2005 4:56 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Klaus



Joined: 12 May 2005
Posts: 193
Location: Münster, Germany

PostPosted: Wed Nov 16, 2005 8:39 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Wed Nov 16, 2005 2:01 pm    Post subject: Reply with quote

Thank you for the compliment of preferring my script Smile
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
Back to top
View user's profile Send private message
WileECoyote



Joined: 04 Nov 2005
Posts: 9

PostPosted: Tue Nov 22, 2005 2:13 pm    Post subject: Bugfix Reply with quote

fixed a small bug concerning the windows' activation
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group