iph*_*aaw 10 directory finder nsworkspace swift
我希望能够选择一个文件夹并在Finder中显示其内容.我已设法选择文件夹本身并选择文件夹中的文件.但我不知道如何显示空文件夹的内容.
例如
文件夹A /文件夹B.
我想显示文件夹B的内容(可能是空的).
我写了以下代码:
func showFolder(fileName : String)
{
var dataPath = homeDirectory.stringByAppendingPathComponent(fileName)
var urlPath = NSURL(fileURLWithPath: dataPath)
var selectedURLs = [urlPath!]
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(selectedURLs)
}
Run Code Online (Sandbox Code Playgroud)
这仅打开文件夹A突出显示文件夹B. 这非常接近,但并不完全正确.
我需要能够打开文件夹B,没有任何突出显示.我显然使用了错误的命令.
And*_*Ley 24
使用该selectFile
方法并传递nil
为第一个参数,并将文件夹的路径显示为第二个参数.
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: "/Users/")
Run Code Online (Sandbox Code Playgroud)
And*_*rew 19
2020 | 斯威夫特 5.1:
func showInFinder(url: URL?) {
guard let url = url else { return }
if url.isDirectory {
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
}
else {
showInFinderAndSelectLastComponent(of: url)
}
}
fileprivate func showInFinderAndSelectLastComponent(of url: URL) {
NSWorkspace.shared.activateFileViewerSelecting([url])
}
Run Code Online (Sandbox Code Playgroud)
显示查找器:
文件夹的 url = 将显示文件夹的内容。
文件的 url = 将在 Finder 文件的父文件中打开并在那里选择文件。
网址为零 = 什么都不做
文件/路径不存在 = 什么也不做
Swift 2.1代码启动OS X Finder
使用selectFile
或activateFileViewerSelectingURLs
选择文件.
在Finder中选择1个项目,路径为YOUR_PATH_STRING
NSWorkspace.sharedWorkspace().selectFile(YOUR_PATH_STRING, inFileViewerRootedAtPath: "")
Run Code Online (Sandbox Code Playgroud)
第二个参数使用空字符串,如果为此参数指定空字符串"",则在主查看器中选择该文件.
如果要选择1个或多个文件使用 activateFileViewerSelectingURLs(_ fileURLs: [NSURL])
选择一个文件
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs([NSURL].init(arrayLiteral: NSURL.init(fileURLWithPath: YOUR_PATH_STRING)))
Run Code Online (Sandbox Code Playgroud)
选择多个文件
let urls : [NSURL] = [NSURL.init(fileURLWithPath: "/Users/USER_NAME/Pictures"),
NSURL.init(fileURLWithPath: "/Users/USER_NAME/Music")]
Run Code Online (Sandbox Code Playgroud)
如果您提供的项目不在同一文件夹中,则会打开选择指定文件的更多窗口.
let urls : [NSURL] = [NSURL.init(fileURLWithPath: "/Users/USER_NAME/Pictures"),
NSURL.init(fileURLWithPath: "/Users/USER_NAME/Music"),
NSURL.init(fileURLWithPath: "/Users")]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4761 次 |
最近记录: |