NSOutlineViewInSwift icon indicating copy to clipboard operation
NSOutlineViewInSwift copied to clipboard

Set root to anything other than '/' and adding selection

Open kimaldis opened this issue 8 years ago • 5 comments

Thanks for this example, saved a newcomer to OS X dev a lot of grief.

I've a couple of questions, if you wouldn't mind: I changed Filesystem.rootItem to something other than '/' and I lost the file tree in the view. Any idea what I'm missing?

I'd like to add selection. Something that allows me to run something when items in the Outline are clicked on. Any thoughts?

Again, thanks.

kimaldis avatar Nov 20 '16 18:11 kimaldis

I've figured out the selection thing, using a NSOutlineView action seems to do the trick:

@IBAction func OutlineViewClicked(_ sender: NSOutlineView) {
    
      let item = sender.item(atRow: sender.clickedRow) as!FileSystemItem
      let fullPath = item.fullPath()
 
      print( "Path:  \(fullPath)")
}

kimaldis avatar Nov 20 '16 19:11 kimaldis

I've figured out why my changing rootItem wasn't working. If I make rootItem, say, "/Users" then the line self.relativePath = path.lastPathComponent.copy() as! String

in init() sets relative path to "Users" and nothing gets found.

My rather messy solution is the following lines after self.relativePath is set:

    if self.relativePath == "Users" {
        self.relativePath = "/Users"
    }

I'm sure there's a more elegant way to do this but for now it's working.

kimaldis avatar Nov 20 '16 21:11 kimaldis

Thanks for the kind words. Pleased to hear that this project was useful for you. Also pleased to see that you have found workarounds for your original questions (sorry I didn't get back to you first).

Out of curiosity, did you have to upgrade the code to Swift version 2.3 or 3.0 before it would work? Or are you using a version of Xcode earlier than 8.0? I've been meaning to update to the most recent version of Swift but haven't done so yet.

If you would like to contribute any of your updates back into the project I'll happily accept a pull request (I haven't accepted a pull request before but happy to try and figure it out if you are interested).

Thanks Daniel.

danielpi avatar Nov 21 '16 02:11 danielpi

Xcode 8 converted it to Swift 3.0 for me with no issues at all. It's nice to find code that does that.

I have a more elegant fix. Add the following init override:

override init() {
     self.relativePath = "/Library" // or whatever full path to root of display
     self.parent = nil
}

and alter the rootItem definition:

class var rootItem: FileSystemItem {
     get {
         return FileSystemItem( )
     }
}

To catch selectinon, create an Action linked from the NSOutlineView in ViewController and add code :

@IBAction func OutlineViewClicked(_ sender: NSOutlineView) {
    
      let item = sender.item(atRow: sender.clickedRow) as!FileSystemItem
      let fullPath = item.fullPath()
       print( "Path Selected =  \(fullPath)")
      //let iRow = sender.selectedRow 
 }

I'll happily contribute, although I've never done so on Github. Or make the changes yourself. Whichever is easiest.

kimaldis avatar Nov 21 '16 08:11 kimaldis

One other thing, if it's useful. You can filter out hidden files starting with periods with the following in the for contents in array loop

if contents[contents.startIndex] != "." {   // don't show files starting with a period
    let newChild = FileSystemItem(path: contents as NSString, parent: self)
        newChildren.append(newChild)
    }
}

something similar should work for filtering file types or files with particular extensions.

kimaldis avatar Nov 21 '16 08:11 kimaldis