【问题标题】:Unable to unwrap NSAppleEventDescriptor in SWIFT无法在 SWIFT 中解开 NSAppleEventDescriptor
【发布时间】:2015-03-14 14:30:05
【问题描述】:

我有一个 Applescript,它给了我一个结果。但我无法将值解开为字符串,因此我可以使用它。

var set: String = "set windowTile to \"\"\n"
var tell: String = "tell application \"System Events\"\n"
var setFrontApp: String = "set frontApp to first application process whose frontmost is true\n"
var setFrontAppName: String = "set frontAppName to name of frontApp\n"
var tellProcces: String = "tell process frontAppName\n"
var tellFirst: String = "tell (1st window whose value of attribute \"AXMain\" is true)\n"
var setWindowTitle: String = "set windowTitle to value of attribute \"AXTitle\"\n"
var endTellFirst: String = "end tell\n"
var endTellProcess: String = "end tell\n"
var endTell: String = "end tell"
var startAtLoginScript: NSAppleScript = NSAppleScript(source: set + tell + setFrontApp + setFrontAppName + tellProcces + tellFirst + setWindowTitle + endTellFirst + endTellProcess + endTell)     
var scriptResult:NSAppleEventDescriptor = startAtLoginScript.executeAndReturnError(errorInfo)!

NSLog ("%@",  scriptResult)

NSLog 看起来像这样:

2015-03-14 15:15:14.001 test[7315:161881] 
<NSAppleEventDescriptor:'utxt'("test.swift")>

实际的结果是一个字符串“test.swift”。我怎样才能解开/解析这个结果?

我尝试添加这个:

var number:Int = 1
let result = scriptResult.descriptorAtIndex(number)

我也尝试使用descriptorForKeyword(&lt;#keyword: AEKeyword#&gt;)的方法,但我不知道设置AEKeyword的热度。

【问题讨论】:

    标签: objective-c swift applescript cocoa-scripting nsappleeventdescriptor


    【解决方案1】:

    您可以使用if…let 语法同时检查nil 的结果,如果有值则解包。

    descriptorAtIndex 不会得到你想要的,因为描述符不包含 utxt 条目——它 utxt 条目(你可以通过打印出 @987654326 来看到这一点@ 这将为您提供 'utxt' 的四字符代码)。所以stringValue 应该为您提供纯字符串值,但它是可选的,因此您可以在相同的let 中展开。

    如果您只想输出数据,println 将在没有时间戳等的情况下执行此操作。(事实上,NSLog 还会将您可能不想要的错误记录到控制台)

    import Foundation
    
    let script = "\n".join([
      "set windowTile to \"\"",
      "tell application \"System Events\"",
        "set frontApp to first application process whose frontmost is true",
        "set frontAppName to name of frontApp",
        "tell process frontAppName",
          "tell (1st window whose value of attribute \"AXMain\" is true)",
            "set windowTitle to value of attribute \"AXTitle\"",
          "end tell",
        "end tell",
      "end tell",
    ])
    
    var errorInfo: NSDictionary?
    
    if let script = NSAppleScript(source: script),
       let result = script.executeAndReturnError(&errorInfo),
       let text = result.stringValue {
        println(text)
    }
    else if let error = errorInfo {
        println(error)
    }
    else {
        println("Unexpected error while executing script")
    }
    

    【讨论】:

      【解决方案2】:

      scriptResult.stringValue(),也许吧? (我不怎么用 Swift。)

      【讨论】:

      • 这不起作用,因为它返回太多.2015-03-14 15:51:43.143 test[8272:175567] test.swift.我只需要最后一部分
      • 那只是记录碎屑。
      • OP 是正确的。 -stringValue 会将 AE 描述符强制转换为 typeUnicodeText,然后将其解压缩为 NSStringnil,如果它无法将描述符强制转换为该类型。至于噪音,我认为您遇到了阅读问题:NSLog 将您的消息字符串写入 stderr 时包含日期戳等;这不是你结果的一部分。 (提示:CFShow((CFStringRef)[NSString stringWithFormat:...]) 是一个噪音较小的选项。)
      • @Wevah 这样做了,除了stringValue 是一个属性,所以不需要(),它是一个可选字符串,所以想要展开。
      • 就像我说的,我很少使用 Swift。 :p
      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 2019-11-11
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      相关资源
      最近更新 更多