Xcode 8.0 和 swift 3.0 中的 ScriptingBridge 代码错误
ScriptingBridge code errors in Xcode 8.0 and swift 3.0
这是我在 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!)
}
这是针对 Xcode 8.0 更正的代码,但它不起作用:
最后一行产生错误
错误 = 无法调用非函数类型“[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!)
}
}
我试过很多办法,可惜找不到头绪。
我想这是因为 Swift 3.0x API 发生了巨大变化....
欢迎任何帮助!
这是一种略有不同的方法,使用几个本机 Swift 函数 Swift 3
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([=10=].pathExtension)})
print(filteredURLs)
}
PS:我强烈推荐使用AppleScriptObjC
。使用起来非常简单。
PPS:valueForKey
是有意使用的,因为确实需要KVC才能得到属性值。
这是我在 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!)
}
这是针对 Xcode 8.0 更正的代码,但它不起作用: 最后一行产生错误
错误 = 无法调用非函数类型“[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!)
}
}
我试过很多办法,可惜找不到头绪。 我想这是因为 Swift 3.0x API 发生了巨大变化.... 欢迎任何帮助!
这是一种略有不同的方法,使用几个本机 Swift 函数 Swift 3
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([=10=].pathExtension)})
print(filteredURLs)
}
PS:我强烈推荐使用AppleScriptObjC
。使用起来非常简单。
PPS:valueForKey
是有意使用的,因为确实需要KVC才能得到属性值。