Skip to content

Commit 9fad23c

Browse files
additional scenario
1 parent 0a6b6bc commit 9fad23c

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

Stapler.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@
281281
GENERATE_INFOPLIST_FILE = YES;
282282
INFOPLIST_FILE = Stapler/Info.plist;
283283
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
284-
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "This app needs access to your Desktop folder to open and manage files.";
285-
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "This app needs access to your Documents folder to open and manage files.";
286-
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "This app needs access to your Downloads folder to open and manage files.";
287-
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton.\nAll rights reserved.";
284+
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
285+
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
286+
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
287+
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton";
288288
LD_RUNPATH_SEARCH_PATHS = (
289289
"$(inherited)",
290290
"@executable_path/../Frameworks",
@@ -313,10 +313,10 @@
313313
GENERATE_INFOPLIST_FILE = YES;
314314
INFOPLIST_FILE = Stapler/Info.plist;
315315
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
316-
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "This app needs access to your Desktop folder to open and manage files.";
317-
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "This app needs access to your Documents folder to open and manage files.";
318-
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "This app needs access to your Downloads folder to open and manage files.";
319-
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton.\nAll rights reserved.";
316+
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
317+
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
318+
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
319+
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton";
320320
LD_RUNPATH_SEARCH_PATHS = (
321321
"$(inherited)",
322322
"@executable_path/../Frameworks",

Stapler/Info.plist

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>NSDesktopFolderUsageDescription</key>
6-
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
7-
<key>NSDocumentsFolderUsageDescription</key>
8-
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
9-
<key>NSDownloadsFolderUsageDescription</key>
10-
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
115
<key>CFBundleDocumentTypes</key>
126
<array>
137
<dict>

Stapler/StaplerApp.swift

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ enum DocumentOpeningScenario {
88
case launchedWithDocument
99
case resumedBySystem
1010
case openedThroughFileMenu
11+
case openedFromFinderWhileRunning
1112
case unknown
1213
}
1314

1415
// Modify the AppDelegate to work with the new AppStateManager
1516
class AppDelegate: NSObject, NSApplicationDelegate {
1617
func setupDefaultCommandKeyDelay() {
1718
if UserDefaults.standard.object(forKey: "CommandKeyDelay") == nil {
18-
UserDefaults.standard.set(100, forKey: "CommandKeyDelay") // Default wait 100ms
19+
UserDefaults.standard.set(0, forKey: "CommandKeyDelay") // Default wait 0ms
1920
}
2021
}
2122

@@ -585,6 +586,10 @@ struct StaplerApp: App {
585586
logger.info("Document Opening Scenario: openedThroughFileMenu")
586587
// Handle opened through file menu scenario
587588
break
589+
case .openedFromFinderWhileRunning:
590+
logger.info("Document Opening Scenario: openedFromFinderWhileRunning")
591+
// Handle opened through Finder whilst running scenario
592+
handleOpenedFromFinderWhileRunning(url)
588593
case .unknown:
589594
logger.info("Document Opening Scenario: unknown")
590595
// Handle unknown scenarios
@@ -601,6 +606,8 @@ struct StaplerApp: App {
601606

602607
if appStateManager.wasJustLaunched && isOpenedFromFinder {
603608
return .launchedWithDocument
609+
} else if isOpenedFromFinder && NSApp.isActive {
610+
return .openedFromFinderWhileRunning
604611
} else if NSApp.isActive {
605612
return .openedThroughFileMenu
606613
} else if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 15, patchVersion: 0)) {
@@ -612,38 +619,65 @@ struct StaplerApp: App {
612619
}
613620

614621
private func handleLaunchedWithDocument(_ url: URL) {
615-
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
616-
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
617-
618-
if !commandKeyPressed {
619-
do {
620-
guard url.startAccessingSecurityScopedResource() else {
621-
logger.error("Failed to access security-scoped resource")
622-
return
623-
}
624-
defer { url.stopAccessingSecurityScopedResource() }
625-
626-
let document = try StaplerDocument(contentsOf: url)
627-
let viewModel = StaplerViewModel(document: document)
628-
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
629-
630-
// Close the document
631-
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
632-
windowController.close()
633-
}
634-
635-
// If this was the only document and the app was just launched, quit the app
636-
if NSDocumentController.shared.documents.count == 1 {
637-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
638-
NSApp.terminate(nil)
639-
}
640-
}
641-
} catch {
642-
logger.error("Error handling document opening: \(error.localizedDescription)")
643-
}
644-
}
645-
}
646-
}
622+
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
623+
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
624+
625+
if !commandKeyPressed {
626+
do {
627+
guard url.startAccessingSecurityScopedResource() else {
628+
logger.error("Failed to access security-scoped resource")
629+
return
630+
}
631+
defer { url.stopAccessingSecurityScopedResource() }
632+
633+
let document = try StaplerDocument(contentsOf: url)
634+
let viewModel = StaplerViewModel(document: document)
635+
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
636+
637+
// Close the document
638+
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
639+
windowController.close()
640+
}
641+
642+
// If this was the only document and the app was just launched, quit the app
643+
if NSDocumentController.shared.documents.count == 1 {
644+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
645+
NSApp.terminate(nil)
646+
}
647+
}
648+
} catch {
649+
logger.error("Error handling document opening: \(error.localizedDescription)")
650+
}
651+
}
652+
}
653+
}
654+
655+
private func handleOpenedFromFinderWhileRunning(_ url: URL) {
656+
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
657+
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
658+
659+
if !commandKeyPressed {
660+
do {
661+
guard url.startAccessingSecurityScopedResource() else {
662+
logger.error("Failed to access security-scoped resource")
663+
return
664+
}
665+
defer { url.stopAccessingSecurityScopedResource() }
666+
667+
let document = try StaplerDocument(contentsOf: url)
668+
let viewModel = StaplerViewModel(document: document)
669+
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
670+
671+
// Close the document
672+
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
673+
windowController.close()
674+
}
675+
} catch {
676+
logger.error("Error handling document opening: \(error.localizedDescription)")
677+
}
678+
}
679+
}
680+
}
647681

648682
var body: some Scene {
649683
DocumentGroup(newDocument: StaplerDocument()) { file in

0 commit comments

Comments
 (0)