2003年2月27日,苹果公司员工克里斯托弗·内贝尔说他想按照比尔·凯奇曼的报告理顺this problem:
由于应用程序和应用程序的命名不同 在某些情况下,我们最终不得不写一点 混淆这样的脚本(如果我们将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的优秀人员可以帮助找到解决方法;提前谢谢!
给定一个应用程序名称(即我可以指示activate
),我希望能够将该名称传递给子程序以查找相应的进程名称。相反,给定一个进程名称,我希望能够将它传递给子程序以找到相应的应用程序名称。
有什么建议吗?
答案 0 :(得分:2)
on get_application_name(this_process)
tell application "System Events" to set the BID to (get the id of application process this_process)
tell application "Finder" to return the name of every item of (path to applications folder) whose id is BID and kind is "Application"
end get_application_name
-----------------------------------------------------------------------------------------------------------------------------------------
on get_process_name(this_application)
tell application "Finder" to set the BID to (get the id of application this_application)
tell application "System Events"
set open_applications to (get id of every application process) as list
return every item of open_applications whose id is BID
end tell
end get_process_name
这两个子程序都是未经测试的,所以他们可能不会做他们应该做的事情。 :S
更新:process
是指已经打开的应用。
答案 1 :(得分:1)
以下代码就足够了。它在某种程度上取决于fireshadow52的回答和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)
答案 2 :(得分:1)
I find this works very well:
on GetApplicationCorrespondingToProcess(process_name)
tell application "System Events"
set application_file to file of (application processes where name is process_name)
end tell
return application_file
end GetApplicationCorrespondingToProcess
on GetProcessCorrespondingToApplication(application_name)
tell application "System Events"
set process_name to name of my application application_name
end tell
return process_name
end GetProcessCorrespondingToApplication
-- Example usage:
set myprocess to GetProcessCorrespondingToApplication("Terminal") as string
set myfile to GetApplicationCorrespondingToProcess(myprocess) as string
set mypath to the POSIX path of myfile -- create this just to compare to myfile
set myapp to do shell script "myval='" & myfile & "' ; echo ${myval%.app:} | awk -F':' '{print ($NF)}'"
log myprocess
log myfile
log mypath
log myapp
-- A process appears to be the name of the MacOS executable within the application.
-- Replace "Terminal" by "Firefox" to see the distinction.
-- Also, you could substitute mypath for myfile and / for : in "set myapp ...".