skip to Main Content

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


  1. With AppleScript, You can use NSAppleScript in Swift.

    Example:

    set r to "" -- an empty variable for appending a string
    tell application "Safari"
        repeat with w in windows -- loop for each window, w is a variable which contain the window object
            if exists current tab of w then --  is a valid browser window
                repeat with t in tabs of w -- loop for each tab of this window, , t is a variable which contain the tab object
                    -- get the title (name) of this tab and get the url of this tab
                    tell t to set r to r & "Title : " & name & ", URL : " & URL & linefeed -- append a line to the variable (r)
                    (*
                     'linefeed'  mean a line break
                     'tell t' mean a tab of w (window)
                     '&'  is for  concatenate strings, same as the + operator in Swift
                    *)
                end repeat
            end if
        end repeat
    end tell
    return r -- return the string (each line contains a title and an URL)
    

    The AppleScript will return a string, like this:

    Title : the title of the first tab, URL : the url of the first tab
    Title : the title of the second tab, URL : the url of the second tab
    Title : the title of the third tab, URL : the url of the third tab
    ....
    

    Login or Signup to reply.
  2. I would look into the property kAXURLAttrubte. It is better to use than "AXURL"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search