1

我正在做一个基本的 pingtest 来扩展我对 bash 和 Applescript 的了解,所以我做了这个:

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

我能做些什么来解决这个问题,或者甚至只是让我的代码更整洁?

谢谢

编辑(1):如果我的程序没有提供任何参数,我希望我的程序运行一个 GUI 版本,并且当它被传递参数时,我希望它在后台运行。

4

1 回答 1

0

您可以像这样直接从 bash 将参数传递给 AppleScript:

--AppleScript named myScript.scpt
    on run {argv}
        beep argv
    end run 

命令:

osascript -e 'run script "/Users/user1781742/Desktop/myScipt.scpt" with parameters {2}'
于 2012-10-29T04:50:34.013 回答