diff --git a/Boop/Boop/AppDelegate.swift b/Boop/Boop/AppDelegate.swift index bff23474..5786418e 100644 --- a/Boop/Boop/AppDelegate.swift +++ b/Boop/Boop/AppDelegate.swift @@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var closePickerMenuItem: NSMenuItem! @IBOutlet weak var popoverViewController: PopoverViewController! + @IBOutlet weak var mainViewController: MainViewController! @IBOutlet weak var scriptManager: ScriptManager! @IBOutlet weak var editor: SyntaxTextView! @@ -42,6 +43,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } + func application(_ sender: NSApplication, openFile filename: String) -> Bool { + + let text=try? String(contentsOf: URL(fileURLWithPath: filename)) + mainViewController.setText(text ?? "Failed to load: " + filename) + return true + } + @IBAction func showPreferencesWindow(_ sender: NSMenuItem) { let controller = NSStoryboard.init(name: "Preferences", bundle: nil).instantiateInitialController() as? NSWindowController diff --git a/Boop/Boop/Boop.entitlements b/Boop/Boop/Boop.entitlements index 6abec674..98432a10 100644 --- a/Boop/Boop/Boop.entitlements +++ b/Boop/Boop/Boop.entitlements @@ -6,7 +6,7 @@ com.apple.security.files.bookmarks.app-scope - com.apple.security.files.user-selected.read-only + com.apple.security.files.user-selected.read-write com.apple.security.network.client diff --git a/Boop/Boop/BoopRelease.entitlements b/Boop/Boop/BoopRelease.entitlements index 6abec674..98432a10 100644 --- a/Boop/Boop/BoopRelease.entitlements +++ b/Boop/Boop/BoopRelease.entitlements @@ -6,7 +6,7 @@ com.apple.security.files.bookmarks.app-scope - com.apple.security.files.user-selected.read-only + com.apple.security.files.user-selected.read-write com.apple.security.network.client diff --git a/Boop/Boop/Controllers/MainViewController.swift b/Boop/Boop/Controllers/MainViewController.swift index 762b7f49..fc6df743 100644 --- a/Boop/Boop/Controllers/MainViewController.swift +++ b/Boop/Boop/Controllers/MainViewController.swift @@ -48,25 +48,76 @@ class MainViewController: NSViewController { } @IBAction func clear(_ sender: Any) { + setText("") + } + + public func setText(_ text: String) { let textView = editorView.contentTextView textView.textStorage?.beginEditing() let range = NSRange(location: 0, length: textView.textStorage?.length ?? textView.string.count) - guard textView.shouldChangeText(in: range, replacementString: "") else { + guard textView.shouldChangeText(in: range, replacementString: text) else { return } - textView.textStorage?.replaceCharacters(in: range, with: "") + textView.textStorage?.replaceCharacters(in: range, with: text) textView.textStorage?.endEditing() textView.didChangeText() } - + @IBAction func checkForUpdates(_ sender: Any) { updateBuddy.check() } + + @IBAction func openFile(sender: AnyObject) { + + let dialog = NSOpenPanel(); + + dialog.title = "Choose a file"; + dialog.showsResizeIndicator = true; + dialog.showsHiddenFiles = false; + dialog.canChooseDirectories = false; + dialog.canCreateDirectories = false; + dialog.allowsMultipleSelection = false; + + if (dialog.runModal() == NSApplication.ModalResponse.OK) { + if let pathUrl = dialog.url { // Pathname of the file + let text=try? String(contentsOf: pathUrl) + setText(text ?? "Failed to load file '\(pathUrl.path)'.") + } + } + + } + + @IBAction func saveFileAs(sender: AnyObject) { + + let dialog = NSSavePanel(); + + dialog.title = "Save content as…"; + dialog.showsResizeIndicator = true; + dialog.showsHiddenFiles = false; + dialog.showsTagField = false; + dialog.canCreateDirectories = true; + dialog.nameFieldStringValue = "Untitled.txt" + + if (dialog.runModal() == NSApplication.ModalResponse.OK) { + if let pathUrl = dialog.url { // Pathname of the file + let textView = editorView.contentTextView + let textData=textView.textStorage?.string.data(using: .utf8) + do { + try textData?.write(to: pathUrl) + } catch let error as NSError { + setText("Failed to save content to file '\(pathUrl.path)'. (Reason: \(error.localizedFailureReason!))") + // TODO: Show error in alert box + } + } + } + + } + } extension MainViewController: SyntaxTextViewDelegate { diff --git a/Boop/Boop/Info.plist b/Boop/Boop/Info.plist index 5753c31a..72579964 100644 --- a/Boop/Boop/Info.plist +++ b/Boop/Boop/Info.plist @@ -53,5 +53,16 @@ + CFBundleDocumentTypes + + + CFBundleTypeRole + Viewer + CFBundleTypeExtensions + + * + + + diff --git a/Boop/UI/Base.lproj/MainMenu.xib b/Boop/UI/Base.lproj/MainMenu.xib index f132c070..a1172a30 100644 --- a/Boop/UI/Base.lproj/MainMenu.xib +++ b/Boop/UI/Base.lproj/MainMenu.xib @@ -55,6 +55,7 @@ + @@ -128,9 +129,9 @@ -