如何写一个脚本自动打开组合窗口

wen 实用脚本 2

本文目录导读:

如何写一个脚本自动打开组合窗口

  1. Windows 环境(Python + pyautogui)
  2. 浏览器自动打开多个标签页
  3. 批处理脚本 (Windows .bat)
  4. macOS 环境(AppleScript)
  5. 跨平台 Python 脚本(推荐)
  6. 高级:自动排列窗口布局
  7. 安装依赖
  8. 使用建议

我来帮您写一个自动打开组合窗口的脚本,根据不同的操作系统和环境,有几种常见方式:

Windows 环境(Python + pyautogui)

import pyautogui
import time
import subprocess
# 方法1:使用热键组合(Win + E 打开文件资源管理器)
pyautogui.hotkey('win', 'e')
time.sleep(2)
# 方法2:打开特定窗口组合
def open_combination_windows():
    # 打开记事本
    subprocess.Popen('notepad.exe')
    time.sleep(1)
    # 打开计算器
    subprocess.Popen('calc.exe')
    time.sleep(1)
    # 打开画图
    subprocess.Popen('mspaint.exe')
    time.sleep(1)
    # 排列窗口(显示并排)
    pyautogui.hotkey('win', 'left')  # 将当前窗口置左
    time.sleep(0.5)
    pyautogui.hotkey('win', 'right')  # 将下一个窗口置右
open_combination_windows()

浏览器自动打开多个标签页

import webbrowser
import time
def open_browser_tabs():
    # 打开多个标签页
    urls = [
        'https://www.google.com',
        'https://www.github.com',
        'https://www.stackoverflow.com'
    ]
    for i, url in enumerate(urls):
        if i == 0:
            # 第一个打开新窗口
            webbrowser.open_new(url)
        else:
            # 后续在新标签页打开
            webbrowser.open_new_tab(url)
        time.sleep(1)
open_browser_tabs()

批处理脚本 (Windows .bat)

@echo off
echo 正在打开组合窗口...
:: 同时打开多个程序
start notepad.exe
start calc.exe
start mspaint.exe
:: 等待程序加载
timeout /t 2 /nobreak >nul
:: 使用快捷键排列窗口(需要焦点在特定窗口)
echo 窗口已打开,请手动排列或使用快捷键

macOS 环境(AppleScript)

tell application "System Events"
    -- 打开多个应用
    tell application "TextEdit" to activate
    delay 1
    tell application "Calculator" to activate
    delay 1
    tell application "Notes" to activate
    delay 1
end tell
-- 使用Mission Control组合窗口
tell application "Mission Control" to activate

跨平台 Python 脚本(推荐)

import os
import sys
import platform
import time
def open_windows_combination():
    """Windows系统"""
    import subprocess
    apps = ['notepad.exe', 'calc.exe', 'mspaint.exe']
    for app in apps:
        subprocess.Popen(app)
        time.sleep(1)
def open_macos_combination():
    """macOS系统"""
    apps = ['TextEdit', 'Calculator', 'Notes']
    for app in apps:
        os.system(f'open -a "{app}"')
        time.sleep(1)
def open_linux_combination():
    """Linux系统"""
    apps = ['gedit', 'gnome-calculator', 'eog']
    for app in apps:
        os.system(f'{app} &')
        time.sleep(1)
# 自动识别系统
system = platform.system()
if system == 'Windows':
    open_windows_combination()
elif system == 'Darwin':  # macOS
    open_macos_combination()
else:
    open_linux_combination()

高级:自动排列窗口布局

import pygetwindow as gw
import pyautogui
import time
def arrange_windows():
    """自动排列打开的窗口"""
    # 获取所有窗口
    windows = gw.getWindowsWithTitle('')
    # 筛选出非空窗口
    active_windows = [w for w in windows if w.title and w.isVisible]
    if len(active_windows) >= 2:
        # 获取屏幕尺寸
        screen_width, screen_height = pyautogui.size()
        # 将前两个窗口左右排列
        win1 = active_windows[0]
        win2 = active_windows[1]
        # 左窗口
        win1.moveTo(0, 0)
        win1.resizeTo(screen_width//2, screen_height)
        # 右窗口
        win2.moveTo(screen_width//2, 0)
        win2.resizeTo(screen_width//2, screen_height)
# 使用示例
open_windows_combination()
time.sleep(2)  # 等待程序打开
arrange_windows()

安装依赖

根据选择的脚本,安装相应的依赖:

# Python依赖
pip install pyautogui pygetwindow
# Windows还需要
pip install pywin32

使用建议

  1. Windows用户:推荐使用批处理脚本(.bat)或Python脚本
  2. macOS用户:推荐使用AppleScript或Python脚本
  3. 跨平台需求:使用Python跨平台版本

您需要哪种具体的组合窗口效果?我可以帮您定制更精确的脚本。

抱歉,评论功能暂时关闭!