Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic open/save functionality #331

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add "save content as" functionality
  • Loading branch information
Christof Rath committed Apr 22, 2022
commit 771855625d7e8df14bf15b9797b255b2a019af60
2 changes: 1 addition & 1 deletion Boop/Boop/Boop.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
Expand Down
2 changes: 1 addition & 1 deletion Boop/Boop/BoopRelease.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
Expand Down
35 changes: 30 additions & 5 deletions Boop/Boop/Controllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MainViewController: NSViewController {
updateBuddy.check()
}

@IBAction func browseFile(sender: AnyObject) {
@IBAction func openFile(sender: AnyObject) {

let dialog = NSOpenPanel();

Expand All @@ -84,10 +84,35 @@ class MainViewController: NSViewController {
dialog.allowsMultipleSelection = false;

if (dialog.runModal() == NSApplication.ModalResponse.OK) {
if let result = dialog.url { // Pathname of the file
let path = result.path
let text=try? String(contentsOf: URL(fileURLWithPath: path))
setText(text ?? "Failed to load: " + path)
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!))")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad move ;)

it replaces my "important" text and its deleted :/

// TODO: Show error in alert box
}
}
}

Expand Down
19 changes: 10 additions & 9 deletions Boop/UI/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
</menuItem>
<menuItem title="Open…" keyEquivalent="o" id="IAo-SY-fd9">
<connections>
<action selector="browseFileWithSender:" target="unv-pE-gme" id="KxG-Xy-LeB"/>
<action selector="openFileWithSender:" target="unv-pE-gme" id="l3F-jU-eYg"/>
</connections>
</menuItem>
<menuItem title="Open Recent" hidden="YES" id="tXI-mr-wws">
Expand All @@ -148,19 +148,15 @@
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="m54-Is-iLE"/>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
</connections>
</menuItem>
<menuItem title="Save…" hidden="YES" keyEquivalent="s" id="pxx-59-PXV">
<menuItem title="Save…" hidden="YES" id="pxx-59-PXV">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="saveDocument:" target="-1" id="teZ-XB-qJY"/>
</connections>
</menuItem>
<menuItem title="Save As…" hidden="YES" keyEquivalent="S" id="Bw7-FT-i3A">
<menuItem title="Save As…" keyEquivalent="s" id="Bw7-FT-i3A">
<connections>
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
<action selector="saveFileAsSender:" target="unv-pE-gme" id="vLF-0s-Nt1"/>
</connections>
</menuItem>
<menuItem title="Revert to Saved" hidden="YES" keyEquivalent="r" id="KaW-ft-85H">
Expand All @@ -180,6 +176,11 @@
<action selector="print:" target="-1" id="qaZ-4w-aoO"/>
</connections>
</menuItem>
<menuItem title="Close" keyEquivalent="w" id="DVo-aG-piG">
<connections>
<action selector="performClose:" target="-1" id="HmO-Ls-i7Q"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down