python、Windows 10:在特定虚拟桌面环境(工作空间)上启动应用程序

python, Windows 10: launching an application on a specific virtual desktop environment (work-spaces)

我有 3 个不同的 Windows 10 个虚拟桌面。当计算机启动时,我希望 python 将我的所有应用程序加载到不同的虚拟桌面中。

现在我只能在桌面 1 中启动程序。如何告诉 python 在桌面 2 和 3 中启动应用程序?

我正在使用 python 3.6.

TL; DR:使用 VDesk?


几年前 Windows 中似乎缺少对此的内置支持:

"Are you referring to task view feature "Multiple desktops" in Windows 10?

If yes, please be informed that you cannot have apps/programs automatically startup in different desktops.

-- A. User

我不知道 python-native 方法,但有几个关于该主题的答案更普遍地建议 VDesk -- https://github.com/eksime/VDesk

VDesk is a free, open source, program for the Windows 10 operating system that extends a system's virtual desktop functionality.

-- MSPO

这加上从 python(即 subprocess 模块)调用外部程序的常用方法应该有望获得您想要的效果。祝你好运。

How do I tell python to launch an app but in Desktop 2 and 3?

这可以通过以下方式实现:使用 subprocess.Popen() 启动您的应用程序,然后在 ctypes 的帮助下从 VirtualDesktopAccessor.dll 调用 GoToDesktopNumber() 来更改虚拟桌面,然后启动您的再次申请。使用 64 位 Windows 10 版本 10.0.18363.720.

进行测试

VirtualDesktopAccessor.dll by Jari Pennanen exports the functions a part of the mostly undocumented (by Microsoft) Virtual Desktop API. Put the dll 在当前工作目录中。

import ctypes, time, shlex, subprocess

def launch_apps_to_virtual_desktops(command_lines, desktops=3):
    virtual_desktop_accessor = ctypes.WinDLL("VirtualDesktopAccessor.dll")
    for i in range(desktops):
        virtual_desktop_accessor.GoToDesktopNumber(i)
        time.sleep(0.25) # Wait for the desktop to switch
        for command_line in command_lines:
            if command_line:
                subprocess.Popen(shlex.split(command_line))
        time.sleep(2) # Wait for apps to open their windows
    virtual_desktop_accessor.GoToDesktopNumber(0) # Go back to the 1st desktop

command_lines = r"""
"C:\Program Files (x86)\Google\Chrome Beta\Application\chrome.exe"
"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "C:\StudyGuide.pdf"
"C:\Program Files\Mozilla Firefox\firefox.exe"
"C:\Program Files\VideoLAN\VLC\vlc.exe"
""".splitlines()

launch_apps_to_virtual_desktops(command_lines)

需要 time.sleep() 调用,因为 Windows 不会立即更改虚拟桌面(可能是因为动画),并给进程时间来创建 windows。您可能需要调整时间。

请注意,某些应用程序只允许一个 instance/process,因此您无法为每个虚拟桌面获得多个单独的 windows(例如,默认设置的 Adob​​e Reader)。


我尝试的另一个策略是启动应用程序,休眠一段时间以允许创建 windows,然后调用 MoveWindowToDesktopNumber() 移动新进程创建的每个 window到不同的虚拟桌面。这样做的问题是,对于像 Chrome 或 Firefox 这样的应用程序,如果现有进程已经存在,新进程将立即关闭,因此它不会移动新的 windows (实际上属于另一个,旧进程)到另一个桌面。

import ctypes, time, shlex, subprocess
from ctypes.wintypes import *
from ctypes import windll, byref

def get_windows(pid):
    current_window = 0
    pid_local = DWORD()
    while True:
        current_window = windll.User32.FindWindowExA(0, current_window, 0, 0)
        windll.user32.GetWindowThreadProcessId(current_window, byref(pid_local))
        if pid == pid_local.value:
            yield current_window

        if current_window == 0:
            return

def launch_apps_to_virtual_desktops_by_moving(command_lines, desktops=3):
    virtual_desktop_accessor = ctypes.WinDLL("VirtualDesktopAccessor.dll")
    for i in range(desktops):
        pids = []
        for command_line in command_lines:
            if command_line:
                args = shlex.split(command_line)
                pids.append(subprocess.Popen(args).pid)

        time.sleep(3)
        for pid in pids:
            for window in get_windows(pid):
                window = HWND(window)
                virtual_desktop_accessor.MoveWindowToDesktopNumber(window, i)

command_lines = r"""
"C:\Program Files (x86)\Google\Chrome Beta\Application\chrome.exe"
"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "C:\StudyGuide.pdf"
"C:\Program Files\Mozilla Firefox\firefox.exe"
"C:\Program Files\VideoLAN\VLC\vlc.exe"
""".splitlines()

launch_apps_to_virtual_desktops_by_moving(command_lines)