在OS X上的另一个应用程序中插入文本

时间:2018-09-08 05:17:12

标签: swift macos accessibility uiaccessibility

您好,我所有的代码都应检测到重点关注的应用程序,在下一部分中,我想在检测到的应用程序的字段中插入一些文本,但是运行此代码时出现错误,我无法解决它:

import Foundation 

func getFocusedApp() -> AXUIElement? {
    var output: AXUIElement? = nil
    let systemWideElement: AXUIElement = AXUIElementCreateSystemWide()
    let kAXFocusedApplication : CFString! = "AXFocusedApplication" as CFString
    let focusedElementPtr : UnsafeMutablePointer<CFTypeRef?> = UnsafeMutablePointer.allocate(capacity: 1)
    let error : AXError = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedApplication, focusedElementPtr)

    if let ue = focusedElementPtr.memory {
        output = ue.takeUnretainedValue() as AXUIElement
    }
    else {
        print(error)
    }
    focusedElementPtr.deallocate()
    return output
}

print(getFocusedApp())

这是错误:

error: value of type 'UnsafeMutablePointer<CFTypeRef?>' (aka 'UnsafeMutablePointer<Optional<AnyObject>>') has no member 'memory' 
if let ue = focusedElementPtr.memory { 

顺便说一句。我不是SWIFT开发人员,所以为所有错误感到抱歉。

0 个答案:

没有答案