2016-08-18 133 views
0

如果可以使用它的名稱來ping我們的服務器,並且使用「https://」(如果它不在場),那麼將一個應用程序放在使用「smb://」掛載驅動器的automator中。忽略Applescript中的shell腳本錯誤

我們現在正在解決這個問題,除非通過smb連接,否則Mac無法打開pdf,docs等。所以直到我們知道這只是一個繃帶。 不想僅將兩個連接添加到登錄項目中的驅動器,因爲用戶會看到錯誤。

問題與此代碼是,如果MAC不能ping我們的服務器,劇本只是停止並拋出一個錯誤,

行動「運行AppleScript」遇到一個錯誤。 ping:無法解析cvm62v:未知主機

我弄錯了「出錯」等,但我對此知之甚少,所以我不知道什麼是錯的。

-- (0) Check to see if there server exists by pinging it 
 
set max_retry to 60 
 
set k to 0 
 
repeat while (do shell script "ping -c 1 cvm62v") contains "100% packet loss" 
 
\t delay 5 
 
\t set k to k + 1 
 
\t -- (2) It doesn't exist, mount the https volume 
 
\t if k > max_retry then tell application "Finder" 
 
\t \t try 
 
\t \t \t mount volume "https://my.server.edu/shared/" 
 
\t \t \t delay 1 
 
\t \t end try 
 
\t end tell 
 
end repeat 
 
-- (2) It exists, mount the smb volume 
 
tell application "Finder" 
 
\t try 
 
\t \t mount volume "smb://cvm62v/shared/" 
 
\t \t delay 1 
 
\t end try 
 
end tell

回答

0

你必須把你做的shell腳本在try塊。基於您的代碼,我建議你使用一個子功能「ServConnected」波紋管:

-- how to call the sub-function in the main program : 
set mytest to ServConnected("cvm62v") -- = true if 0% loss, else = false 


on ServConnected(ServName) 
try 
    set R to do shell script "ping -c 1 " & ServName 
    set SOK to (R contains "0.0% packet loss") 
on error 
    set SOK to false 
end try 
return SOK 
end ServConnected 

在這種情況下,該函數返回true,如果0.0%的丟包。如果你的網絡不好,那麼ping會導致20.0%的數據包丟失?你認爲服務器連接與否?

另外請記住,您的服務器必須設置爲應答ping請求。有些服務器不會,即使它們在線。