You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CLI根据PID切换应用程序焦点?(含Firefox多配置场景)

嘿,这个场景我太熟悉了——多开Firefox配置文件的时候,找对应窗口真的头疼!刚好我折腾过用命令行根据PID切换窗口焦点的方法,不同操作系统的方案不一样,给你梳理清楚:

Windows系统

如果你用Windows,有两种靠谱的方法:

  • PowerShell原生方法(无需额外工具):
    直接在PowerShell里运行这段命令,把1234换成你的目标PID就行:
    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class User32 {
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
    "@
    (Get-Process -Id 1234).MainWindowHandle | ForEach-Object { [User32]::SetForegroundWindow($_) }
    
    原理是调用Windows系统API,直接把目标进程的主窗口拉到前台。
  • 用nircmd工具(更简洁):
    先下载nircmd(微软推荐的轻量命令行工具),然后在命令行里直接跑:
    nircmd win activate processid 1234
    
    这个工具还能做很多窗口操作,适合经常折腾的人。
Linux系统

Linux下主要靠窗口管理工具,推荐两个常用的:

  • xdotool(通用度高):
    先安装xdotool(比如Ubuntu用sudo apt install xdotool),然后执行:
    xdotool windowactivate $(xdotool search --pid 1234)
    
    如果目标进程有多个窗口,可以加--onlyvisible参数只找可见窗口:
    xdotool windowactivate $(xdotool search --onlyvisible --pid 1234)
    
  • wmctrl(适合传统桌面):
    安装wmctrl后,用下面的命令:
    wmctrl -i -a $(wmctrl -lp | grep 1234 | awk '{print $1}')
    
    这里wmctrl -lp会列出所有窗口及其关联的PID,grep找到目标PID后,awk提取窗口ID,最后用wmctrl -i -a激活窗口。
macOS系统

macOS自带的osascript就能搞定,不需要额外装工具:

osascript -e 'tell application "System Events" to set frontmost of process id 1234 to true'

这条命令直接告诉系统把指定PID的进程设为前台,对多配置Firefox特别管用——因为每个Firefox配置文件都是独立进程,PID对应准确的那个实例。

小提醒

切换前最好先确认PID对应的进程没错:

  • Windows:tasklist /fi "PID eq 1234"
  • Linux:ps aux | grep 1234
  • macOS:ps -p 1234

这样就不会不小心切错窗口啦!

内容的提问来源于stack exchange,提问作者kenorb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:54:30