使用 Applescript 从应用程序名称获取进程名称,反之亦然

5 applescript

2003 年 2 月 27 日,Apple 员工 Christopher Nebel 表示他想解决Bill Cheeseman 报道的这个问题:

由于在某些情况下应用程序和应用程序进程的命名不同,我们最终不得不编写像这样稍微令人困惑的脚本(如果我们在 Finder 中将 Adob​​e Photoshop 7.0 重命名为“Photoshop”):

tell application "Photoshop" to activate
tell application "System Events"
tell application process "Adobe Photoshop 7.0"
Run Code Online (Sandbox Code Playgroud)

可以这么说,2011 年 8 月这仍然是一个问题,而且我一直遇到这个问题,所以我希望 StackOverflow 的好心人能够帮助找到解决方法;提前致谢!

给定一个应用程序名称(即我可以指示的名称activate),我希望能够将该名称传递给子例程以查找相应的进程名称。相反,给定一个进程名称,我希望能够将其传递给子例程以查找相应的应用程序名称。

有什么建议么?

小智 4

下面的代码就足够了。在某种程度上,它借鉴了 fireshadow52 的答案和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)
Run Code Online (Sandbox Code Playgroud)