Launch Finder window with specific files selected

后端 未结 7 1354
执笔经年
执笔经年 2020-12-12 22:21

I\'m trying to programmatically launch an OS X Finder window from an Xcode project. I need the window to open to a specific folder and have specific files within that folder

相关标签:
7条回答
  • 2020-12-12 22:41

    Objective-C version:

    NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
    
    0 讨论(0)
  • 2020-12-12 22:42

    When opening a file at path:

    NSString* path = @"/Users/user/Downloads/my file"
    NSArray *fileURLs = [NSArray arrayWithObjects:[NSURL fileURLWithPath:path], nil];
    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
    
    0 讨论(0)
  • 2020-12-12 22:46
    $ open -R <path-to-reveal>
    
    0 讨论(0)
  • 2020-12-12 22:51

    Swift 3.2/4.0 Version:

    NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])
    
    0 讨论(0)
  • 2020-12-12 22:57

    Swift version:

    let paths = ["/Users/peter/foo/bar.json"]
    let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
    NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)
    
    0 讨论(0)
  • 2020-12-12 23:01

    I'm finding that activateFileViewerSelectingURLs is not working on Yosemite (at least when in separate space from Finder). It will cause a switch to the Finder's space but won't seem to select the URL. Using:

    - `(BOOL)selectFile:(NSString *)fullPath inFileViewerRootedAtPath:(NSString *)rootFullPath` 
    

    will switch spaces from full screen app and select path.

    0 讨论(0)
提交回复
热议问题