Skip to content

Instantly share code, notes, and snippets.

@erica
Last active June 28, 2020 14:52
Show Gist options
  • Select an option

  • Save erica/e83485ae85e497c145d28cfa140a0258 to your computer and use it in GitHub Desktop.

Select an option

Save erica/e83485ae85e497c145d28cfa140a0258 to your computer and use it in GitHub Desktop.

Revisions

  1. erica revised this gist Apr 14, 2016. 1 changed file with 5 additions and 15 deletions.
    20 changes: 5 additions & 15 deletions 00-dragndrop.swift
    Original file line number Diff line number Diff line change
    @@ -6,24 +6,17 @@ public extension String { public var ns: NSString {return self as NSString} }
    /// Custom Labeled Playground-Based Drag-and-Drop window
    public class DropView: NSTextField {

    // Default action handler
    public var handler: ([String]) -> Void = { paths in Swift.print(paths) }

    // Drag and drop notification
    public override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func performDragOperation(sender: NSDraggingInfo) -> Bool {
    let pboard = sender.draggingPasteboard()
    let note = NSNotification(name: String(ObjectIdentifier(self).hashValue), object: pboard)
    NSNotificationCenter.defaultCenter().postNotification(note)
    return true
    }

    // Default action handler
    public var handler: ([String]) -> Void = { paths in Swift.print(paths) }

    // Fetch file names for handler
    @objc private func handle(note: NSNotification) {
    guard let pasteboard = note.object as? NSPasteboard else { return }
    guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return }
    guard let paths = pboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return false }
    handler(paths)
    return true
    }

    public required init?(coder: NSCoder) { super.init(coder: coder) }
    @@ -37,8 +30,5 @@ public class DropView: NSTextField {

    // Register for file name drags
    registerForDraggedTypes([NSFilenamesPboardType])

    // Convey drags to handler
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handle(_:)), name: String(ObjectIdentifier(self).hashValue), object: nil)
    }
    }
  2. erica revised this gist Apr 13, 2016. No changes.
  3. erica revised this gist Apr 13, 2016. No changes.
  4. erica revised this gist Apr 13, 2016. No changes.
  5. erica revised this gist Apr 13, 2016. No changes.
  6. erica revised this gist Apr 13, 2016. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions 00-dragndrop.swift
    Original file line number Diff line number Diff line change
    @@ -7,25 +7,22 @@ public extension String { public var ns: NSString {return self as NSString} }
    public class DropView: NSTextField {

    // Drag and drop notification
    public override func draggingEntered(
    sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingUpdated(
    sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingEntered(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingUpdated(sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func performDragOperation(sender: NSDraggingInfo) -> Bool {
    let pboard = sender.draggingPasteboard()
    let note = NSNotification(name: String(ObjectIdentifier(self).hashValue), object: pboard)
    NSNotificationCenter.defaultCenter().postNotification(note)
    return true
    }

    // Action handler
    // Default action handler
    public var handler: ([String]) -> Void = { paths in Swift.print(paths) }

    // Fetch file names for handler
    @objc private func handle(note: NSNotification) {
    guard let pasteboard = note.object as? NSPasteboard else { return }
    guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType)
    as? [String] else { return }
    guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType) as? [String] else { return }
    handler(paths)
    }

    @@ -42,9 +39,6 @@ public class DropView: NSTextField {
    registerForDraggedTypes([NSFilenamesPboardType])

    // Convey drags to handler
    NSNotificationCenter.defaultCenter()
    .addObserver(self, selector: #selector(handle(_:)),
    name: String(ObjectIdentifier(self).hashValue),
    object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handle(_:)), name: String(ObjectIdentifier(self).hashValue), object: nil)
    }
    }
  7. erica revised this gist Apr 13, 2016. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions 00-dragndrop.swift
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,7 @@ public class DropView: NSTextField {
    // Presentation
    stringValue = "Drop Here"; editable = false
    font = NSFont(name: "Palatino", size: 48.0); alignment = .Center

    // Background
    wantsLayer = true
    layer?.backgroundColor = NSColor.whiteColor().CGColor
    wantsLayer = true; layer?.backgroundColor = NSColor.whiteColor().CGColor

    // Register for file name drags
    registerForDraggedTypes([NSFilenamesPboardType])
  8. erica revised this gist Apr 13, 2016. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions 00-dragndrop.swift
    Original file line number Diff line number Diff line change
    @@ -34,13 +34,12 @@ public class DropView: NSTextField {
    super.init(frame: frameRect)

    // Presentation
    (stringValue, editable, font) = ("Drop Here", false, NSFont(name: "Palatino", size: 48.0))
    stringValue = "Drop Here"; editable = false
    font = NSFont(name: "Palatino", size: 48.0); alignment = .Center

    // Background
    wantsLayer = true
    layer?.backgroundColor = NSColor.whiteColor().CGColor
    layer?.borderWidth = 2.0
    layer?.cornerRadius = 4.0

    // Register for file name drags
    registerForDraggedTypes([NSFilenamesPboardType])
    @@ -51,4 +50,4 @@ public class DropView: NSTextField {
    name: String(ObjectIdentifier(self).hashValue),
    object: nil)
    }
    }
    }
  9. erica revised this gist Apr 13, 2016. No changes.
  10. erica created this gist Apr 13, 2016.
    54 changes: 54 additions & 0 deletions 00-dragndrop.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    import Cocoa

    // Support Foundation calls on String
    public extension String { public var ns: NSString {return self as NSString} }

    /// Custom Labeled Playground-Based Drag-and-Drop window
    public class DropView: NSTextField {

    // Drag and drop notification
    public override func draggingEntered(
    sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func draggingUpdated(
    sender: NSDraggingInfo) -> NSDragOperation { return NSDragOperation.Copy }
    public override func performDragOperation(sender: NSDraggingInfo) -> Bool {
    let pboard = sender.draggingPasteboard()
    let note = NSNotification(name: String(ObjectIdentifier(self).hashValue), object: pboard)
    NSNotificationCenter.defaultCenter().postNotification(note)
    return true
    }

    // Action handler
    public var handler: ([String]) -> Void = { paths in Swift.print(paths) }

    // Fetch file names for handler
    @objc private func handle(note: NSNotification) {
    guard let pasteboard = note.object as? NSPasteboard else { return }
    guard let paths = pasteboard.propertyListForType(NSFilenamesPboardType)
    as? [String] else { return }
    handler(paths)
    }

    public required init?(coder: NSCoder) { super.init(coder: coder) }
    public override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)

    // Presentation
    (stringValue, editable, font) = ("Drop Here", false, NSFont(name: "Palatino", size: 48.0))

    // Background
    wantsLayer = true
    layer?.backgroundColor = NSColor.whiteColor().CGColor
    layer?.borderWidth = 2.0
    layer?.cornerRadius = 4.0

    // Register for file name drags
    registerForDraggedTypes([NSFilenamesPboardType])

    // Convey drags to handler
    NSNotificationCenter.defaultCenter()
    .addObserver(self, selector: #selector(handle(_:)),
    name: String(ObjectIdentifier(self).hashValue),
    object: nil)
    }
    }
    18 changes: 18 additions & 0 deletions 01-DropWordCountExample.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import Cocoa
    import XCPlayground

    // Establish drop view
    let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200))
    dropView.stringValue = "Drop text files here for word counts"
    dropView.handler = {
    print("Word Counts")
    for path in $0 {
    guard let string = try? String(contentsOfFile: path) else { continue }
    print(path.ns.lastPathComponent, ":",
    string.ns.componentsSeparatedByString(" ").count)
    }
    }

    // Run live
    XCPlaygroundPage.currentPage.liveView = dropView
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
    20 changes: 20 additions & 0 deletions 02-DropPictureExample.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    import Cocoa
    import XCPlayground

    // Establish drop view
    let dropView = DropView(frame: NSRect(x: 0, y: 0, width: 600, height: 200))
    dropView.stringValue = "Drop Pictures"
    dropView.handler = {
    // Print out each file name and image size
    $0.forEach {
    if let image = NSImage(contentsOfFile: $0) {
    Swift.print($0.ns.lastPathComponent, image.size)
    } else {
    Swift.print($0.ns.lastPathComponent, "is not an image")
    }
    }
    }

    // Run live
    XCPlaygroundPage.currentPage.liveView = dropView
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true