首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在SWIFT中打开NSAppleEventDescriptor

无法在SWIFT中打开NSAppleEventDescriptor
EN

Stack Overflow用户
提问于 2015-03-14 14:30:05
回答 2查看 1K关注 0票数 3

我有一个Applescript,它给了我一个结果。但是,我无法将值解压缩为String,所以我可以使用它。

代码语言:javascript
复制
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看起来如下所示:

代码语言:javascript
复制
2015-03-14 15:15:14.001 test[7315:161881] 
<NSAppleEventDescriptor:'utxt'("test.swift")>

实际结果是字符串"test.swift“。如何打开/解析此结果?

我试着加了这个:

代码语言:javascript
复制
var number:Int = 1
let result = scriptResult.descriptorAtIndex(number)

另外,我尝试使用descriptorForKeyword(<#keyword: AEKeyword#>)方法,但我不知道如何设置AEKeyword。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-14 15:11:24

您可以使用if…let语法同时检查nil的结果,如果它有一个值,则将其展开。

descriptorAtIndex不会得到您想要的东西,因为描述符不包含utxt条目--它是一个utxt条目(您可以通过打印出result.descriptorType来看到这一点,这将为‘utxt’提供四个字符的代码)。因此,stringValue应该获得普通字符串值,但它是可选的,因此可以在相同的let中展开。

如果您只想输出数据,println将在没有时间戳等的情况下这样做(实际上,NSLog也会将错误记录到控制台,而您可能不想要)

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-03-14 14:36:11

也许是scriptResult.stringValue()?(我不常使用Swift。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29050049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档