Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbeardman committed Aug 19, 2024
1 parent 531a5a7 commit 8d4e007
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Stapler.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.2.1;
MARKETING_VERSION = 1.2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.gingerbeardman.Stapler;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -321,7 +321,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.2.1;
MARKETING_VERSION = 1.2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.gingerbeardman.Stapler;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
29 changes: 23 additions & 6 deletions Stapler/StaplerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,29 @@ struct AliasItem: Identifiable, Codable, Hashable {
do {
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
if isStale {
// If the bookmark is stale, we need to create a new one
_ = try url.bookmarkData(options: [.withSecurityScope, .securityScopeAllowOnlyReadAccess], includingResourceValuesForKeys: nil, relativeTo: nil)
if let aliasItem = try? AliasItem(id: id, url: url) {
return aliasItem.resolveURL()
}
}
if !url.startAccessingSecurityScopedResource() {
print("Failed to access security scoped resource")
return nil
}
return url
} catch {
print("StaplerApp: Alias: Error resolving bookmark: \(error)")
print("Error resolving bookmark: \(error)")
return nil
}
}

static func == (lhs: AliasItem, rhs: AliasItem) -> Bool {
lhs.id == rhs.id && lhs.bookmarkData == rhs.bookmarkData
}
}

struct StaplerDocument: FileDocument, Equatable {
static var readableContentTypes: [UTType] { [.staplerDocument] }
static var writableContentTypes: [UTType] { [.staplerDocument] }

var fileURL: URL?

var aliases: [AliasItem]

init() {
Expand All @@ -109,6 +112,7 @@ struct StaplerDocument: FileDocument, Equatable {
let data = try Data(contentsOf: url)
let decodedData = try JSONDecoder().decode(StaplerDocumentData.self, from: data)
self.aliases = decodedData.aliases
self.fileURL = url
}

func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
Expand Down Expand Up @@ -381,6 +385,10 @@ struct ContentView: View {

private func launchAlias(_ alias: AliasItem) {
if let url = alias.resolveURL() {
defer {
url.stopAccessingSecurityScopedResource()
}

let coordinator = NSFileCoordinator()
var error: NSError?
coordinator.coordinate(readingItemAt: url, options: .withoutChanges, error: &error) { url in
Expand Down Expand Up @@ -522,6 +530,15 @@ struct StaplerApp: App {
// Launch all items and close the document
DispatchQueue.main.async {
do {
// Start accessing the security-scoped resource
guard url.startAccessingSecurityScopedResource() else {
logger.error("Failed to access security-scoped resource")
return
}
defer {
url.stopAccessingSecurityScopedResource()
}

let document = try StaplerDocument(contentsOf: url)
let viewModel = StaplerViewModel(document: document)
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
Expand Down

0 comments on commit 8d4e007

Please sign in to comment.