如何使用给定的进程 ID 激活 Mac OS X 应用程序?
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想/无法安装任何其他软件,可以使用一种内置方法来查找进程 ID 和应用程序:ps。
ps 是一个有用的命令行工具,用于查找有关正在运行的进程的信息。要查找给定进程号(我已将其分配给变量 myProcessId)的特定应用程序:
这将返回这样的结果,
以将结果限制为仅相关行,将其通过管道传递给 grep,如下所示
通过解析答案,您可以找到应用程序的名称。这可能有点棘手,因为结果将显示用于应用程序的实际命令,而不是应用程序名称(如果您查看示例,您会发现可以通过在结果中查找 some.app 来找到它) 。
编辑 - 抱歉,我误解了这个问题。
您可以使用系统事件来完成此操作(事实证明这比在 shell 中闲逛要容易得多):
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):
this will return a result like this
to restrict the result to just the relevant line, pipe it to grep like so
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):
@stib 的答案有效,但可以简化:
鉴于根据定义只有一个进程可以匹配- PID(进程 ID)唯一标识单个进程 - 不需要循环:只需直接定位第一个 - 并且根据定义仅 - 元素< /strong> 过滤器返回的 PID 列表
unix id 为 ...
的进程:ehime 贡献了以下 bash 函数包装器:
示例调用(假设在当前 shell 中定义或获取了 activateByPid):
@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 ...
:ehime contributed the following
bash
function wrapper:Sample call (assumes that
activateByPid
was defined or sourced in the current shell):这是我的解决方案,使用 python 和 applescript:
appscript.app(pid=).activate()
就是这样!
Here is my solution, with python and applescript:
appscript.app(pid=<yourpid>).activate()
That's it!