How do I programmatically open a folder in the Finder without selecting anything?

Viewed 3779

When the user clicks a button in my application I want to have the Finder come to the front and display the contents of a folder. The NSWorkspace class has two calls, activateFileViewerSelectingURLs(:) and selectFile(:inFileViewerRootedAtPath:), that almost do what I want, but they both select one or more items. I don't want the Finder to select anything.

I see the behavior I want if I enter

/usr/bin/open /path/to/my/folder

in Terminal. Is there a Cocoa API for doing this, or do I need to have NSTask run /usr/bin/open?

4 Answers
NSWorkspace.shared.open(URL(fileURLWithPath: "/your/path/goes/here"))

Note that you have to have read permissions for the path that you want to open.

Related