Skip to content

Commit 7ad818f

Browse files
oaeenclaude
andcommitted
fix: handle nil Finder selection on macOS 15+
On macOS 15+, `finder.selection?.get()` via ScriptingBridge returns nil when nothing is selected (older macOS returned an empty array). The old `guard let selection, let selectionItems = selection.get() else throw` misread this as a permission failure, so every click with no selection surfaced `OITError.cannotAccessFinder` and fell out of `openOutsideSandbox` silently — the lite binary only writes the error to `~/Library/Logs` and exits, giving the user no feedback. Treat nil as an empty selection and fall through to the front-window check. Also unify the singular and plural helpers on the safer `as? [FinderItem]` cast (dropping an unsound `as! FinderItem` on individual items in the singular variant). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bee2c57 commit 7ad818f

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

OpenInTerminalCore/FinderManager.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ public class FinderManager {
1717
public func getFullUrlToFrontFinderWindowOrSelectedFile() throws -> URL? {
1818

1919
let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication
20-
20+
2121
var target: FinderItem
22-
23-
guard let selection = finder.selection,
24-
let selectionItems = selection.get() else {
25-
throw OITError.cannotAccessFinder
26-
}
27-
28-
if let firstItem = (selectionItems as! Array<AnyObject>).first {
22+
23+
// On macOS 15+, finder.selection?.get() returns nil when nothing is
24+
// selected (older macOS returned an empty array). Treat nil as "no
25+
// selection" and fall through to the front-window check, instead of
26+
// throwing cannotAccessFinder which surfaces no UI feedback.
27+
let selectionItems = (finder.selection?.get() as? [FinderItem]) ?? []
28+
29+
if let firstItem = selectionItems.first {
2930
// Files or folders selected
30-
target = firstItem as! FinderItem
31+
target = firstItem
3132
} else {
3233
// Check if there are finder windows opened
3334
guard let windows = finder.FinderWindows?(),
@@ -59,18 +60,17 @@ public class FinderManager {
5960
public func getFullUrlsToFrontFinderWindowOrSelectedFile() throws -> [URL] {
6061

6162
let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication
62-
63+
6364
var targets: [FinderItem]
64-
65-
guard let selection = finder.selection,
66-
let selectionItems = selection.get() else {
67-
throw OITError.cannotAccessFinder
68-
}
69-
70-
if let items = selectionItems as? Array<FinderItem>,
71-
items.count > 0 {
65+
66+
// See note in getFullUrlToFrontFinderWindowOrSelectedFile: on macOS
67+
// 15+ a nil result from selection.get() means "nothing selected",
68+
// not a permission failure. Treat it as empty and fall through.
69+
let selectionItems = (finder.selection?.get() as? [FinderItem]) ?? []
70+
71+
if !selectionItems.isEmpty {
7272
// Files or folders are selected
73-
targets = items
73+
targets = selectionItems
7474
} else {
7575
// Check if there are opened finder windows
7676
guard let windows = finder.FinderWindows?(),

0 commit comments

Comments
 (0)