Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 无法在SWIFT中打开NSAppleEventDescriptor_Objective C_Swift_Applescript_Cocoa Scripting_Nsappleeventdescriptor - Fatal编程技术网

Objective c 无法在SWIFT中打开NSAppleEventDescriptor

Objective c 无法在SWIFT中打开NSAppleEventDescriptor,objective-c,swift,applescript,cocoa-scripting,nsappleeventdescriptor,Objective C,Swift,Applescript,Cocoa Scripting,Nsappleeventdescriptor,我有一个苹果脚本,它给了我一个结果。但我无法将该值取消到字符串,以便使用它 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 setFrontA

我有一个苹果脚本,它给了我一个结果。但我无法将该值取消到字符串,以便使用它

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")>

我还尝试使用方法
descriptorworkeyword()
,但我不知道如何设置AEKeyword。

scriptResult.stringValue()
,可能吧?(我不太使用Swift。)

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

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

如果您只想输出数据,
println
将在不带时间戳的情况下执行此操作(事实上,
NSLog
也会在控制台上记录一个错误,您可能不想这样做)


这不起作用,因为它返回了太多的
.2015-03-14 15:51:43.143测试[8272:175567]测试。swift。
我只需要记录碎屑的最后一部分。OP是正确的
-stringValue
将AE描述符强制为
typeUnicodeText
,然后将其解压为
NSString
,或者
nil
如果无法强制描述符为该类型。至于噪音,我认为你有阅读问题:当它将你的消息字符串写入stderr时,
NSLog
包含了邮戳等;这不是你成绩的一部分。(提示:
CFShow((CFStringRef)[NSString stringWithFormat:…])
是一个噪音较小的选项。)@Wevah这样做,除了
stringValue
是一个属性,因此不需要
()
,而且它是一个可选字符串,因此需要展开。正如我所说,我不太使用Swift.:P
var number:Int = 1
let result = scriptResult.descriptorAtIndex(number)
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")
}