jyet jyet - 3 months ago 11
Swift Question

How to get all open URL in every tab of every Window in Safari?

I am using NSAppleScript to tell Safari to return URL of all opened tabs as follows:

let script = NSAppleScript.init(source: "tell application \"Safari\" to get URL of every tab of every window")
var errorInfo: NSDictionary?
let eventDescriptor:NSAppleEventDescriptor = (script?.executeAndReturnError(&errorInfo))!
print(eventDescriptor.stringValue)


Above returns nil. Telling to return current URL do work.

"tell application \"Safari\" to return URL of front document as string"





How to get all opened URL in every tab of every window?

foo foo
Answer

The result will be an NSDescriptor representing a list of lists of strings. Either you 1. coerce the list to string in AppleScript before returning it, using a suitable text item delimiter upon which you can then split the string into a list of strings in Swift (cheat), 2. use a couple of nested loops to unpack the list descriptors yourself (tedious), or 3. ditch NSAppleScript and use the AppleScript-ObjC bridge instead (a little fiddly to set up, but by far the simplest and most capable to use).