Route AppleScript chapter imports and make File > Optimize work on MKV

importFilesDirectly mirrors the open panel's extension dispatch, so
AppleScript `import file` now sends .txt/.csv to addChapters/
updateChapters and everything else to FileImportController; the chapter
track created on an MKV doc gets the tags-only trackId/
updateChangeCount bookkeeping in didSelect(chapters:). searchChapters
moves to the always-valid validation group, though CLAUDE.md now
records that it is orphaned dead code with no UI entry point.

File > Optimize on a Matroska document forces writeMatroska's
structural branch: a full remuxer rebuild from the existing tracks,
the container-equivalent of atom-order optimize. Its validation uses
fileURL instead of hasFileRepresentation, which is always false for
tags-only models and kept the menu disabled forever.

Bump MP42Foundation for the tagsOnly importer fix that mirrors the
file's chapter atoms verbatim (offset first starts and tied zero
starts survive an edit); harness matrix PASS=324/0.

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
Scott GarrettandClaude Code committed 2026-11-26 19:14:59 -05:00
1 parent f457b4e89c
commit 06ecb0af5f
4 files changed
+46 -15

No files matched your search

+13 -2
View File
@@ -339,7 +339,12 @@ final class Document: NSDocument {
default: chaptersChanged = true
}
if tracksChanged || chaptersChanged || saveOperation != .saveOperation {
// `optimize` (Save and Optimize) forces the structural branch: the
// Matroska equivalent of reordering atoms is rebuilding the whole file
// — blocks stream-copied verbatim, Tracks/Tags rewritten, SeekHead and
// Cues regenerated — i.e. exactly the Save-As pass, done in place
// through the remuxer's temp file + atomic rename.
if tracksChanged || chaptersChanged || optimize || saveOperation != .saveOperation {
// An absent model track means "no chapters" — pass an empty
// track so the remuxer removes the element rather than copying
// the old one verbatim (nil replacement = verbatim).
@@ -480,7 +485,13 @@ final class Document: NSDocument {
return true
case #selector(sendToQueue(_:)):
return true
case #selector(saveAndOptimize(_:)) where isDocumentEdited == false && mp4.hasFileRepresentation == true:
// On a Matroska document optimize = full remuxer rebuild (see
// writeMatroska), the container-equivalent of the atom-order optimize.
// Tags-only models report hasFileRepresentation == false (their tracks
// are never muxed), so the on-disk precondition there is just the
// remux source: fileURL.
case #selector(saveAndOptimize(_:)) where isDocumentEdited == false
&& (isMatroska ? fileURL != nil : mp4.hasFileRepresentation == true):
return true
default:
return false
+29 -9
View File
@@ -280,7 +280,8 @@ final class DocumentWindowController: NSWindowController, NSWindowDelegate, Trac
#selector(searchMetadata(_:)),
#selector(sendToQueue(_:)),
#selector(insertChapter(_:)),
#selector(addChaptersEvery(_:)):
#selector(addChaptersEvery(_:)),
#selector(searchChapters(_:)):
return true
// Matroska documents may import subtitle text files (written as
@@ -288,12 +289,11 @@ final class DocumentWindowController: NSWindowController, NSWindowDelegate, Trac
case #selector(selectFile(_:)):
return true
// Track bulk modifications (and the chapter-DB search, whose apply
// path lacks the tags-only bookkeeping): unavailable in Matroska
// tags-only mode. The Insert-a-chapter actions are NOT on this list —
// chapter changes ride the stream-copy remuxer, so they work on MKV.
case #selector(searchChapters(_:)),
#selector(iTunesFriendlyTrackGroups(_:)),
// Track bulk modifications: unavailable in Matroska tags-only mode.
// Chapter actions (searchChapters, the Insert-a-chapter commands) are
// NOT on this list — chapter changes ride the stream-copy remuxer,
// so they work on MKV too.
case #selector(iTunesFriendlyTrackGroups(_:)),
#selector(clearTrackNames(_:)),
#selector(prettifyAudioTrackNames(_:)),
#selector(fixAudioFallbacks(_:)):
@@ -552,7 +552,13 @@ final class DocumentWindowController: NSWindowController, NSWindowDelegate, Trac
chapterTrack.addChapter(chapter)
}
mp4.addTrack(chapterTrack)
mp4.addTrack(chapterTrack) // addTrack: replaces any previous chapter track
if doc.isMatroska {
// Tags-only bookkeeping, same as addChapters/insertChapter:
// addTrack: zeroes trackId, and nothing else marks the document.
chapterTrack.trackId = (mp4.tracks.map { $0.trackId }.max() ?? 0) + 1
doc.updateChangeCount(.changeDone)
}
tracksViewController.reloadData()
tracksViewController.selectTracks([chapterTrack])
}
@@ -761,8 +767,22 @@ final class DocumentWindowController: NSWindowController, NSWindowDelegate, Trac
}
@objc func importFilesDirectly(_ fileURLs: [URL]) {
// Mirror the open panel's extension routing (selectFile): chapter
// text files go to the chapter commands — addChapters bootstraps the
// chapter track on both containers — everything else to the regular
// track importer.
var mediaURLs: [URL] = []
for url in fileURLs {
switch url.pathExtension.lowercased() {
case "txt": addChapters(fileURL: url)
case "csv": updateChapters(fileURL: url)
default: mediaURLs.append(url)
}
}
guard mediaURLs.isEmpty == false else { return }
do {
let controller = try FileImportController(fileURLs: fileURLs, delegate: self)
let controller = try FileImportController(fileURLs: mediaURLs, delegate: self)
// Call addTracks directly - the Settings initialization logic runs when the controller is created
controller.addTracks(self)