我正在使用“null”的cMissingValue 版本,如下所示:
NSAppleEventDescriptor(typeCode: UInt32(cMissingValue))
根据下面的测试,我决定使用它。
我正在使用 Swift 运行并将参数传递给 AppleScript 子例程,使用此处的代码:
Passing variables to an 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 子例程,我已经对每个结果进行了注释:
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() 版本似乎出于某种原因正在获取“当前应用程序”值。
看来我可能想使用cMissingValue,如另一个答案所示。
可以像这样在 AppleScript 中对变量进行零检查:
if yourVar is missing value then
display dialog "is missing value" buttons {"OK"}
end if
NSAppleEventDescriptor.null()没有打了这个检查。输入 cMissingValue 即可。
我已将其添加为 Swift 扩展:NSAppleEventDescriptor.missingValue()
extension NSAppleEventDescriptor {
static func missingValue() -> NSAppleEventDescriptor {
return NSAppleEventDescriptor(typeCode: UInt32(cMissingValue))
}
}