问题描述
2003 年 2 月 27 日,Apple 员工 Christopher Nebel 表示他想理顺这个问题:
On 27 February 2003, Apple employee Christopher Nebel said he'd like to straighten out this problem as reported by Bill Cheeseman:
由于应用程序和应用程序的不同命名在某些情况下,我们最终不得不稍微写一些像这样令人困惑的脚本(如果我们将 Adobe Photoshop 7.0 重命名为Finder 中的Photoshop"):
tell application "Photoshop" to activate
tell application "System Events"
tell application process "Adobe Photoshop 7.0"
我只想说,2011 年 8 月它仍然是一个问题,我一直在遇到它,所以我希望 StackOverflow 上的好人可以帮助找到解决方法;提前致谢!
Suffice it to say, it's still a problem in August 2011, and I keep running into it, so I hope the good folks here at StackOverflow can help find a workaround; thanks in advance!
给定一个应用程序名称(即我可以指示activate
),我希望能够将该名称传递给一个子例程以找到相应的进程名称.相反,给定一个进程名称,我希望能够将其传递给一个子程序以找到相应的应用程序名称.
Given an application name (i.e. something I can instruct to activate
), I'd like to be able to pass that name to a subroutine to find the corresponding process name. Conversely, given a process name, I'd like to be able to pass it to a subroutine to find the corresponding application name.
有什么建议吗?
推荐答案
以下代码就够了.在某种程度上,它借鉴了 fireshadow52 的回答和 MacScripter.net 上的帖子.
The following code suffices. It draws, to some extent, upon fireshadow52's answer and upon a post at MacScripter.net.
on GetApplicationCorrespondingToProcess(process_name)
tell application "System Events"
set process_bid to get the bundle identifier of process process_name
set application_name to file of (application processes where bundle identifier is process_bid)
end tell
return application_name
end GetApplicationCorrespondingToProcess
on GetProcessCorrespondingToApplication(application_name)
tell application "System Events"
set application_id to (get the id of application "Adobe Acrobat Professional" as string)
set process_name to name of (application processes where bundle identifier is application_id)
end tell
return process_name
end GetProcessCorrespondingToApplication
-- Example usage:
display dialog (GetProcessCorrespondingToApplication("Adobe Acrobat Professional") as string)
display dialog (GetApplicationCorrespondingToProcess("Acrobat") as string)
这篇关于使用 Applescript 从应用程序名称获取进程名称,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!