使用Swift在Finder中显示文件夹的内容 [英] Show folder's contents in finder using Swift
问题描述
我希望能够选择一个文件夹并在Finder中显示其内容。我设法选择了文件夹本身,并在该文件夹中选择了一个文件。但是我不知道如何显示空文件夹的内容。
I want to be able to select a folder and show its contents in the Finder. I have managed to select the folder itself and select a file within the folder. But I don't know how to show the contents of an empty folder.
例如
文件夹A /文件夹B
Folder A/Folder B
我要显示文件夹B的内容(可以为空)。
I want to display the contents of folder Folder B (which could be empty).
I已编写以下代码:
func showFolder(fileName : String)
{
var dataPath = homeDirectory.stringByAppendingPathComponent(fileName)
var urlPath = NSURL(fileURLWithPath: dataPath)
var selectedURLs = [urlPath!]
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(selectedURLs)
}
这只会打开文件夹A,突出显示文件夹B。这是非常接近的,但不太正确。
This only opens Folder A with Folder B highlighted. This is very close, but not quite right.
我需要能够打开文件夹B而不会突出显示任何内容。我显然使用了错误的命令。
I need to be able to open Folder B with nothing highlighted. I'm obviously using the wrong command.
推荐答案
使用 selectFile
方法并传递 nil
作为第一个参数,指向文件夹的路径显示为第二个参数。
Use the selectFile
method and pass nil
as first argument and the path to the folder to be shown as second argument.
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: "/Users/")
这篇关于使用Swift在Finder中显示文件夹的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!