如何使用给定的进程 ID 激活 Mac OS X 应用程序?

发布于 2024-08-22 18:09:12 字数 225 浏览 4 评论 0原文

我知道 Mac OS X 中应用程序的进程 ID。我如何切换到它(使用 applescript、Python 或其他)?

我所说的“切换”是指集中注意力。

通常的解决方案是使用 applescript 代码告诉应用程序“Foo”激活,但这里的名称没有用,因为我正在运行同一应用程序的许多实例。但是我能够获取应用程序的进程 ID。

如何以编程方式切换到该应用程序?

I know the process id of an application in Mac OS X. How can I switch to it (using applescript, or python, or whatever)?

By "switch", I mean, put in focus.

The usual solution is to use the applescript code tell application "Foo" activate, but here the name is not useful because I have many instances of the same application running. I am however able to get the process id of the application.

How can I switch to this application programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傾旎 2024-08-29 18:09:12

如果您不想/无法安装任何其他软件,可以使用一种内置方法来查找进程 ID 和应用程序:ps。

ps 是一个有用的命令行工具,用于查找有关正在运行的进程的信息。要查找给定进程号(我已将其分配给变量 myProcessId)的特定应用程序:

do shell script "ps -p " & myProcessId

这将返回这样的结果,

  PID TTY           TIME CMD
66766 ??         9:17.66 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_3793822

以将结果限制为仅相关行,将其通过管道传递给 grep,如下所示

do shell script "ps -p " & myProcessId & "|grep " & myProcessId

通过解析答案,您可以找到应用程序的名称。这可能有点棘手,因为结果将显示用于应用程序的实际命令,而不是应用程序名称(如果您查看示例,您会发现可以通过在结果中查找 some.app 来找到它) 。

编辑 - 抱歉,我误解了这个问题。

您可以使用系统事件来完成此操作(事实证明这比在 shell 中闲逛要容易得多):

tell application "System Events"
    set theprocs to every process whose unix id is myProcessId
    repeat with proc in theprocs
        set the frontmost of proc to true
    end repeat
end tell

If you don't want to /can't install any additional software, there is a built-in way of looking up process IDs and apps: ps.

ps is a useful command line tool to find info on running processes. To find a particular app given the process number (which I've assigned to a variable myProcessId):

do shell script "ps -p " & myProcessId

this will return a result like this

  PID TTY           TIME CMD
66766 ??         9:17.66 /Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_3793822

to restrict the result to just the relevant line, pipe it to grep like so

do shell script "ps -p " & myProcessId & "|grep " & myProcessId

By parsing the answer you can find the name of the app. This might be a little tricky because the result will show the actual command used for the app, not the app name (if you look at the example you'll see it is possible to find it by looking for something.app in the result).

Edit - sorry, I misunderstood the question.

You can do it with system events (turns out to be much easier than faffing around with the shell anyway):

tell application "System Events"
    set theprocs to every process whose unix id is myProcessId
    repeat with proc in theprocs
        set the frontmost of proc to true
    end repeat
end tell
素衣风尘叹 2024-08-29 18:09:12

@stib 的答案有效,但可以简化:

鉴于根据定义只有一个进程可以匹配- PID(进程 ID)唯一标识单个进程 - 不需要循环:只需直接定位第一个 - 并且根据定义 - 元素< /strong> 过滤器返回的 PID 列表 unix id 为 ... 的进程:

# Assumes that variable `myProcessId` contains the PID of interest.
tell application "System Events"
  set frontmost of the first process whose unix id is myProcessId to true
end tell

ehime 贡献了以下 bash 函数包装器:

# Pass the PID as the 1st (and only) argument.
activateByPid()
{
  osascript -e "
    tell application \"System Events\"
      set frontmost of the first process whose unix id is ${1} to true
    end tell
  "
}

示例调用(假设在当前 shell 中定义或获取了 activateByPid):

# Activate Safari by its PID.
activateByPid $(pgrep -x 'Safari')

@stib's answer works, but can be simplified:

Given that by definition only one process can match - PIDs (process IDs) uniquely identify a single process - there is no need for a loop: simply directly target the first - and by definition only - element of the list of PIDs returned by filter process whose unix id is ...:

# Assumes that variable `myProcessId` contains the PID of interest.
tell application "System Events"
  set frontmost of the first process whose unix id is myProcessId to true
end tell

ehime contributed the following bash function wrapper:

# Pass the PID as the 1st (and only) argument.
activateByPid()
{
  osascript -e "
    tell application \"System Events\"
      set frontmost of the first process whose unix id is ${1} to true
    end tell
  "
}

Sample call (assumes that activateByPid was defined or sourced in the current shell):

# Activate Safari by its PID.
activateByPid $(pgrep -x 'Safari')
四叶草在未来唯美盛开 2024-08-29 18:09:12

这是我的解决方案,使用 python 和 applescript:

  1. 安装 appscript
  2. 运行 appscript.app(pid=).activate()

就是这样!

Here is my solution, with python and applescript:

  1. Install appscript
  2. Run appscript.app(pid=<yourpid>).activate()

That's it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文