applescript脚本从命令行运行,但在保存到应用程序时双击

时间:2015-08-08 23:21:51

标签: applescript

我尝试编写一个既可以作为应用程序运行的脚本,也可以使用osascript从命令行运行。在这两种情况下,我都希望弹出一个" help"对话。在应用程序的情况下,当我双击应用程序时它应该弹出。在命令行启动的情况下,我希望它在没有参数的情况下运行脚本时弹出(例如:osascript myScript.scpt)。当我双击应用程序时,附加的脚本会正确弹出对话框,但它可以从命令行运行。如果我只删除第一行上的 argv ,然后删除第二行上的 - ,从而模拟argv的存在,它可以正常工作,双击。也就是说,当我使用提供的 argv 时,行为完全不同于我不喜欢的行为。这是一个错误还是我做错了什么?

on run argv             -- if I remove the argv from this line
    --  set argv to []  -- and then comment *in* this line, it works fine
    getDefaults()       -- when I double-click the app

    if (count of argv) = 0 then 
        displayHelp()  -- doesn't display on double click when I use "on run argv"
    else
        processFromCommandLine(argv)
    end if
end run

on displayHelp()
     display dialog "Help!"
end displayHelp
on processFromCommandLine(argv)
end processFromCommandLine

2 个答案:

答案 0 :(得分:1)

双击时脚本应用程序正在退出,因为arg未分配给您可以计算的类,但是您尝试获取其计数。只需将其包装在try块中即可。

on run argv
    getDefaults()
    try
        get (count of argv)
        processFromCommandLine(argv)
    on error
        displayHelp()
    end try
end run

答案 1 :(得分:1)

几乎可以肯定这是一种更好的方法,但这有效:

on run argv

    -- Display help.
    if argv = current application or ¬
        argv's class ≠ list or argv's length = 0 then
        display dialog "Help!"
        return
    end if

    -- Do stuff.

end run

看起来direct parameter的{​​{1}}设置为...

相关问题