AppleScript处理程序中的可选参数 [英] Optional parameters in AppleScript handlers

查看:95
本文介绍了AppleScript处理程序中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Applescript文档说从优胜美地开始,处理程序的参数可以设为可选.

The Applescript documentation says that as of Yosemite, parameters for handlers can be made optional.

从参数规格"部分:

标签参数可以通过在形式参数名称后加上:literal来声明为默认值.这样做会使参数在被调用时是可选的.例如,这将为make处理程序声明with数据参数的默认值:

Labeled parameters may be declared with a default value by following the formal parameter name with :literal. Doing so makes the parameter optional when called. For example, this declares a make handler with a default value for the with data parameter:

on make new theClass with data theData : missing value

现在可以在不提供with数据参数的情况下调用此处理程序;处理程序将看到Data设置为指定的默认缺失值,然后可以对其进行测试和适当处理.

This handler can now be called without supplying a with data parameter; the handler would see theData set to the specified default missing value, which it could then test for and handle appropriately.

因此,由于需要带有可选参数的处理程序,我尝试创建一个.我已经走了这么远:

So, being in need of a handler with optional parameters, I tried to create one. I have got this far:

set theResult to Create of me given the string:"stuff", info:"this"

on Create given info:missing value, thestring:"stuff"
    if info is missing value then
        set present to false
    else
        set present to true
    end if
    return {present, thestring}
end Create

它可以编译,但是给我错误未定义变量thestring."

which compiles, but gives me the error 'The variable thestring is not defined.'

如果仅使用一个参数调用它:

If I call it with only one parameter:

set theResult to Create of me given thestring:"stuff"

我收到错误消息:创建时缺少info参数."也就是说,该参数毕竟不是可选的.

I receive the error: 'The info parameter is missing for Create.' i.e. the parameter isn't optional after all.

如何在Applescript处理程序中使用可选参数?

How can I get optional parameters working in Applescript handlers?

推荐答案

您必须为命令定义基于SDEF的术语,此命令才能正常工作(这反过来意味着与XML和脚本包混为一谈,并可能造成术语冲突,等等).本来可以使AppleScript库更友好地使用,但这实际上只是浪费每个人的时间.

You have to define SDEF-based terminology for your command for this to work (which in turn means mucking about with XML and script bundles and potentially creating terminology conflicts, and so on). It's supposed to make AppleScript libraries friendlier to use, but it's really just a waste of everyone's time.

使用普通的位置参数并让用户传递missing value作为可选"参数是最简单的,您的处理程序可以检查以下参数:

It's simplest just to use normal positional parameters and just have the user pass missing value for 'optional' arguments, which your handler can check for:

set theResult to Create("stuff", "this")
set theResult to Create("stuff", missing value)
set theResult to Create(missing value, missing value)

on Create(thestring, info)
    if thestring is missing value then set thestring to "stuff"
    set present to info is not missing value
    return {present, thestring}
end Create

或者让您的处理程序将一条记录作为其参数,然后将其与默认属性的记录连接起来:

or else have your handler take a single record as its parameter then concatenate that with a record of default properties:

set theResult to Create for {thestring:"stuff", info:"this"}
set theResult to Create for {thestring:"stuff"}
set theResult to Create for {}

on Create for args
    set args to args & {info:missing value, thestring:"stuff"}
    set present to info is not missing value
    return {present, thestring of args}
end Create

这两种解决方案都不是理想的;但是AppleScript中的所有内容都不都是真的吗?

Neither solution is ideal; but isn't true of everything in AppleScript?

这篇关于AppleScript处理程序中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆