2012-05-23 12 views
17

में 'खोजक में दिखाएं' बटन को कार्यान्वित करना मेरे आवेदन में मैं 'खोजक में दिखाएं' बटन बनाना चाहता हूं। मैं यह पता लगाने में सक्षम हूं कि उस निर्देशिका की खोजक विंडो को कैसे पॉप अप करना है, लेकिन यह पता नहीं लगाया गया है कि ओएस की तरह फ़ाइल को हाइलाइट कैसे किया जाए।उद्देश्य सी

क्या यह संभव है?

+0

"उस निर्देशिका की खोजक विंडो को कैसे पॉप अप करें"? – onmyway133

+0

[चयनित फ़ाइलों के साथ लॉन्च ओएसएक्स खोजक विंडो] का संभावित डुप्लिकेट [http://stackoverflow.com/questions/7652928/launch-osx-finder-window-with- विशिष्ट- फाइल- चयन) – Eonil

उत्तर

15

चोरी आप इस तरह NSWorkspace विधि -selectFile:inFileViewerRootedAtPath: उपयोग कर सकते हैं:

[[NSWorkspace sharedWorkspace] selectFile:fullPathString inFileViewerRootedAtPath:pathString]; 
+0

त्रुटि -600 मेरे लिए बढ़ रहा है (ओएसएक्स 10.6.5) – user23790

3

इसके लायक उल्लेख है कि अनिल के विधि केवल OSX 10.6 या बाद से काम करता है (रेफरी: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html)।

तो यदि आपकी पीढ़ी पुरानी पीढ़ियों पर चलने के लिए कुछ लिखना संभवतः जस्टिन द्वारा सुझाए गए तरीके से ऐसा करने के लिए बेहतर है क्योंकि इसे हटा दिया गया है (अभी तक)।

+2

केवल इसे एक उत्तर के रूप में जोड़ा क्योंकि मैं अभी तक सामान पर "टिप्पणी" नहीं कर सकता – andrewktmeikle

0
// 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() 
    } 

}