I've created an app that grabs the current URL and Title/name from the frontmost window/tab of Safari or any of a number of Chromium browsers, using NSAppleScript
. The app sits in the menu bar and can be summoned by shortcut key combo.
let script = """
tell application \"Safari\"
if not (exists front window) then return {\"\", \"\"}
set theTab to current tab of front window
set theURL to URL of theTab
set theTitle to name of theTab
return {theURL, theTitle}
end tell
"""
if let appleScript = NSAppleScript(source: script) {
let output = appleScript.executeAndReturnError(&error)
if output.numberOfItems == 2 {
let url = output.atIndex(1)?.stringValue
let title = output.atIndex(2)?.stringValue
if let url = url, !url.isEmpty {
return (url, title)
}
}
}
If I sign an archived build and run it locally it works beautifully, no matter which browser I am using.
But the URL/title grabbing breaks in sandbox due to permissions.
I read and have been informed that I need to use com.apple.security.scripting-targets
entitlement. The example for this is in WWDC 2012 and talks about accessing Mail compose window.
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.mail</key>
<array>
<string>com.apple.mail.compose</string>
</array>
</dict>
However, I don't want to control the app or use any access groups, as I've looked through the sdef and Safari/Chrome do not provide any access groups whose contents I'm interested in.
I just want to get the property/values of a window/tab. So I think I could be quite restrictive about the read-only access to two properties or objects that I need.
That said, I'm going back and forth with TestFlight review kind of shooting in the dark. I need help!
So I figure it's time to ask: what content should my entitlement have?
Or am I on the wrong path entirely?
I know it's possible because an app called Neptunes does it to get properties from Music.app
Many thanks in advance,
matt
I expect you will also need to add a com.apple.security.temporary-exception.apple-events App Sandbox Temporary Exception Entitlement entitlement.
Note, you'll need to replace the bundle ID in the examples displayed in the documentation above with the bundle ID for the app you're targeting. For example, if you're targeting Safari, you'd do something like this:
<key>com.apple.security.temporary-exception.apple-events:before:10.8</key>
<string>com.apple.Safari</string>
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.Safari</key>
<array/>
</dict>
Those entitlements should allow you to use an NSAppleScript object in your app to send events to Safari. For other browser apps that don't explicitly support AppleScript commands you will probably need to use GUI scripting or the accessibility APIs to get the information you're looking for.