I am trying to get the urls that a browser like safari has open (for each window). Currently, I get all of the running apps on my Mac and pull out the safari app, so I can get it as an ‘NSRunningApplication’. Further, I am trying to find the file that an app like Final Cut Pro has open for all of its instances.
Then I use the following code to try and get the urls from the Safari browser:
if AXUIElementCopyAttributeValue(appReference, "AXURL" as CFString, &urls) == .success {
let url = urls as! [AXUIElement]
print("url = (url)")
}
The code that I use to try and get the document of Final Cut Pro instance is (not sure how to get the document for each instance of the program):
if AXUIElementCopyAttributeValue(appReference, "AXDocument" as CFString, &doc) == .success {
let docReference = doc as! [AXUIElement]
print("docReference = (docReference)")
}
this code only returns an array of ‘AXUIElement’ that I am not sure how to get a url out of it.
Any ideas on what I might be missing to pull the current urls that the safari browser has open? Also any idea on what I might be missing to pull the file for each instance (window) of a given program? I would like to get all of the urls from each window and somehow distinguish which window is which (E.g., use an array of arrays with urls in them).
Please note that I was trying not to use apple script, but if you know of a good solution and how I might integrate it into my swift app I would love to hear about it.
2
Answers
With AppleScript, You can use
NSAppleScript
in Swift.Example:
The AppleScript will return a string, like this:
I would look into the property kAXURLAttrubte. It is better to use than "AXURL"