问题描述
限时送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:关键词]
发布评论