Add a Save As location preference (#145)

Add a General preferences option controlling where the Save As panel
opens: the last used folder (default), the same folder as the
imported file, or a custom folder chosen by the user.
This commit is contained in:
mindprint authored and GitHub committed 2026-09-23 21:15:08 +02:00
1 parent 151530d6ab
commit c8fb950854
5 files changed
+172 -4

No files matched your search

+73
View File
@@ -23,6 +23,79 @@ class GeneralPrefsViewController: NSViewController {
return "GeneralPrefsViewController"
}
// MARK: Save As location popUp
@IBOutlet var saveLocationPopUp: NSPopUpButton!
private let customLocationTag = 2
override func viewDidLoad() {
super.viewDidLoad()
prepareSaveLocationPopUp()
}
private func menuItem(url: URL) -> NSMenuItem {
let icon = NSWorkspace.shared.icon(forFile: url.path)
icon.size = NSSize(width: 16, height: 16)
let item = NSMenuItem(title: url.lastPathComponent, action: #selector(setSaveLocation(_:)), keyEquivalent: "")
item.image = icon
item.target = self
item.representedObject = url
item.toolTip = url.path
item.tag = customLocationTag
return item
}
/// Rebuilds the custom folder item, then selects the current location.
private func prepareSaveLocationPopUp() {
guard let menu = saveLocationPopUp.menu else { return }
if let item = menu.items.first(where: { $0.tag == customLocationTag }) {
menu.removeItem(item)
}
if let url = Prefs.saveAsCustomLocation {
menu.insertItem(menuItem(url: url), at: 2)
}
selectCurrentSaveLocation()
}
private func selectCurrentSaveLocation() {
let location = Prefs.saveAsLocation
if location == .custom && Prefs.saveAsCustomLocation == nil {
saveLocationPopUp.selectItem(withTag: SaveAsLocation.lastUsed.rawValue)
} else {
saveLocationPopUp.selectItem(withTag: location.rawValue)
}
}
@IBAction func setSaveLocation(_ sender: NSMenuItem) {
if let url = sender.representedObject as? URL {
Prefs.saveAsCustomLocation = url
Prefs.saveAsLocation = .custom
} else {
Prefs.saveAsLocation = SaveAsLocation(rawValue: sender.tag) ?? .lastUsed
}
}
@IBAction func chooseSaveLocation(_ sender: Any) {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseFiles = false
panel.canChooseDirectories = true
panel.canCreateDirectories = true
panel.begin { response in
if response == .OK, let url = panel.url {
Prefs.saveAsCustomLocation = url
Prefs.saveAsLocation = .custom
}
self.prepareSaveLocationPopUp()
}
}
@IBAction func clearRecentSearches(_ sender: Any) {
MetadataSearchController.clearRecentSearches()
}
+4
View File
@@ -91,6 +91,10 @@ extension MP42File {
}
}
func preferredFileDirectory() -> URL? {
return tracks.compactMap { $0.url }.first?.deletingLastPathComponent()
}
func formattedFileName() -> String? {
guard let mediaKindValue = metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyMediaKind).first?.numberValue?.intValue,
let mediaKind = MediaKind(rawValue: mediaKindValue)
+27 -1
View File
@@ -70,6 +70,16 @@ struct StoredCodable<T: Codable> : Registable {
}
}
/// The folder the Save As… panel opens at.
enum SaveAsLocation: Int {
/// The last folder used, the standard AppKit behaviour.
case lastUsed = 0
/// The folder holding the file the tracks were imported from.
case sameAsFile = 1
/// A folder picked by the user.
case custom = 2
}
enum Prefs {
static func register() {
@@ -78,7 +88,7 @@ enum Prefs {
_audioBitrate, _audioDRC, _audioConvertAC3, _audioKeepAC3, _audioConvertDts,
_audioDtsOptions, _subtitleConvertBitmap, _ratingsCountry, _chaptersPreviewPosition,
_chaptersPreviewTrack, _mp464bitOffset, _mp464bitTimes, _mp4SaveAsOptimize, _forceHvc1,
_logFormat])
_logFormat, _saveAsLocationValue])
}
@Stored(key: "NSApplicationCrashOnException", defaultValue: true)
@@ -161,6 +171,22 @@ enum Prefs {
@Stored(key: "SBLogFormat", defaultValue: 0)
static var logFormat: Int // 0 = Time Only, 1 = Date and Time
@Stored(key: "SBSaveAsLocationMode", defaultValue: 0)
private static var saveAsLocationValue: Int
static var saveAsLocation: SaveAsLocation {
get { SaveAsLocation(rawValue: saveAsLocationValue) ?? .lastUsed }
set { saveAsLocationValue = newValue.rawValue }
}
private static let saveAsCustomLocationKey = "SBSaveAsCustomLocation"
/// The folder used when saveAsLocation is set to custom.
static var saveAsCustomLocation: URL? {
get { ud.url(forKey: saveAsCustomLocationKey) }
set { ud.set(newValue, forKey: saveAsCustomLocationKey) }
}
}
enum MetadataPrefs {
+17
View File
@@ -55,6 +55,19 @@ final class SaveOptions: NSViewController {
}
}
/// The folder the save panel should open at, or nil to leave it
/// at the last folder used.
private func preferredDirectory(for doc: Document) -> URL? {
switch Prefs.saveAsLocation {
case .lastUsed:
return nil
case .sameAsFile:
return doc.fileURL?.deletingLastPathComponent() ?? doc.mp4.preferredFileDirectory()
case .custom:
return Prefs.saveAsCustomLocation
}
}
override func viewDidLoad() {
super.viewDidLoad()
@@ -70,6 +83,10 @@ final class SaveOptions: NSViewController {
savePanel?.nameFieldStringValue = filename
}
if let directory = preferredDirectory(for: doc) {
savePanel?.directoryURL = directory
}
setFileType(filenameExtension: fileType)
_64bit_data.state = Prefs.mp464bitOffset ? .on : .off