私は bash と Applescript の知識を広げるために基本的な pingtest を作成しているので、これを作成しました。
on run argv
try
if (count argv) is less than 1 or (count argv) is greater than 1 then
set IP_address to ""
set dialog_1 to display dialog "Enter IP Address:" default answer "" with title "Ping"
set the IP_address to the text returned of dialog_1
try
set ping to (do shell script "ping -c 2 " & IP_address)
display dialog "Connection Successful." buttons {"OK"} default button 1
return 1
on error
--if we get here, the ping failed
display dialog "Conection failed. Host is down" buttons {"Darn"} default button 1
return 0
end try
end if
if (count argv) = 1 then
set IP_address to item 1 of argv
try
set ping to (do shell script "ping -c 2 " & IP_address)
return 1
on error
-- if we get here, the ping failed
return 0
end try
end if
on error error_message number error_number
if (error_number) is not equal to -128 then
display alert ("An error has occured!") message error_message & (" Error number ") & error_number & "."
end if
return error_number
end try
エンドラン
(コードスニペット内でエンドランを取得できませんでした)
そして、bashでこれを作成しました:
!/bin/bash currentDir= pwd
Success= $currentDir/$1/Contents/MacOS/applet $2
echo $Success
(コードスニペット内でそれを取得できませんでした)
AppleScript だけを実行すると、エディターで正常に動作しますが、.app および bash バージョンでは -1708 エラーが発生します。問題を次のように絞り込みました。if (count argv) is less than 1 or (count argv) is greater than 1 then
これを修正するには、どうすればよいでしょうか。あるいは、コードをよりきれいにするためにはどうすればよいでしょうか?
ありがとう
EDIT(1): 引数が指定されていない場合はプログラムで GUI バージョンを実行し、引数が渡された場合はバックグラウンドで実行するようにします。