diff --git a/AVRsack/ASApplication.swift b/AVRsack/ASApplication.swift index b89f6be..b395e71 100644 --- a/AVRsack/ASApplication.swift +++ b/AVRsack/ASApplication.swift @@ -57,9 +57,10 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { menuItem!.tag = index } } - + func applicationShouldOpenUntitledFile(sender: NSApplication) -> Bool { + return false + } func applicationWillTerminate(aNotification: NSNotification) { - // Insert code here to tear down your application } func menuNeedsUpdate(menu: NSMenu) { @@ -85,6 +86,42 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { } } + func openTemplate(template: NSURL) { + ASApplication.newProjectLocation(nil, + message: "Save editable copy of project \(template.lastPathComponent)") + { (saveTo) -> Void in + let oldName = template.lastPathComponent + let newName = saveTo.lastPathComponent + let fileManager = NSFileManager.defaultManager() + fileManager.copyItemAtURL(template, toURL: saveTo, error: nil) + let contents = fileManager.enumeratorAtURL(saveTo, + includingPropertiesForKeys: [NSURLNameKey, NSURLPathKey], + options: .SkipsHiddenFiles, errorHandler: nil) + while let item = contents?.nextObject() as? NSURL { + var renameItem = false + var itemName = item.lastPathComponent + if itemName.stringByDeletingPathExtension == oldName { + renameItem = true + itemName = newName.stringByAppendingPathExtension(itemName.pathExtension)! + } + if renameItem { + fileManager.moveItemAtURL(item, + toURL: item.URLByDeletingLastPathComponent!.URLByAppendingPathComponent(itemName), + error: nil) + } + } + let sketch = ASSketchBook.findSketch(saveTo.path!) + switch sketch { + case .Sketch(_, let path): + let doc = NSDocumentController.sharedDocumentController() as NSDocumentController + doc.openDocumentWithContentsOfURL(NSURL(fileURLWithPath: path)!, display: true) { (doc, alreadyOpen, error) -> Void in + } + default: + break + } + } + } + @IBAction func openSketch(item: NSMenuItem) { let url = NSURL(fileURLWithPath: sketches[item.tag])! let doc = NSDocumentController.sharedDocumentController() as NSDocumentController @@ -94,8 +131,25 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate { @IBAction func openExample(item: NSMenuItem) { let url = NSURL(fileURLWithPath: examples[item.tag])! - let doc = NSDocumentController.sharedDocumentController() as NSDocumentController - doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in + openTemplate(url.URLByDeletingLastPathComponent!) + } + + class func newProjectLocation(documentWindow: NSWindow?, message: String, completion: (NSURL) -> ()) { + let savePanel = NSSavePanel() + savePanel.allowedFileTypes = [kUTTypeFolder] + savePanel.message = message + if let window = documentWindow { + savePanel.beginSheetModalForWindow(window, completionHandler: { (returnCode) -> Void in + if returnCode == NSFileHandlingPanelOKButton { + completion(savePanel.URL!) + } + }) + } else { + savePanel.beginWithCompletionHandler({ (returnCode) -> Void in + if returnCode == NSFileHandlingPanelOKButton { + completion(savePanel.URL!) + } + }) } } } diff --git a/AVRsack/ASSketchBook.swift b/AVRsack/ASSketchBook.swift index b823800..6fc2bd7 100644 --- a/AVRsack/ASSketchBook.swift +++ b/AVRsack/ASSketchBook.swift @@ -15,17 +15,33 @@ class ASSketchBook { case SketchDir(String, [SketchBookItem]) } - private class func enumerateSketches(path: NSString) -> SketchBookItem { + class func findSketch(path: NSString) -> SketchBookItem { let fileManager = NSFileManager.defaultManager() + var inoSketch = SketchBookItem.Nothing let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as [String] for item in contents { switch item.pathExtension { - case "ino", "avrsackproj": + case "avrsackproj": return .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item)) + case "ino": + inoSketch = .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item)) default: break } } + return inoSketch + } + + private class func enumerateSketches(path: NSString) -> SketchBookItem { + let fileManager = NSFileManager.defaultManager() + let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as [String] + let sketch = findSketch(path) + switch sketch { + case .Sketch: + return sketch + default: + break + } var sketches = [SketchBookItem]() for item in contents { let subpath = path.stringByAppendingPathComponent(item)