Open examples as templates
This commit is contained in:
parent
f560ce747b
commit
e905f08f76
|
@ -57,9 +57,10 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
||||||
menuItem!.tag = index
|
menuItem!.tag = index
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func applicationShouldOpenUntitledFile(sender: NSApplication) -> Bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
func applicationWillTerminate(aNotification: NSNotification) {
|
func applicationWillTerminate(aNotification: NSNotification) {
|
||||||
// Insert code here to tear down your application
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func menuNeedsUpdate(menu: NSMenu) {
|
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) {
|
@IBAction func openSketch(item: NSMenuItem) {
|
||||||
let url = NSURL(fileURLWithPath: sketches[item.tag])!
|
let url = NSURL(fileURLWithPath: sketches[item.tag])!
|
||||||
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
|
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
|
||||||
|
@ -94,8 +131,25 @@ class ASApplication: NSObject, NSApplicationDelegate, NSMenuDelegate {
|
||||||
|
|
||||||
@IBAction func openExample(item: NSMenuItem) {
|
@IBAction func openExample(item: NSMenuItem) {
|
||||||
let url = NSURL(fileURLWithPath: examples[item.tag])!
|
let url = NSURL(fileURLWithPath: examples[item.tag])!
|
||||||
let doc = NSDocumentController.sharedDocumentController() as NSDocumentController
|
openTemplate(url.URLByDeletingLastPathComponent!)
|
||||||
doc.openDocumentWithContentsOfURL(url, display: true) { (doc, alreadyOpen, error) -> Void in
|
}
|
||||||
|
|
||||||
|
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!)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,17 +15,33 @@ class ASSketchBook {
|
||||||
case SketchDir(String, [SketchBookItem])
|
case SketchDir(String, [SketchBookItem])
|
||||||
}
|
}
|
||||||
|
|
||||||
private class func enumerateSketches(path: NSString) -> SketchBookItem {
|
class func findSketch(path: NSString) -> SketchBookItem {
|
||||||
let fileManager = NSFileManager.defaultManager()
|
let fileManager = NSFileManager.defaultManager()
|
||||||
|
var inoSketch = SketchBookItem.Nothing
|
||||||
let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as [String]
|
let contents = fileManager.contentsOfDirectoryAtPath(path, error: nil) as [String]
|
||||||
for item in contents {
|
for item in contents {
|
||||||
switch item.pathExtension {
|
switch item.pathExtension {
|
||||||
case "ino", "avrsackproj":
|
case "avrsackproj":
|
||||||
return .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item))
|
return .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item))
|
||||||
|
case "ino":
|
||||||
|
inoSketch = .Sketch(path.lastPathComponent, path.stringByAppendingPathComponent(item))
|
||||||
default:
|
default:
|
||||||
break
|
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]()
|
var sketches = [SketchBookItem]()
|
||||||
for item in contents {
|
for item in contents {
|
||||||
let subpath = path.stringByAppendingPathComponent(item)
|
let subpath = path.stringByAppendingPathComponent(item)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user