2011-08-03 108 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 ...".