Launch OSX Finder window with specific files selected

只谈情不闲聊 提交于 2019-12-17 21:50:39

问题


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

Does anyone know how to do this in either objective c, applescript, or Finder command-line parameters?

Thanks!


回答1:


Objective-C version:

NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];



回答2:


$ open -R <path-to-reveal>



回答3:


Another AppleScript flavor - the Finder's reveal command will both open a window to the containing folder and select the item(s). If there are multiple containing folders, multiple Finder windows will be opened.

tell application "Finder" 
   to reveal {someAlias, "path/to/POSIXfile" as POSIX file, etc}



回答4:


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.




回答5:


Swift version:

    let paths = ["/Users/peter/foo/bar.json"]
    let fileURLs = paths.map{ NSURL(fileURLWithPath: $0)}
    NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(fileURLs)



回答6:


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];



回答7:


Swift 3.2/4.0 Version: NSWorkspace.shared.activateFileViewerSelecting([outputFileURL])



来源:https://stackoverflow.com/questions/7652928/launch-osx-finder-window-with-specific-files-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!