Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show folder's contents in finder using Swift

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.

e.g.

Folder A/Folder B

I want to display the contents of folder Folder B (which could be empty).

I have written the following code:

func showFolder(fileName : String)
{
    var dataPath = homeDirectory.stringByAppendingPathComponent(fileName)
    var urlPath = NSURL(fileURLWithPath: dataPath)
    var selectedURLs = [urlPath!]
    NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(selectedURLs)
}

This only opens Folder A with Folder B highlighted. This is very close, but not quite right.

I need to be able to open Folder B with nothing highlighted. I'm obviously using the wrong command.

like image 975
iphaaw Avatar asked Jun 09 '15 16:06

iphaaw


3 Answers

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/")
like image 84
Andreas Ley Avatar answered Oct 19 '22 13:10

Andreas Ley


2021 | SWIFT 5.1:

func showInFinder(url: URL?) {
    guard let url = url else { return }
    
    if url.isDirectory {
        NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
    } else {
        NSWorkspace.shared.activateFileViewerSelecting([url])
    }
}

extension URL {
    var isDirectory: Bool {
        return (try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
    }
}

showInFinder:

  • Folder's url = will show content of the folder.

  • File's url = will open in Finder file's parent and select file there.

  • Url is nil = Will do nothing

  • File/path does not exist = Will do nothing

like image 26
Andrew Avatar answered Oct 19 '22 15:10

Andrew


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")]
like image 10
Hugo Pereira Avatar answered Oct 19 '22 13:10

Hugo Pereira