CodexBloom - Programming Q&A Platform

implementing NSWorkspace openFile: in macOS 14 - Files Not Opening in Default Applications

๐Ÿ‘€ Views: 161 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-08-30
macos swift nsworkspace Swift

I'm confused about I'm converting an old project and I'm working with an scenario with `NSWorkspace` when trying to open files with their associated default applications on macOS 14. Specifically, I'm using the `openFile:` method to open a `.txt` file, but the file does not open in TextEdit as expected. Instead, it throws an behavior: `NSErrorDomain=NSCocoaErrorDomain Code=260 "The file couldnโ€™t be opened because there is no such file."`. Here's the snippet I'm using: ```swift import Cocoa func openTextFile(atPath path: String) { let fileURL = URL(fileURLWithPath: path) let workspace = NSWorkspace.shared let success = workspace.openFile(fileURL.path) if !success { print("Failed to open file: \(fileURL.path)") } } openTextFile(atPath: "/Users/username/Documents/sample.txt") ``` I have verified that the file path is correct and the file exists at that location. I also tried using `NSWorkspace.shared.open()` with a URL, but the same question occurs. Hereโ€™s the code for that: ```swift let fileURL = URL(fileURLWithPath: "/Users/username/Documents/sample.txt") NSWorkspace.shared.open(fileURL) ``` Both methods return false or cause the behavior mentioned above. I've also checked the file permissions and they seem fine. Is there something I've overlooked or a change in behavior with macOS 14? Any help would be appreciated! This issue appeared after updating to Swift 3.11. Thanks for taking the time to read this!