Applescript .app总是给出-1708

时间:2012-10-29 01:27:08

标签: bash applescript runtime-error

我正在进行基本的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中我做了这个:

!/斌/庆典 currentDir = pwd 成功= $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版本,当它传递参数时,我希望它在后台运行。

1 个答案:

答案 0 :(得分: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}'