Show folder's contents in finder using Swift

一世执手 提交于 2019-12-01 03:14:11

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 2.1 code to Launch OS X Finder

Use the selectFile or activateFileViewerSelectingURLs to select files.

Select 1 item in finder with path YOUR_PATH_STRING

NSWorkspace.sharedWorkspace().selectFile(YOUR_PATH_STRING, inFileViewerRootedAtPath: "")

The second param use empty string, if you specify an empty string "" for this parameter, the file is selected in the main viewer.


If you want to select 1 or more files use activateFileViewerSelectingURLs(_ fileURLs: [NSURL])

To select one file

NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs([NSURL].init(arrayLiteral: NSURL.init(fileURLWithPath: YOUR_PATH_STRING)))

To select multiple files

let urls : [NSURL] = [NSURL.init(fileURLWithPath: "/Users/USER_NAME/Pictures"),
                      NSURL.init(fileURLWithPath: "/Users/USER_NAME/Music")]

If you provide item that are not in the same folder more windows selecting the specified files are open.

let urls : [NSURL] = [NSURL.init(fileURLWithPath: "/Users/USER_NAME/Pictures"),
                      NSURL.init(fileURLWithPath: "/Users/USER_NAME/Music"),
                      NSURL.init(fileURLWithPath: "/Users")]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!