2017-09-13 62 views
3

可以在Mac上運行多個實例或同一應用程序的副本。但AppleScript似乎無法單獨識別它們。假設我的應用程序是「FileMaker Pro」,並且我有多個副本正在運行。讓AppleScript告訴「FileMaker Pro」退出後,我相信會退出第一個運行的,這可能不是我希望它退出的那個。AppleScript,按ID退出應用程序,安全

我想編寫一個腳本,首先識別最前面的應用程序,然後關閉並執行其他一些操作(可能會將其他應用程序放在前面),然後安全地退出它在開始時標識的原始應用程序。

一些谷歌搜索我所做的發現建議,在那裏我通過進程ID標識最前面的應用程序,然後

做shell腳本「殺......」

但是從我可以告訴「殺」不要求保存更改等(所以它不會安全退出「)。

我想這個腳本來做什麼AppleScript退出命令或手動選擇退出文件菜單會做,包括要求保存更改或w討厭的人。

這可能嗎?如果是這樣如何?

+0

即使您嘗試使用unix ID而不是名稱,Applescript也會始終處理您應用程序的最後打開的實例。解決方法是在應用程序中更改名稱以使其成爲FM1,FM2 ...,但它肯定會產生其他問題,特別是對於像FileMaker這樣的標準應用程序。然後要退出Filemaker的第一個實例,必須首先正確退出第一個之後打開的所有其他實例。要退出,請使用{command down}中的按鍵q來告訴「系統事件」區塊。 – pbell

回答

2

對於應用程序的副本:可以使用應用程序的路徑而不是名稱。

tell application "System Events" 
    tell (first application process whose frontmost is true) 
     set x to its application file -- get the path of this application (the result is an alias of "System Events") 
    end tell 
    set thisAppPath to path of x -- get the path (a string) 
end tell 

--- *** go off and do some other stuff **** 
--- 


--- SAFELY quit the original frontmost application that it identified at the start. 
tell application thisAppPath -- the path must be a string 
    try -- *** use 0 to no limit *** 
     with timeout of 240 seconds -- a time limit is given to the user to save changes, etc. 
      quit 
     end timeout 
    on error -- (timeout error): the user do nothing because the application is still open 
     -- do something 
    end try 
end tell 
-- 
-- script after the application quits 
-- 
+0

任何不使用將this應用程序文件設置爲字符串而不是將set x設置爲其應用程序文件並將set thisAppPath設置爲x的路徑的原因? – user3439894

+0

@ user3439894因爲我收到此錯誤:**錯誤「無法將應用程序\」System Events \「的別名\」Macintosh HD:應用程序:Safari.app:\「轉換爲類型字符串。」編號-1700從別名「Macintosh HD:應用程序:Safari.app:」到字符串**。 區別在於它不是AppleScript的別名,而是「系統事件」的別名。如果你喜歡單行,有可能:'將thisAppPath設置爲(其應用程序文件)的路徑' – jackjr300

+0

是的,我建議在OS X 10.8.5下正常工作,這是我通常運行的,但是在macOS下10.12.5它不起作用。你的'設置thisAppPath路徑(它的應用程序文件)'在macOS 10.12.5下工作。 +1,以任何一種方式獲得好的答案! :) – user3439894