[英]ScriptingBridge code errors in Xcode 8.0 and swift 3.0
This is the code I used in Xcode 7.3.1 and that worked fine:这是我在 Xcode 7.3.1 中使用的代码,效果很好:
var selectedFiles = NSMutableArray(capacity:1)
let finder: AnyObject! = SBApplication(bundleIdentifier:"com.apple.finder")
let finderObject = finder.selection as! SBObject
let selection: AnyObject! = finderObject.get()
let items = selection.arrayByApplyingSelector(Selector("URL"))
let filteredfiles = (items as NSArray).pathsMatchingExtensions(["ai","pdf","ap","paf","pafsc"])
for item in filteredfiles {
let url = NSURL(string:item ,relativeToURL:nil)
selectedFiles.addObject(url!)
}
This is the code corrected for Xcode 8.0 and that does not work: the error is generated for the last line这是针对 Xcode 8.0 更正的代码,但不起作用:最后一行生成错误
error = Cannot call value of non-function type '[Any]!'错误 = 无法调用非函数类型的值 '[Any]!'
var selectedFiles = NSMutableArray(capacity:1)
let finder: AnyObject! = SBApplication(bundleIdentifier:"com.apple.finder")
let finderObject = finder.selection as! SBObject
if let selection = finderObject.get() as AnyObject?{
let items = selection.array(#selector(getter: NSTextCheckingResult.url))
let filteredfiles = (items as NSArray).pathsMatchingExtensions(["ai","pdf","ap","paf","pafsc"])
for item in filteredfiles {
let url = NSURL(string:item ,relativeToURL:nil)
selectedFiles.addObject(url!)
}
}
I have tried many solutions, but unfortunately cannot find a clue.我尝试了很多解决方案,但不幸的是找不到线索。 I guess this is because Swift 3.0x APIs have changed drastically.... Any help is welcome!
我想这是因为 Swift 3.0x APIs 发生了巨大的变化......欢迎任何帮助!
This is a slightly different approach using a couple of native Swift functions for Swift 3这是一种稍微不同的方法,它使用了几个适用于 Swift 3 的本机 Swift 函数
var selectedFiles = [URL]()
let finder : AnyObject = SBApplication(bundleIdentifier:"com.apple.finder")!
let finderObject = finder.selection as! SBObject
if let selection = finderObject.get() as? [SBObject] {
selection.forEach { item in
let url = URL(string: item.value(forKey:"URL") as! String)!
selectedFiles.append(url)
}
let goodExtensions = ["ai","pdf","ap","paf","pafsc"]
let filteredURLs = selectedFiles.filter({goodExtensions.contains($0.pathExtension)})
print(filteredURLs)
}
PS: I highly recommend to use AppleScriptObjC
. PS:我强烈建议使用
AppleScriptObjC
。 It's so much easier to use.使用起来要容易得多。
PPS: valueForKey
is intentionally used because KVC is really needed to get the property value. PPS:
valueForKey
是故意使用的,因为真的需要KVC来获取属性值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.