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
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.
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)
}
I'm just not sure what to do with the AXUIElement once I make it. Any help would be greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…