В моем приложении я хотел бы создать кнопку "show in finder". Мне удалось выяснить, как открыть окно поиска в этом каталоге, но не выяснили, как выделить файл, как это делает ОС.
Возможно ли это?
В моем приложении я хотел бы создать кнопку "show in finder". Мне удалось выяснить, как открыть окно поиска в этом каталоге, но не выяснили, как выделить файл, как это делает ОС.
Возможно ли это?
NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
украдено из Запустить окно поиска OSX с выбранными выбранными файлами
Вы можете использовать метод NSWorkspace
-selectFile:inFileViewerRootedAtPath:
следующим образом:
[[NSWorkspace sharedWorkspace] selectFile:fullPathString inFileViewerRootedAtPath:pathString];
Стоит отметить, что метод owen работает только с osx 10.6 или более поздней версии (Ref: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html).
Итак, если вы пишете что-то для старших поколений, то, вероятно, лучше сделать это так, как это было предложено Justin, так как его не устарели (пока).
// Place the following code within your Document subclass
// enable or disable the menu item called "Show in Finder"
override func validateUserInterfaceItem(anItem: NSValidatedUserInterfaceItem) -> Bool {
if anItem.action() == #selector(showInFinder) {
return self.fileURL?.path != nil;
} else {
return super.validateUserInterfaceItem(anItem)
}
}
// action for the "Show in Finder" menu item, etc.
@IBAction func showInFinder(sender: AnyObject) {
func showError() {
let alert = NSAlert()
alert.messageText = "Error"
alert.informativeText = "Sorry, the document couldn't be shown in the Finder."
alert.runModal()
}
// if the path isn't known, then show an error
let path = self.fileURL?.path
guard path != nil else {
showError()
return
}
// try to select the file in the Finder
let workspace = NSWorkspace.sharedWorkspace()
let selected = workspace.selectFile(path!, inFileViewerRootedAtPath: "")
if !selected {
showError()
}
}