Search code examples
swiftcocoansurlfindernsworkspace

How do I query for Finder NSURL?


What is the correct way to determine the NSURL of Finder.app on any macOS system?

Here's the context. I have the following line of code in my app:

NSWorkspace.shared.openFile(pathToTempFile, withApplication: "Finder")

which causes the Swift compiler to warn me:

'openFile(_:withApplication:)' was deprecated in macOS 11.0: Use -[NSWorkspace openURLs:withApplicationAtURL:configuration:completionHandler:] instead.

The problem with switching to the newer API is that I don't know how to determine the withApplicationAtURL parameter for Finder. Obviously, I can locate the path to Finder on my own system, but I don't know if that is the correct path for all macOS systems, let alone for future versions of macOS. I'd just be hardcoding what works for me.

I cannot find any API in the Apple docs or from searching the web that would give me the URL of Finder.app on that system. What can I do?


Solution

  • NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.Finder")
    // $R1: Foundation.URL? = "file:///System/Library/CoreServices/Finder.app/"