2011-08-03 75 views
3

月27 2003年2月,苹果公司的员工克里斯托弗·内贝尔说,他想理顺this problem报道由比尔·奇斯曼:由于不同的命名的获取进程名称,使用AppleScript

在某些情况下,应用程序和应用 过程中,我们最终不得不写咯 混乱的脚本是这样的(如果我们已经更名为Adobe公司的Photoshop 7.0 “的Photoshop”在Finder):

tell application "Photoshop" to activate 
tell application "System Events" 
tell application process "Adobe Photoshop 7.0" 

只要说明一下,它在2011年8月仍然是一个问题,并且我一直在考虑这个问题,所以我希望StackOverflow中的优秀人员可以帮助您找到解决方法;提前致谢!

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

有什么建议吗?

+0

一种选择,如果给应用程序的名称,将启动应用程序,然后拿到最前面的进程的进程名,但是这似乎有点不可靠:俯卧如果应用程序在运行失败背景或忙碌或诸如此类。 – sampablokuper

+0

I.e.按照http://stackoverflow.com/questions/5913738/applescript-hide-get-process-name-from-app – sampablokuper

回答

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
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

更新: A process是指已经打开的应用程序。

+0

没有快乐,我很抱歉地说。将两个子例程放在脚本的开头,后面跟着'get_application_name from process'Acrobat“'给出一条语法错误消息。将“我的get_application_name从进程中”Acrobat“'放入一个tell块中导致Applescript Editor将其更改为'get_application_name of me from process”Acrobat“',执行时会出现Applescript错误消息:'无法创建应用程序进程”Acrobat “转化为整型。”将“get_application_name from process”放在“Acrobat”中(实验性)也会产生Applescript错误消息。 – sampablokuper

+0

我会试着将两个子程序调用改为normail类型(即'get_application_name(“Acrobat”)');花哨的电话并不是真的有必要。 – fireshadow52

+0

我同意不需要花哨的电话。仍然没有喜悦,但我认为我可以调整它,直到它工作。尽管感谢您的帮助。 – sampablokuper

0
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 ...".