【发布时间】:2012-05-30 03:59:42
【问题描述】:
【问题讨论】:
-
"如何弹出该目录的查找窗口"?
标签: objective-c macos cocoa finder
【问题讨论】:
标签: objective-c macos cocoa finder
NSArray *fileURLs = [NSArray arrayWithObjects:fileURL1, /* ... */ nil];
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];
【讨论】:
你可以像这样使用NSWorkspace方法-selectFile:inFileViewerRootedAtPath::
[[NSWorkspace sharedWorkspace] selectFile:fullPathString inFileViewerRootedAtPath:pathString];
【讨论】:
值得一提的是,欧文的方法仅适用于 osx 10.6 或更高版本(参考:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html)。
因此,如果您编写的东西要在老一代人身上运行,最好按照贾斯汀建议的方式来做,因为它还没有被弃用(还)。
【讨论】:
// 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()
}
}
【讨论】: