Commit 280c86a0f1

280c86a0f1113de7543d6fb7f171444e9611d590

parent: 722c0f5b47

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 08:21 UTC

Start access to picked folders before bookmarking them

A folder from SwiftUI's file importer is security-scoped, and the sandbox
refuses a bookmark of it until access starts, so Add Folder failed with
"couldn't be opened" in the Mac App Store build. Ref !402

Layout: unified · split

Sources/OrgApp/ConfigFolder.swift +2 −2
@@ -14,7 +14,7 @@ public enum ConfigFolder {
14 var stale = false 14 var stale = false
15 guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil } 15 guard let url = try? URL(resolvingBookmarkData: data, options: Sandbox.bookmarkResolutionOptions(sandboxed: sandboxed), bookmarkDataIsStale: &stale) else { return nil }
16 _ = url.startAccessingSecurityScopedResource() 16 _ = url.startAccessingSecurityScopedResource()
17 if stale, let fresh = try? url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed)) { 17 if stale, let fresh = try? Sandbox.bookmark(url, sandboxed: sandboxed) {
18 defaults.set(fresh, forKey: bookmarkKey) 18 defaults.set(fresh, forKey: bookmarkKey)
19 } 19 }
20 return url 20 return url
@@ -25,7 +25,7 @@ public enum ConfigFolder {
25 @discardableResult 25 @discardableResult
26 public static func set(_ url: URL, defaults: UserDefaults = .standard, sandboxed: Bool = Sandbox.isActive) -> Bool { 26 public static func set(_ url: URL, defaults: UserDefaults = .standard, sandboxed: Bool = Sandbox.isActive) -> Bool {
27 _ = url.startAccessingSecurityScopedResource() 27 _ = url.startAccessingSecurityScopedResource()
28 guard let bookmark = try? url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed)) else { return false } 28 guard let bookmark = try? Sandbox.bookmark(url, sandboxed: sandboxed) else { return false }
29 defaults.set(bookmark, forKey: bookmarkKey) 29 defaults.set(bookmark, forKey: bookmarkKey)
30 return true 30 return true
31 } 31 }
Sources/OrgApp/FolderGrants.swift +1 −1
@@ -5,7 +5,7 @@ import OrgCore
5/// elsewhere. 5/// elsewhere.
6public enum SecurityBookmark { 6public enum SecurityBookmark {
7 public static func make(_ url: URL, sandboxed: Bool = Sandbox.isActive) -> Data? { 7 public static func make(_ url: URL, sandboxed: Bool = Sandbox.isActive) -> Data? {
8 try? url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed)) 8 try? Sandbox.bookmark(url, sandboxed: sandboxed)
9 } 9 }
10 10
11 /// The bookmarked URL with access to it started, and new data when the bookmark was stale. 11 /// The bookmarked URL with access to it started, and new data when the bookmark was stale.
Sources/OrgCore/Sandbox.swift +8
@@ -25,6 +25,14 @@ public enum Sandbox {
25 #endif 25 #endif
26 } 26 }
27 27
28 /// A bookmark of `url` made with `bookmarkCreationOptions`. Access to the URL is started
29 /// first: a URL from SwiftUI's file importer is security-scoped, and the sandbox refuses a
30 /// bookmark of it until access starts. Access stays on for the session.
31 public static func bookmark(_ url: URL, sandboxed: Bool = isActive) throws -> Data {
32 _ = url.startAccessingSecurityScopedResource()
33 return try url.bookmarkData(options: bookmarkCreationOptions(sandboxed: sandboxed), includingResourceValuesForKeys: nil, relativeTo: nil)
34 }
35
28 /// The resolving side of `bookmarkCreationOptions`. 36 /// The resolving side of `bookmarkCreationOptions`.
29 public static func bookmarkResolutionOptions(sandboxed: Bool = isActive) -> URL.BookmarkResolutionOptions { 37 public static func bookmarkResolutionOptions(sandboxed: Bool = isActive) -> URL.BookmarkResolutionOptions {
30 #if os(macOS) 38 #if os(macOS)
Sources/OrgWorkspace/Discovery.swift +1 −1
@@ -10,7 +10,7 @@ public struct WorkspaceRoot: Sendable, Equatable, Codable {
10 public var path: String 10 public var path: String
11 11
12 public init(url: URL, sandboxed: Bool = Sandbox.isActive) throws { 12 public init(url: URL, sandboxed: Bool = Sandbox.isActive) throws {
13 bookmark = try url.bookmarkData(options: Sandbox.bookmarkCreationOptions(sandboxed: sandboxed), includingResourceValuesForKeys: nil, relativeTo: nil) 13 bookmark = try Sandbox.bookmark(url, sandboxed: sandboxed)
14 path = url.standardizedFileURL.path 14 path = url.standardizedFileURL.path
15 } 15 }
16 16