2013-07-12 20 views
10

我嘗試做什麼:如何獲得最前端的應用程序的名稱使用AppleScript,並用它來獲取文件路徑

當我在我的文字編輯器之一(文字編輯,笑談,FoldingText)是我想這個AppleScript來顯示文件路徑。

我想要最前面的窗口應用程序讓我的應用程序名稱很好,很容易,然後我可以在下一步要求POSIX路徑。

問題:

劇本已經99%,但我失去了一些東西。當我嘗試使用的activeApp變量它不工作,我得到這個錯誤:

Error Number:System Events got an error: Can’t get application {"TextEdit"}. 
-1728 

這裏的腳本:

tell application "System Events" 
    set activeApp to name of application processes whose frontmost is true 

    --This doesn't work either: 
    --do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'" 

    tell application activeApp 
     set myPath to POSIX path of (get file of front document) 
    end tell 
    display dialog myPath 
end tell 

如果我交流activeApp"TextEdit"一切正常。幫助將不勝感激。

也許有什麼東西在這裏,可以幫助:Get process name from application name and vice versa, using Applescript

回答

7

要麼得到一個文件的path屬性或使用系統事件得到value of attribute "AXDocument"

try 
    tell application (path to frontmost application as text) 
     (path of document 1) as text 
    end tell 
on error 
    try 
     tell application "System Events" to tell (process 1 where frontmost is true) 
      value of attribute "AXDocument" of window 1 
     end tell 
     do shell script "x=" & quoted form of result & " 
     x=${x/#file:\\/\\/} 
     x=${x/#localhost} # 10.8 and earlier 
     printf ${x//%/\\\\x}" 
    end try 
end try 

第一種方法沒有用預覽,TextMate的2,崇高的文本,或ICHM,第二種方法工作沒有與Acorn合作。第二種方法需要訪問要啓用的輔助設備。

+0

嘗試部分太棒了。非常靈活,並且使得所有不同的應用程序輸出正確的路徑(只要我測試了這個,就立即停止寫入IF條件)的手工工作。 Apropos,「如果」我可以得到這個與nvALT工作,那麼我甚至比我想象的還多。 – pattulus

+0

你可以用'tell application'System Events'來告訴(處理1最前面是真的)如果name是「nvALT」那麼((系統屬性「HOME」)&「/ Notes /」)和文本字段的值窗口1的工具欄1的組1中的「1」和「.txt」)'。 – user495470

+1

這很好。謝謝。但是當我運行這個腳本時,它將刪除路徑開頭的文本,所以'/ users/username/path/to/file'就成爲'sername/path/to/file'。任何想法爲什麼? – drstevok

1

您所要求的...

set activeApp to name of application processes whose frontmost is true 

通知「過程」,這是複數意義可以響應這樣的AppleScript得到幾個進程給你一個名字清單。即使只有一個應用程序返回,它仍然是列表格式。另請參閱您的錯誤包含{「TextEdit」}。名稱周圍的括號表示它是一個列表,所以錯誤向您顯示了問題。

您無法將名稱列表傳遞給下一行代碼。因此,您有幾個選擇。 1)你可以只需要一個進程而不是所有進程。這將返回一個字符串而不是一個列表。試試這個代碼...

set activeApp to name of first application process whose frontmost is true 

2)你可以使用列表中的「第1項」來處理列表。試試這個代碼...

set activeApps to name of application processes whose frontmost is true 
set activeApp to item 1 of activeApps 

最後,你不應該告訴系統事件告訴應用程序。分開這2個tell代碼塊。以下是我將如何編寫代碼的方法。

tell application "System Events" 
    set activeApp to name of first application process whose frontmost is true 
end tell 

try 
    tell application activeApp 
     set myPath to POSIX path of (get file of front document) 
    end tell 

    tell me 
     activate 
     display dialog myPath 
    end tell 
on error theError number errorNumber 
    tell me 
     activate 
     display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop 
    end tell 
end try 

我不能承諾「獲取前端文件的文件」代碼將起作用。這取決於應用程序。並非所有的應用程序都能理解該請求這就是我使用try塊的原因。無論如何,儘管您可以確定您正在處理正確的應用程序。祝你好運。

+0

感謝您糾正我的複雜過程...我沒有想到這一點。我仍然在學習基礎知識(昨天我正在研究AppleScript的列表,並在這裏使用大括號使我感到驚訝)。 //此外,我現在已經將這兩部分分開了,就像你所建議的那樣。我仍然沒有運氣。 //我複製了你的腳本,並在我的文本編輯器上試過,但仍然存在-1728問題。 – pattulus

+0

我更新了代碼...我忘了「第一個應用程序的名稱」,請重試。 – regulus6633

+0

我更新了我的描述,更好地解釋了我之後的情況。悲傷地嘗試「第一個名字」只是輸出錯誤的一個變種:「錯誤」AppleScript編輯器得到一個錯誤:無法獲取文檔1的文件。「文檔1的文件中編號-1728」 – pattulus

1

我一直在使用這個片段了一會,似乎所有的Cocoa程序(不知道X11)工作:

set front_app to (path to frontmost application as Unicode text) 

tell application front_app 
    -- Your code here 
end tell 
+0

感謝您的貢獻,但這隻能獲得應用程序,而不能獲得主動/最前面的文檔。 – pattulus

0

的這一切似乎並另存爲一個應用程序編譯的AppleScript工作並放在Dock上。無論何時運行應用程序,IT都是最前面的,而不是顯示其前窗的應用程序。該應用程序在我的Applescript運行時變爲不活動狀態。如何編寫一個Applescript應用程序,該應用程序在運行時處於非活動狀態?

我可能已經找到了上面列出的問題的解決方案。只要告訴用戶重新激活所需的應用程序,並給他們時間。

tell application "Finder" 
    activate 
    say "Click front window of your application" 
    delay 5 
    set myapp to get name of first application process whose frontmost is true 
-- etc. 
-- your code 
end tell 
相關問題