Cocoa 脚本:返回“null"与“缺失值"对比

编程入门 行业动态 更新时间:2024-10-24 00:32:29
本文介绍了Cocoa 脚本:返回“null"与“缺失值"对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

AppleScript 似乎知道特殊值 null.

It appears that AppleScript knows the special value null.

如何从基于 Cocoa Scripting 的应用程序中为可编写脚本的属性返回这样的值?

How do I return such a value from my Cocoa Scripting based app for a scriptable property?

如果我从基于 Cocoa Scripting 的应用程序返回 nil (NULL) 或 NSNull作为缺失值.

If I return nil (NULL) or NSNull for a scriptable property getter from my Cocoa Scripting-based app, the script editor interprets that as missing value.

如果我返回 [NSAppleEventDescriptor nullDescriptor],AppleScript 甚至会显示错误.

And if I return [NSAppleEventDescriptor nullDescriptor], AppleScript even shows an error.

推荐答案

我正在使用null"的 cMissingValue 版本,如下所示:

I'm using the cMissingValue version of "null", like so:

NSAppleEventDescriptor(typeCode: UInt32(cMissingValue))

我决定根据下面的测试使用它.

I decided to use that based on the testing below.

我使用 Swift 运行并将参数传递给 AppleScript 子例程,使用以下代码:

I'm using Swift to run and passing parameters to an AppleScript subroutine, using the code here:

将变量传递给 AppleScript

let parameters = NSAppleEventDescriptor.list()

let null         = NSAppleEventDescriptor.null()
let missingValue = NSAppleEventDescriptor(typeCode: UInt32(cMissingValue))

parameters.insert(null, at: 0)
parameters.insert(missingValue, at: 0)
return parameters

这些参数被传递给 AppleScript 子程序,我已经对每个结果进行了注释:

These parameters are passed to the AppleScript subroutine, and I've commented each results:

on nilTest(paramNull, paramMissingValue)
    display dialog paramNull buttons {"OK"} # execution error: Can’t make current application into type string. (-1700)
    display dialog paramMissingValue buttons {"OK"} #"msng"

    display dialog "" & paramNull buttons {"OK"} #"current application"
    display dialog "" & paramMissingValue buttons {"OK"} #"missing value"
end nilTest

NSAppleEventDescriptor.null() 版本似乎出于某种原因正在获取当前应用程序"值.

The NSAppleEventDescriptor.null() version seems to for some reason be getting the "current application" value.

似乎我可能想要使用 cMissingValue,如另一个答案中所示.

It seems like I may want to use the cMissingValue as shown in the other answer.

可以像这样在 AppleScript 中对变量进行 nil-check:

The variables can be nil-checked in AppleScript like so:

if yourVar is missing value then
    display dialog "is missing value" buttons {"OK"}
end if

NSAppleEventDescriptor.null()没有点击这个检查.输入 cMissingValue 即可.

NSAppleEventDescriptor.null() does not hit this check. Type cMissingValue does.

我已将其添加为 Swift 扩展:NSAppleEventDescriptor.missingValue()

I've added it as a Swift Extension: NSAppleEventDescriptor.missingValue()

extension NSAppleEventDescriptor {

    static func missingValue() -> NSAppleEventDescriptor {
        return NSAppleEventDescriptor(typeCode: UInt32(cMissingValue))
    }

}

这篇关于Cocoa 脚本:返回“null"与“缺失值"对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:38:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缺失   脚本   Cocoa   quot   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!