python,Windows 10:在特定的虚拟桌面环境(工作区)上启动应用程序 [英] python, Windows 10: launching an application on a specific virtual desktop environment (work-spaces)

查看:109
本文介绍了python,Windows 10:在特定的虚拟桌面环境(工作区)上启动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3 different Windows 10 virtual desktops. When the computer starts up, I want python to load all of my applications in the different virtual desktops.

现在,我只能在Desktop 1中启动内容.如何告诉python在Desktop 2和3中启动应用程序?

Right now I can only start things in Desktop 1. How do I tell python to launch an app but in Desktop 2 and 3?

我正在使用python 3.6.

I'm using python 3.6.

推荐答案

如何告诉python在桌面2和3中启动应用程序?

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

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

This can be achieved by launching your applications with subprocess.Popen(), then changing virtual desktop by calling GoToDesktopNumber() from VirtualDesktopAccessor.dll with the help of ctypes, and launching your applications again. Tested with 64-bit Windows 10 Version 10.0.18363.720.

Jari Pennanen

VirtualDesktopAccessor.dll 将功能导出为大多数未记录的(由Microsoft提供)虚拟桌面API的一部分.将 dll 放在当前工作目录中.>

VirtualDesktopAccessor.dll by Jari Pennanen exports the functions a part of the mostly undocumented (by Microsoft) Virtual Desktop API. Put the dll in the current working directory.

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不会立即更改虚拟桌面(可能是由于动画),并且不会给进程腾出时间来创建窗口.您可能需要调整时间.

The time.sleep() calls are needed because Windows doesn't change virtual desktops instantly (presumably because of animations), and to give the processes time to create windows. You might need to tweak the timings.

请注意,某些应用程序仅允许一个实例/进程,因此您无法为每个虚拟桌面获得多个单独的窗口(例如具有默认设置的Adobe Reader).

Note that some applications only allow one instance/process, so you can't get multiple separate windows for each virtual desktop (e.g. Adobe Reader with default settings).

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

Another strategy I tried was launching the applications, sleeping for a bit to allow the windows to be created, then calling MoveWindowToDesktopNumber() to move every window created by the new processes to different virtual desktops. The problem with that is, for applications like Chrome or Firefox, the new process is immediately closed if an existing process already exists, so it doesn't move the new windows (which actually belong to another, older process) to another desktop.

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)

这篇关于python,Windows 10:在特定的虚拟桌面环境(工作区)上启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆