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

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

问题描述

Applescript 文档 说从 Yosemite 开始,处理程序的参数可以是可选的.

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

来自参数规范"部分:

带标签的参数可以通过在形式参数名称后面加上:literal 来声明一个默认值.这样做会使参数在调用时可选.例如,这为 with data 参数声明了一个具有默认值的 make 处理程序:

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:

使用数据创建新的类 theData : 缺失值

现在可以在不提供带数据参数的情况下调用此处理程序;处理程序会看到数据设置为指定的默认缺失值,然后它可以测试并适当处理.

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"

我收到错误消息:Create 缺少信息参数."即参数毕竟不是可选的.

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.

最简单的方法是使用普通的位置参数并且让用户为可选"参数传递缺失值,您的处理程序可以检查这些参数:

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天全站免登陆