简体   繁体   English

Xcode 8.0 和 swift 3.0 中的 ScriptingBridge 代码错误

[英]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.

相关问题 Xcode 8.0 Swift 3.0 缓慢的索引和构建 - Xcode 8.0 Swift 3.0 slow indexing and building sinch 验证方法未在 swift 3.0 和 xcode 8.0 中显示 - sinch verification method not showing in swift 3.0 and xcode 8.0 Xcode 8.0和Swift 3.0转换:寻找特定转换错误的解释 - Xcode 8.0 and Swift 3.0 conversion: Looking for explanation for a particular conversion error Firebase推送通知-XCode 8.0 / Swift 3.0 / ios 10.0 - Firebase Push Notification - XCode 8.0 / Swift 3.0/ ios 10.0 在Xcode 8.0 Swift 3.0的接口控制器之间传递数据 - Passing Data between Interface Controllers on Xcode 8.0 Swift 3.0 代码完成下拉建议无法正常工作Xcode 8.0 Swift 3 - Code completion drop down suggestions not working properly Xcode 8.0 Swift 3 Xcode 8.0 swift 3.0 错误 Alamofire-swift.h 未找到且无法构建 Objective-C 模块“Alamofire” - Xcode 8.0 swift 3.0 error Alamofire-swift.h not found and Could not build Objective-C module 'Alamofire' 使用Swift 3.0和部署目标8.0的Alamofire - Alamofire with Swift 3.0 & Deployment Target 8.0 Xcode 8 Swift 3.0:解析错误-无法读取PHPMailer代码 - Xcode 8 Swift 3.0 : Parse error - Could not read PHPMailer code 带有旧版Swift 2.3的Xcode 8:SourceKit强加Swift 3.0规则并显示幻像错误 - Xcode 8 with legacy Swift 2.3: SourceKit imposes Swift 3.0 rules & shows phantom errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM