@@ -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
1516class 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