Swift - 在另一个应用程序中获取当前打开的文档的文件路径 [英] Swift - Get file path of currently opened document in another application

查看:37
本文介绍了Swift - 在另一个应用程序中获取当前打开的文档的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 swift 代码从另一个应用程序获取打开文档的文件路径.我知道如何在 AppleScript 中做到这一点,就像这样:

I am attempting to get the file path of an open document from another application using swift code. I know how to do it in AppleScript, which is like this:

tell application "System Events"

if exists (process "Pro Tools") then

    tell process "Pro Tools"

        set thefile to value of attribute "AXDocument" of window 1

    end tell

end if

end tell

这个 AppleScript 做了我想要的,但我希望在 Swift 中实现它.我知道一个选择是从我的程序中运行这个脚本,但我希望找到另一种方法,可能不使用辅助功能.

This AppleScript does what I want, but I was hoping to do it natively in Swift. I know that one option is to run this script from my program, but I was hoping to find another way of doing it, potentially without using Accessibility.

在我的应用程序中,我可以通过执行以下操作将应用程序作为 AXUIElement 获取:

In my app I can get the application as a AXUIElement by doing the following:

let proToolsBundleIdentifier = "com.avid.ProTools"
let proToolsApp : NSRunningApplication? = NSRunningApplication
        .runningApplications(withBundleIdentifier: proToolsBundleIdentifier).last as NSRunningApplication?


if let app = proToolsApp {
    app.activate(options: .activateAllWindows)
}


if let pid = proToolsApp?.processIdentifier {   
    let app = AXUIElementCreateApplication(pid)
}

我只是不确定一旦我制作了 AXUIElement 该怎么办.任何帮助将不胜感激.

I'm just not sure what to do with the AXUIElement once I make it. Any help would be greatly appreciated.

推荐答案

感谢@JamesWaldrop 的另一篇博文,我能够自己回答这个问题,并想在这里发帖供任何寻找类似内容的人使用:

Thanks to the help of another post from @JamesWaldrop, I was able to answer this myself and wanted to post here for anyone looking for something similar:

let proToolsBundleIdentifier = "com.avid.ProTools"
let proToolsApp : NSRunningApplication? = NSRunningApplication
        .runningApplications(withBundleIdentifier: proToolsBundleIdentifier).last as NSRunningApplication?


if let pid = proToolsApp?.processIdentifier {

    var result = [AXUIElement]()
    var windowList: AnyObject? = nil // [AXUIElement]

    let appRef = AXUIElementCreateApplication(pid)
    if AXUIElementCopyAttributeValue(appRef, "AXWindows" as CFString, &windowList) == .success {
            result = windowList as! [AXUIElement]
    }

    var docRef: AnyObject? = nil
    if AXUIElementCopyAttributeValue(result.first!, "AXDocument" as CFString, &docRef) == .success {
        let result = docRef as! AXUIElement
        print("Found Document: (result)")
        let filePath = result as! String
        print(filePath)
    }
}

这会像 AppleScript 一样获取 AXDocument.仍然会接受其他可能更好或不使用辅助功能的方法.

This gets the AXDocument just like the AppleScript does. Would still be open to other methods of doing this that may be better or not using Accessibility.

这篇关于Swift - 在另一个应用程序中获取当前打开的文档的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆