Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access bookmark url in macOS #624

Open
onmyway133 opened this issue Mar 17, 2020 · 0 comments
Open

How to access bookmark url in macOS #624

onmyway133 opened this issue Mar 17, 2020 · 0 comments

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Mar 17, 2020

By default the approaches above grant you access while the app remains open. When you quit the app, any folder access you had is lost.

To gain persistent access to a folder even on subsequent launches, we’ll have to take advantage of a system called Security-Scoped Bookmarks.

Add entitlements

Use of app-scoped bookmarks and URLs

<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>

Enabling Security-Scoped Bookmark and URL Access

If you want to provide your sandboxed app with persistent access to file system resources, you must enable security-scoped bookmark and URL access. Security-scoped bookmarks are available starting in macOS v10.7.3.

To add the bookmarks.app-scope or bookmarks.document-scope entitlement, edit the target’s .entitlements property list file using the Xcode property list editor. Use the entitlement keys shown in Table 4-4, depending on which type of access you want. Use a value of for each entitlement you want to enable. You can enable either or both entitlements.

func saveBookmark(item: ShortcutItem) {
    guard let url = item.fileUrl else { return  }
    do {
        let bookmarkData = try url.bookmarkData(
            options: .withSecurityScope,
            includingResourceValuesForKeys: nil,
            relativeTo: nil
        )

        item.bookmark = bookmarkData
    } catch {
        print("Failed to save bookmark data for \(url)", error)
    }
}

func loadBookmark(item: ShortcutItem) -> URL? {
    guard let data = item.bookmark else { return nil }
    do {
        var isStale = false
        let url = try URL(
            resolvingBookmarkData: data,
            options: .withSecurityScope,
            relativeTo: nil,
            bookmarkDataIsStale: &isStale
        )
        if isStale {
            saveBookmark(item: item)
        }
        return url
    } catch {
        print("Error resolving bookmark:", error)
        return nil
    }
}


_ = url.startAccessingSecurityScopedResource()
NSWorkspace.shared.open(url)
url.stopAccessingSecurityScopedResource()
_ = url.startAccessingSecurityScopedResource()
NSWorkspace.shared.selectFile(
    url.path,
    inFileViewerRootedAtPath: url.deletingLastPathComponent().path
)
url.stopAccessingSecurityScopedResource()

Read more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant