4
UPDATED-STATEMENT: 20181118@205016@SUN A suggestion was made to manually focus the Virtual Desktop of my choice, but this eliminates me from doing other tasks on another Virtual Desktop. Still waiting for a valid take-care-ofitself solution.
UPDATED-STATEMENT: 20181118@210851@SUN There is no way autohotkey can work with moving apps to a specific Virtual Desktop on it's own without dll-calls (*see below*). In other words, the use of dll-files is required for the time being that is Windows language and not as convenient as an ahk-statement. Who knows, maybe ahk can implement this function someday to keep it lightweight and add it to their statement-library.
UPDATED-TATEMENT: 20190509@212923@THU @Shayan suggested sdias-win10virtualdesktopenhancer as a way to manage windows and virtual desktops.
SPECIFICATION:
win10_1803_17134.407
autohotkeyahk_1.1.30.00_20180822

After the 5-month idle time for this topic i've now stopped using virtual desktops.

Virtual Desktops doesn't make navigation any easier, perhaps the clear view of the wallpaper will be enjoyed by some, but not me.

WHY I USE AHK

Using Autohotkey in Windows 10 to open up frequently used apps which will not duplicate opening up a program if it has already launched (IfWinNotExist). Then, move them to where I prefer each app to be (WinMove). I have set a timer (Sleep) because some apps open up slower than others (I would be interested to know if there is a better way to do this). I like ahk for how lightweight it is, but there's Windows 10 related functions that aren't documented from them like Virtual Desktop involvement.

PROBLEM

Problem is that there isn't a way to use WinMove for specifying a Virtual Desktop for win10 to launch an application in. So I struggle running my frequentlyusedapps-script and going into another Virtual Desktop to open up other apps simultaneously. This is because what ever Virtual Desktop i'm focusing on is what my frequentlyusedapps-script will move the window to.

QUESTION

How to have autohotkey move an application to a Virtual Desktop of my choice? I don't think it's possible with ahk-WinMove If the virtualdesktop-number isn't created then the script will obviously fail to move the app, but this isn't my focus just yet. I could otherwise create the appropriate number of Virtual Desktops prior to launching my frequentlyusedapps-script. What I mean by having a way for ahk to achieve this is to be pure ahk code and not fork/branch that will probably be not updated for the unfortunate reasons. If it does have to come from a branched project then let's discuss what ahk elements they're using to involve Virtual Desktop to move windows to a specific one.

!^+o::
  IfWinNotExist, ahk_exe program1.exe
    Run "C:\Dir\program1.exe"
    WinWait, ahk_exe program1.exe
    WinMove, ahk_exe program1.exe,, 953, 61, 967, 1019
    TrayTip, openfrequentlyusedapps, ok, 1,
  IfWinNotExist, ahk_exe program2.exe
    Run "C:\Dir\program2.exe"
    WinWait, ahk_exe program2.exe
    WinMove, ahk_exe program2.exe,, -7, 61, 813, 516
    TrayTip, openfrequentlyusedapps, ok, 1,

SOLUTION

  • As per @miroxlav, "there is no way Autohotkey can do this with their current statements." Although not impossible, but requires more effort as it's now Windows language is using ahk w/ dll-calls.
    • It would help a lot of someone is able to show how this even works
NOTE
10
  • 1
  • 1
    I wanted to stick with the roots of ahk and not a branch away from it's core (due to branding). What do you think this yet another enhancer uses to move windows to another desktop? Because I noticed the use of dll-files which isn't lightweight anymore.
    – fohrums
    Nov 17, 2018 at 22:31
  • What's wrong with using dll-files to facilitate or even make possible the execution of tasks if they come from a secure site?. No program can execute all its functions and various features using its own modules or resources. And dll-files are files that contains specific code that many installed programs use and share. Nov 18, 2018 at 19:54
  • How do you even know those dll-files online are even secured? If getting dll-files from another software, then that only means you have to have the software which is impossible to obtain if you don't have it in the first place especially if you don't have internet at the time being. I really meant it when I talked about using ahk and it being "lightweight". I just need help on using dll-calls and what dll-files are required as dependencies to move windows to a specific virtual desktop.
    – fohrums
    Nov 18, 2018 at 21:29
  • @fohrums Use Windows 10 Virtual Desktop Enhancer it has all the features you need.
    – Shayan
    Apr 25, 2019 at 20:31

4 Answers 4

2

Go with keyboard shortcuts.

Inside the script, when you sequentially start your apps, in the middle of your sequence simply switch to the next desktop by sending Ctrl+Win+:

;here you run and position your windows
Run app1
Move app1  'etc.
WinWait app1window   'wait until app window is open

;switch to next desktop*  (see note below the code)
Send ^#{Right}

;opening apps on next desktop
Run app2
Move app2  'etc.

*) If you do not have that "next desktop" prepared at the time, send Ctrl+Alt+D instead to create and open it.

For more, see section Virtual desktops keyboard shortcuts in the official list of Windows keyboard shortcuts.


Update:

Functionality regarding other desktops as you request it is currently not present directly in AHK statements. You may be able to achieve it by using DLL calls from within the AHK.

3
  • That's an action after an action which only means I still have to be focused on the desktop I want the windows to so happen to land in once opened. This eliminates me from going into another virtual desktop simultaneously to do other tasks while I wait for slow apps to launch (but, thanks for letting me know about ahk-WinWait so I replaced ahk-Sleep with it instead).
    – fohrums
    Nov 18, 2018 at 10:49
  • That's not a problem for me I don't know why it would be. Virtual Desktop is marvelous for being able to simulate working on multiple computers but using just a single rig. I don't know the limit of how many is workable, but I then would use another Virtual Desktop to continue opening up other apps. I hope this answers you're concern.
    – fohrums
    Nov 18, 2018 at 11:00
  • Thanks for the help @miroxlav, I just needed to know that. I thought there was some hidden command that I had to dig up in order to achieve this, but it looks like it's farily new. I imagine AHK could make this possible some day as Windows 10 has been released for over 3-yrs now. Virtual Desktop doesn't seem like it's going to go away anytime soon so it's good I began this topic.
    – fohrums
    Nov 18, 2018 at 11:08
2

Following this answer you will be able to move current window to your next/prev virtual desktop with Win1 / Win2.

You are going to need VirtualDesktopAccessor.

Depending on the version of Windows 10 you're using (you can check with winver, search in start menu), download the correct x64 release.

For exmaple if you're using 1803 download this.

Or if you're using 1809, download this.

Create a new ahk script and put the dll file you downloaded and the script you just created in a same folder.

copy paste this in your script:

#SingleInstance Force

VDALoc := A_ScriptDir "\VirtualDesktopAccessor.dll"

hVirtualDesktopAccessor := DllCall("LoadLibrary", Str, VDALoc, "Ptr") 
GoToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "GoToDesktopNumber", "Ptr")
GetCurrentDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "GetCurrentDesktopNumber", "Ptr")
MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "MoveWindowToDesktopNumber", "Ptr")
activeWindowByDesktop := {}

MoveCurrentWindowToPrevDesktop() {
    global MoveWindowToDesktopNumberProc, GoToDesktopNumberProc, activeWindowByDesktop, GetCurrentDesktopNumberProc
    current := DllCall(GetCurrentDesktopNumberProc, UInt)
    WinGet, activeHwnd, ID, A
    if (current = 0) {
        return
    } else {
        activeWindowByDesktop[current - 1] := 0 ; Do not activate
        DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, current - 1)
        Send, {Blind}{Ctrl Down}{LWin Down}{Left Down}{Left Up}{LWin Up}{Ctrl Up}
    }
    return
}

MoveCurrentWindowToNextDesktop() {
    global MoveWindowToDesktopNumberProc, GoToDesktopNumberProc, activeWindowByDesktop, GetCurrentDesktopNumberProc
    current := DllCall(GetCurrentDesktopNumberProc, UInt)
    WinGet, activeHwnd, ID, A
    if (current = 3) {
        return
    } else {
        activeWindowByDesktop[current + 1] := 0 ; Do not activate
        DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, current + 1)
        Send, {Blind}{Ctrl Down}{LWin Down}{Right Down}{Right Up}{LWin Up}{Ctrl Up}
    }
    return
}

#1::MoveCurrentWindowToPrevDesktop()
#2::MoveCurrentWindowToNextDesktop()
0

Below is my AutoHotKey function that moves a window from the current desktop to an adjacent desktop (or a new desktop if there is only one desktop curently).

This is not a true answer because it does not offer selecting which desktop to move to, but serves my immediate needs of configuring apps to land on specific desktops.

It is pure AutoHotKey script code, as it does not depend upon third-party DLLs. For instance, it does not depend upon VirtualDesktopAccessor because, as of 2021-05-01, one bug (Stop working on windows 10 build 20231 #33) cannot be fixed until Microsoft provides a stable API.

This solution is slow as it has a lot of mouse and keyboard activity to accomplish its goal, along with sleeps to make it work. This is another reason why it will be good to use VirtualDesktopAccessor instead once that is possible. But if you need to position several windows into specific desktops, at least this provides a way to automate it, if you are willing to wait for it to perform it on each window.

I tested this as working on Windows 10 Version 1909 (OS Build 18363.1441).

; MoveWindowToOtherDesktop(title)
;
;  Move a window to some other desktop.
;
;  Example usage:
;
;    pid := MoveWindowToOtherDesktop("window title here")
;    if !pid
;    {
;      MsgBox, Could not find the window.
;    }
;
MoveWindowToOtherDesktop(title)
{
  WinGet, pid, PID, %title%
  if (pid)
  {
    ; Activate the window so that it will be the first one in the
    ; upper left corner in the windows shown in the Task View, right
    ; under the desktops in the first row:
    WinActivate, %title%

    ; Sent Win+Tab to open up the Task View:
    Send, #{Tab}

    ; Wait for it to be active:
    WinWaitActive, Task

    ; Move mouse cursor into that upper left window
    ;   You will have to adjust these coordinates for the size of your monitor:
    MouseMove, 300, 300

    ; For some reason, we need to sleep here, as otherwise the right
    ; mouse click opens the menu but then it quickly
    ; disappears. Reason unknown:
    Sleep, 500

    ; Click right mouse button to bring up the context menu:
    Click, Right

    ; Another little sleep is needed here again as otherwise the first {Down} arrow is somehow ignored:
    Sleep, 500

    ; Send two down arrows to get to "Move to":
    Send, {Down}{Down}

    ; Another little sleep is needed here again for similar reasons as above:
    Sleep, 500

    ; Send one right arrow to get to the selection of the desktops:
    Send, {Right}

    ; Another little sleep is needed here again for similar reasons as above:
    Sleep, 500

    ; Send Enter key to select whatever desktop happens to be there in that position:
    ;
    ;   Note: If there is no desktop to the right, the menu entry will
    ;   point to Desktop 1 and that is the desktop the window will be
    ;   moved to. There is a "New Desktop" menu entry, but it seems
    ;   impossible to select it (Up/Down arrows rotate, and Home/End
    ;   are not recognized).
    ;
    Send, {Enter}

    ; Exit Task View:
    Send, {Esc}

    return %pid%
  }
  return
}
0

I use the following script fot better workspace management using AHK:

; Workspace menagement
;; Mask office opening with Shift+Alt+Ctrl+Win
#^!Shift::
#^+Alt::
#!+Ctrl::
^!+LWin::
^!+RWin::
Send {Blind}{vk07}
return

;; Ctrl-Alt-Shift-Arrow for moveing window to next workspace
!^+Right::
; Get current window
WindowId := WinExist("A")
if (WindowId) {
    ; Hide the window and show it again in in the next workspace
    WinHide ahk_id %WindowId%
    ; Use Blind and alt and shift up so that only ctrl and win are active
    Send {Blind}{Alt up}{Shift up}^#{Right}
    WinShow ahk_id %WindowId%
    WinActivate ahk_id %WindowId%
}
return

!^+Left::
; Get current window
WindowId := WinExist("A")
if (WindowId) {
    ; Hide the window and show it again in in the next workspace
    WinHide ahk_id %WindowId%
    ; Use Blind and alt and shift up so that only ctrl and win are active
    Send {Blind}{Alt up}{Shift up}^#{Left}
    WinShow ahk_id %WindowId%
    WinActivate ahk_id %WindowId%
}
return

;; Ctrl-Alt-Arrow for chaning workspaces
!<^>Right::
Send ^#{Right}
return

!<^>Left::
Send ^#{Left}
return

I found that hiding the window, changing workspaces and opening it again is the cleanest and most reliable way to move a window to the next workspace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.