如何写一个脚本快速切换应用窗口

wen 实用脚本 1

本文目录导读:

如何写一个脚本快速切换应用窗口

  1. Windows - AutoHotkey 脚本
  2. macOS - AppleScript
  3. Linux - Bash 脚本
  4. 跨平台 Python 脚本
  5. 使用技巧

Windows - AutoHotkey 脚本

; 快速切换应用窗口 - 按 Alt+Tab 的增强版
; 切换到上一个应用窗口
#1::  ; Win + 1
    Send, !{Tab}
Return
; 切换到指定应用(以Chrome为例)
#c::  ; Win + C 切换到Chrome
    WinActivate, ahk_exe chrome.exe
Return
; 快速在最近两个窗口间切换
#z::  ; Win + Z
    Send, {Alt Down}{Tab}{Alt Up}
Return
; 循环切换同类型窗口(如多个Chrome标签)
#Tab::
    WinGetClass, CurrentClass, A
    WinSet, Bottom,, A
    WinActivate, ahk_class %CurrentClass%
Return

macOS - AppleScript

-- 快速切换应用
-- 保存为 .applescript 文件
-- 切换到特定应用
tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
    if frontApp is not "Safari" then
        activate application "Safari"
    end if
end tell
-- 循环切换最近使用应用(按Cmd+Tab的效果)
tell application "System Events"
    key code 48 using command down  -- Tab键
end tell
-- 快速切换到下一个窗口
tell application "System Events"
    key code 49 using {command down, shift down}  -- ~键
end tell

Linux - Bash 脚本

#!/bin/bash
# 保存为 switch_window.sh
# 切换到指定应用窗口
switch_to_app() {
    local app_name="$1"
    wmctrl -xa "$app_name" || wmctrl -a "$app_name"
}
# 切换到下一个窗口
switch_next_window() {
    wmctrl -l | awk '{print $1}' | while read id; do
        wmctrl -i -a "$id"
    done
}
# 显示所有窗口并选择
show_window_menu() {
    wmctrl -l | fzf | awk '{print $1}' | xargs wmctrl -i -a
}
# 使用示例
case "$1" in
    "chrome") switch_to_app "chrome" ;;
    "terminal") switch_to_app "terminal" ;;
    "menu") show_window_menu ;;
    *) switch_next_window ;;
esac

跨平台 Python 脚本

#!/usr/bin/env python3
# 需要安装 pyautogui: pip install pyautogui
import pyautogui
import time
import sys
def switch_window(method='alt_tab'):
    """快速切换窗口"""
    if method == 'alt_tab':
        # Windows/Linux Alt+Tab
        pyautogui.keyDown('alt')
        pyautogui.press('tab')
        pyautogui.keyUp('alt')
    elif method == 'cmd_tab':
        # macOS Cmd+Tab
        pyautogui.keyDown('command')
        pyautogui.press('tab')
        pyautogui.keyUp('command')
    elif method == 'specific':
        # 切换到指定窗口(需要窗口标题)
        windows = pyautogui.getWindowsWithTitle(sys.argv[2])
        if windows:
            windows[0].activate()
if __name__ == "__main__":
    method = sys.argv[1] if len(sys.argv) > 1 else 'alt_tab'
    switch_window(method)

使用技巧

推荐的热键方案:

Windows:    Alt + ` (波浪号) - 同程序窗口切换
           Win + 数字   - 切换到任务栏第N个程序
macOS:      Cmd + `    - 同程序窗口切换
           Cmd + Tab   - 程序间切换
Linux:      Alt + `    - 同程序窗口切换
           Super + Tab - 程序间切换

快速启动方法:

  1. Windows: 将AHK脚本编译为exe,设为开机启动
  2. macOS: 将脚本保存为Applescript应用,拖到Dock
  3. Linux: 设置快捷键绑定到脚本

增强功能建议:

# 智能切换脚本示例
import pygetwindow as gw
import win32gui  # Windows only
def smart_switch():
    """智能切换:记住最近使用的窗口"""
    windows = gw.getWindowsWithTitle('')
    if len(windows) > 1:
        # 切换到非当前窗口
        current = gw.getActiveWindow()
        for win in windows:
            if win.title != current.title:
                win.activate()
                break

这些脚本可以根据你的需求定制,比如增加快捷键绑定、窗口记忆功能等,需要什么特定功能的脚本可以告诉我!

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