本文目录导读:

- Windows 系统(使用 PowerShell)
- Windows 批处理版本(更简单)
- Linux 系统(使用 xdotool)
- macOS 系统(使用 AppleScript)
- 自动化窗口管理工具推荐
- 使用说明
我来给你写一个自动排列窗口的脚本,以下是适用于不同系统的方案:
Windows 系统(使用 PowerShell)
# 自动排列窗口脚本 - Windows PowerShell
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
# 获取屏幕尺寸
Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$screenWidth = $screen.Width
$screenHeight = $screen.Height
# 定义排列布局(可以根据需要修改)
$layout = @(
@{Name="左上"; X=0; Y=0; W=$screenWidth/2; H=$screenHeight/2},
@{Name="右上"; X=$screenWidth/2; Y=0; W=$screenWidth/2; H=$screenHeight/2},
@{Name="左下"; X=0; Y=$screenHeight/2; W=$screenWidth/2; H=$screenHeight/2},
@{Name="右下"; X=$screenWidth/2; Y=$screenHeight/2; W=$screenWidth/2; H=$screenHeight/2}
)
# 获取所有可见窗口
Get-Process | Where-Object {$_.MainWindowHandle -ne [IntPtr]::Zero -and $_.MainWindowTitle -ne ""} | ForEach-Object {
$proc = $_
$index = 0
# 为每个窗口分配位置
if ($index -lt $layout.Count) {
$pos = $layout[$index]
[Win32]::MoveWindow($proc.MainWindowHandle, $pos.X, $pos.Y, $pos.W, $pos.H, $true)
$index++
}
}
Windows 批处理版本(更简单)
@echo off :: 简单的窗口排列脚本 :: 需要安装 NirCmd 工具 :: 下载 NirCmd: https://www.nirsoft.net/utils/nircmd.html :: 将窗口排列为 2x2 网格 nircmd win move title "记事本" 0 0 800 600 nircmd win move title "计算器" 800 0 800 600 nircmd win move title "画图" 0 600 800 600 nircmd win move title "浏览器" 800 600 800 600
Linux 系统(使用 xdotool)
#!/bin/bash
# 自动排列窗口脚本 - Linux
# 获取屏幕分辨率
SCREEN_W=$(xdpyinfo | grep dimensions | awk '{print $2}' | cut -d 'x' -f1)
SCREEN_H=$(xdpyinfo | grep dimensions | awk '{print $2}' | cut -d 'x' -f2)
# 获取窗口列表
windows=$(xdotool search --onlyvisible --name "" 2>/dev/null)
# 获取窗口数量
count=$(echo "$windows" | wc -l)
# 计算布局(这里是2列布局示例)
cols=2
rows=$(( (count + cols - 1) / cols ))
cell_w=$((SCREEN_W / cols))
cell_h=$((SCREEN_H / rows))
index=0
for win in $windows; do
col=$((index % cols))
row=$((index / cols))
x=$((col * cell_w))
y=$((row * cell_h))
xdotool windowsize $win $cell_w $cell_h
xdotool windowmove $win $x $y
index=$((index + 1))
done
macOS 系统(使用 AppleScript)
-- 自动排列窗口脚本 - macOS
-- 使用 AppleScript 排列窗口
tell application "System Events"
set screenSize to size of window 1 of (first process whose frontmost is true)
set screenW to item 1 of screenSize
set screenH to item 2 of screenSize
end tell
tell application "Finder"
set desktopBounds to bounds of window of desktop
set screenW to item 3 of desktopBounds - item 1 of desktopBounds
set screenH to item 4 of desktopBounds - item 2 of desktopBounds
end tell
-- 排列窗口为2x2网格
tell application "System Events"
set processList to name of every process whose background only is false
set index to 0
repeat with procName in processList
try
tell process procName
set windowList to every window
repeat with w in windowList
set position of w to {((index mod 2) * (screenW / 2)), ((index div 2) * (screenH / 2))}
set size of w to {(screenW / 2), (screenH / 2)}
set index to index + 1
end repeat
end tell
end try
end repeat
end tell
自动化窗口管理工具推荐
Windows: PowerToys FancyZones
- 强大的窗口布局管理器
- 自定义布局模板
Linux: KDE/KWin 脚本
- 内置窗口管理器脚本支持
跨平台: AutoHotkey (Windows)
; AutoHotkey 版本
#Persistent
#SingleInstance, Force
; 快捷键: Win+1 = 2x2排列
#1::
{
WinGet, windows, List
screenW := A_ScreenWidth
screenH := A_ScreenHeight
Loop, %windows%
{
WinGetTitle, title, % "ahk_id " windows%A_Index%
winID := windows%A_Index%
if (Mod(A_Index, 2) = 1) {
x := 0
} else {
x := screenW/2
}
if (A_Index <= 2) {
y := 0
} else {
y := screenH/2
}
WinMove, ahk_id %winID%, , x, y, screenW/2, screenH/2
}
}
return
使用说明
- 选择适合你的系统的脚本
- 修改布局参数(窗口数量、排列方式)
- 测试运行,可能需要安装依赖工具
- 设置快捷键或开机自启动
需要我帮你调整特定布局或针对某个系统的详细配置吗?