@@ -8,14 +8,15 @@ enum DocumentOpeningScenario {
8
8
case launchedWithDocument
9
9
case resumedBySystem
10
10
case openedThroughFileMenu
11
+ case openedFromFinderWhileRunning
11
12
case unknown
12
13
}
13
14
14
15
// Modify the AppDelegate to work with the new AppStateManager
15
16
class AppDelegate : NSObject , NSApplicationDelegate {
16
17
func setupDefaultCommandKeyDelay( ) {
17
18
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
19
20
}
20
21
}
21
22
@@ -585,6 +586,10 @@ struct StaplerApp: App {
585
586
logger. info ( " Document Opening Scenario: openedThroughFileMenu " )
586
587
// Handle opened through file menu scenario
587
588
break
589
+ case . openedFromFinderWhileRunning:
590
+ logger. info ( " Document Opening Scenario: openedFromFinderWhileRunning " )
591
+ // Handle opened through Finder whilst running scenario
592
+ handleOpenedFromFinderWhileRunning ( url)
588
593
case . unknown:
589
594
logger. info ( " Document Opening Scenario: unknown " )
590
595
// Handle unknown scenarios
@@ -601,6 +606,8 @@ struct StaplerApp: App {
601
606
602
607
if appStateManager. wasJustLaunched && isOpenedFromFinder {
603
608
return . launchedWithDocument
609
+ } else if isOpenedFromFinder && NSApp . isActive {
610
+ return . openedFromFinderWhileRunning
604
611
} else if NSApp . isActive {
605
612
return . openedThroughFileMenu
606
613
} else if ProcessInfo . processInfo. isOperatingSystemAtLeast ( OperatingSystemVersion ( majorVersion: 10 , minorVersion: 15 , patchVersion: 0 ) ) {
@@ -612,38 +619,65 @@ struct StaplerApp: App {
612
619
}
613
620
614
621
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
+ }
647
681
648
682
var body : some Scene {
649
683
DocumentGroup ( newDocument: StaplerDocument ( ) ) { file in
0 commit comments