Extending the desktop (via a script)

Published August 26, 2006 by Robbie

Over the last few months, several articles have been written about the productivity boost associated with having two (or more) monitors (e.g., NYTimes). I’ve had dual monitors at work and home for two months and I love it. Extending the desktop environment over much larger screen real estate means you don’t have to constantly switch between various applications. I can have my email, IM, or browsers up in one monitor while I read a document or code in an IDE in another.

One pain associated with doing this on Windows XP is the fact that you have to go into the Display Properties, select the second montior, and check “Extend my Windows desktop onto this monitor.”. This is a pain because sometimes when I undock my laptop, it won’t recognize that the second monitor isn’t attached. And even if it does recognize that it is disconnected, when I hook up to the docking station again I have to go back into the properties and re-check the “extend my desktop” box.

I figured there had to be a way to script this setting. I found the ChangeDisplaySettingsEx API, but it wasn’t easily scriptable. Next I looked at simulating GUI movements. A quick Google search led me to AutoIt, which looked like exactly what I needed. Plus it is free!

AutoIt was very easy to install and use. It only took me a couple minutes to create this script which will toggle the ‘extend my desktop’ box:

Run("C:\WINDOWS\system32\control.exe desk.cpl,@0,3")
WinWaitActive(”Display Properties”)
Send(”{TAB}”)
Send(”{DOWN}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{TAB}”)
Send(”{SPACE}”)
Send(”{ENTER}”)

It’s a hack, but it works great. Even better than that, AutoIt has an option to compile scripts to executables. This won’t work for everyone, but here is my compiled script.

This post has been viewed 7,316 times

Save to del.icio.us | Digg it | Reddit

Comments (23)

  1. Anonymous says:

    You rule!

    I was searching on the web for the same thing, found AutoIt, but couldn’t figure out how to run the control panel. Thanks!

    Posted October 26, 2006 @ 2:12 pm
  2. Anonymous says:

    quick. easy. thanks

    Posted November 13, 2006 @ 6:37 pm
  3. Jon Hassell says:

    That’s good stuff right there — exactly what I needed for my new MacBook Pro.

    Posted November 30, 2006 @ 2:30 pm
  4. Anonymous says:

    For the easily confused, save what Robbie described in a file on your desktop with a name like DualScreen.au3. If you have autoit installed, all you need to do to switch monitor mode is to double click on this file.

    Posted December 21, 2006 @ 8:32 pm
  5. Jason Gilmore says:

    Wonderful tip, thank you. I couldn’t get the script working on Vista, although it gave me a great head start, thank you! Apparently the display settings tab sequence has changed on Vista, however the below script will enable dual-monitors just fine. I’ve one problem left to solve, and that’s using the same script to *toggle* the monitors; I want to click on the same script to both enable and disable dual screens. Ideas appreciated!

    ;;;;;;;;;;;;;;;;;
    Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3″)

    ; Wait for the window to appear and become active
    WinWaitActive(”Display Properties”, “”, 2)

    Send(”{TAB}”)
    Send(”{DOWN}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{SPACE}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{TAB}”)
    Send(”{ENTER}”)

    ; Now confirm the dialog box
    Send(”{TAB}”)
    Send(”{ENTER}”)

    ; Close the script
    Exit
    ;;;;;;;;;;;;;;;;;

    Posted February 16, 2007 @ 7:11 am
  6. Steven says:

    OMG! OMG! OMG!

    much love to you! no, seriously..i`ve been looking for ways to do this for weeks. i initially though i`d be able to do it via WSH/JScript/VBScript..but after many, may tries it was having none of it!

    so yeh…for some reason some games i play need me to disable my 2nd screen….and it`s a major pain doing the GUI all the time, so i`m just gonna pop off and try this :)

    thanks!

    OMG!^5

    just played about for like 2mins and i got a toggle-ing script, i done it ‘backwards’ as if it`s disabled to start with then the ‘make primary’ is disabled..so working backwards will (well..it should nearly always) work :)

    ;
    ; — toggle-screen.au3
    ;

    ; exec cpanel app `display settings`
    Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3″)

    ; wait for window to be active
    WinWaitActive(”Display Properties”)

    ; select 2nd display
    Send(”{TAB}”)
    Send(”{DOWN}”)

    ; work back to the ‘extend desktop’ control
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)

    ; toggle ‘extend desktop’ control and apply
    Send(”{SPACE}”)
    Send(”{ENTER}”)

    ;
    ; — E.O.F.
    ;

    Posted April 24, 2007 @ 7:32 pm
  7. Nico says:

    Thanks!!

    I was doing this manually every day…
    until I Googled it 2 minutes ago!

    Great work,

    Nico.

    Posted May 16, 2007 @ 12:56 am
  8. Jeremy says:

    Wonderful! That sure saves me a huge pain in the ass!

    Posted May 20, 2007 @ 7:25 pm
  9. Stevo says:

    Had to post thanks also - good hack to a pain in the ar*e procedure just to get another monitor on….

    Posted June 5, 2007 @ 7:19 am
  10. Toby says:

    u rock

    Posted August 7, 2007 @ 2:53 pm
  11. Bill says:

    Fantastic! Exactly what I was looking for. I had been manually doing this. Total time (including downloading AutoIT was 15 minutes).

    I did have problems with cutting and paste on the scripts. The double quotes were being read wrong. I re-moved and re-added them to the scripts and everything started working.

    Posted October 4, 2007 @ 2:40 pm
  12. Anthony Coletti says:

    Eureka! I finally found the answer! (And the correct Google seach terms - ’script extend desktop’). I can’t tell you how much time I’ve wasted trying to learn windows scripting or some other batch language to get my computer to automate that one simple task. My thinking was, if I am repeating the same steps to do the same thing every day, then I should be able to automate it. Thank you very much!

    P.S. Your script worked on my PC straight away - but I’m downloading AutoIt so I can lean how to write my own scripts.

    Posted October 18, 2007 @ 5:33 am
  13. Imran E says:

    Thanks… rewrote it in java.

    Posted April 11, 2008 @ 7:05 am
  14. David says:

    Awesome. Call me lazy but this is exactly what I wanted.

    Posted April 15, 2008 @ 11:21 am
  15. Dirk says:

    Very helpful.

    And if you need to move your secondary display to another position (mine is on the left) you can do it like that:

    ; set mouse coords to relative coords for the active window
    Opt(”MouseCoordMode”, 0)
    ; drag 2nd screen to the left
    MouseClickDrag(”left”, 250, 150, 50, 150)

    You might need to adapt the coordinates to your needs.

    Posted May 24, 2008 @ 4:42 am
  16. Jeremy says:

    If you have an nvidia card(s) installed, this single command will extend your desktop to two screens: (you can put it in a cmd file and stick that on your desktop if you want to)

    rundll32.exe nvcpl.dll,dtcfg setview 1 dualview

    If your start bar shows up on the wrong screen, run these two commands to move it to the other screen:

    rundll32.exe nvcpl.dll,dtcfg setview 2 normal
    rundll32.exe nvcpl.dll,dtcfg setview 2 dualview

    You can also change resolutions with nvcpl.dll,dtcfg - just search for “nvcpl.dll,dtcfg setwiew” on google.

    Posted June 20, 2008 @ 8:04 am
  17. Tom says:

    Hi,

    well, it seems like you guys talk about and have exactly what I need ! A little script to TURN ON / TURN OFF a secondary monitor. However, the .exe I downloaded from the to of this pages doesn’t work in Vista and I am not a scripter, so I don’t know how to make this work.
    I got Vista Ultimate on my Notebook and have a second monitor attached. I’d like to be able to enable/disable the secondary mnitor with ONE cick ! Is this possible and what do I need to do ?

    Thank you.

    PS: Besides posting here, please send me also an e-mail to jce2005@gmail.com letting me know. Thanks,

    Tom

    Posted September 6, 2008 @ 12:06 pm
  18. John says:

    Tweaked the previous posts for Vista, tested on Vista Ultimate x64 SP1 and AutoIT 3 but should work with all versions of Vista. Same command toggles Dualview on and off. :)

    Thanks to the previous posters, hope damn nVIDIA fixes the stupid nvcpl.dll for vista so that we can switch with a simple command line.

    ;
    ; — toggle-screen.au3
    ;

    ; exec cpanel app `display settings`
    Run(”C:\WINDOWS\system32\control.exe desk.cpl,@0,3?”)

    ; wait for window to be active
    WinWaitActive(”Display Settings”)

    ; select 2nd display
    Send(”{TAB}”)
    Send(”{DOWN}”)

    ; work back to the ‘extend desktop’ control
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)
    Send(”+{TAB}”)

    ; toggle ‘extend desktop’ control and apply
    Send(”{SPACE}”)
    Send(”{ENTER}”)

    ; wait for window to be active
    WinWaitActive(”Display Settings”)

    ; accept
    Send(”{TAB}”)
    Send(”{ENTER}”)

    ;
    ; — E.O.F.
    ;

    Posted September 23, 2008 @ 12:56 am
  19. jib says:

    I couldn’t get the script to run in AutoIt even when I copied and pasted it. But I figured out it didn’t like the “s must be the font of somthing. It worked when i retyped them all. Just through I would let people know :)

    Posted November 27, 2008 @ 4:52 am
  20. Rob says:

    I’ve tried copying and pasting and re-typing the above scripts but AutoIt v 1.77 does not like the Run statement and will not compile. I’m using WinXP Pro Sp3. Any ideas why I am running into this? This would be exactly what I am looking for b/c my remote software does not like extended desktops, especially since I cannot have both of the monitors at the same resolution, I just want to be able to double click an app or script and have it disable or enable the extended desktop. I’m getting an ‘Unable to Parse Line’ on that statement, but its not being specific.

    Thanks,
    Rob

    Posted March 7, 2009 @ 10:10 am
  21. Rob says:

    Ok… I think I got the compile thing fixed… I just went in and replaced the quotes with find and replace. I went Jib’s advise but when I retyped it, it seemed to still take the funky quotes… weird..

    Thanks,
    Rob

    Posted March 7, 2009 @ 10:24 am
  22. Rob says:

    Did I do something wrong? I noticed my posts were deleted and I think my last post might have been helpful.

    Rob

    Posted March 8, 2009 @ 8:02 pm
  23. scott says:

    Is there a mod for a macbook pro to extend the desktop across two external displays while the notebook is in clamshell mode? please tell me there is!

    Posted April 27, 2009 @ 3:00 pm

Write a Comment: