Files
SublerMKV/Classes/Utilities.swift
T
Scott GarrettandClaude Code eb386a52c3 Open and edit Matroska files in place, including track removal
.mkv/.mka/.mks now open directly into a tags-only document instead of
forcing the convert-import sheet:

- Metadata, cover art, and the four per-track fields Matroska stores
  (name, language, enabled, subtitle forced) save back into the same
  file via MP42MkvTagWriter; MP4-only properties stay read-only.
- Real tracks can be deleted and Save-As is enabled; both go through
  MP42MkvRemuxer's stream-copy rebuild (Document keeps an importer-order
  track snapshot and pairs TrackEntry elements by object identity).
  Destinations must stay .mkv/.mka/.mks.
- Info.plist: the Matroska document type becomes Editor so NSDocument
  offers .mkv Save-As instead of defaulting to the MP4 type.
- Delete on MKV docs skips organizeAlternateGroups/
  inferMediaCharacteristics because they reorder the model, which the
  TrackEntry pairing depends on; chapter rows stay undeletable.

Bumps MP42Foundation for the Matroska tagging/remux support.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-25 19:48:34 -05:00

145 lines
5.0 KiB
Swift

//
// Utilities.swift
// Subler
//
// Created by Damiano Galassi on 31/01/2019.
//
import Cocoa
enum PrivacyConsentState {
case unknown
case granted
case denied
}
func automationConsent(bundleIdentifier: String, promptIfNeeded: Bool) -> PrivacyConsentState {
var result: PrivacyConsentState = .denied
if #available(macOS 10.14, *) {
bundleIdentifier.withCString { (identifier) in
let workspace = NSWorkspace.shared
// if #available(macOS 12, *) {
// // Make sure the target is running, if not the consent alert will not be shown.
// if let url = workspace.urlsForApplications(withBundleIdentifier: bundleIdentifier).first {
// let configuration = NSWorkspace.OpenConfiguration()
// workspace.openApplication(at: url, configuration: configuration) {_,_ in
// }
// }
// }
// Mare sure the target is running, if not the consent alert will not be shown.
workspace.launchApplication(withBundleIdentifier: bundleIdentifier, options: [],
additionalEventParamDescriptor: nil, launchIdentifier: nil)
var addressDesc = AEAddressDesc()
let createDescResult = AECreateDesc(typeApplicationBundleID, identifier, strlen(identifier), &addressDesc)
if createDescResult == noErr {
let appleScriptPermission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, promptIfNeeded)
AEDisposeDesc(&addressDesc)
switch appleScriptPermission {
case -1744://Int32(errAEEventWouldRequireUserConsent):
result = .unknown
case noErr:
result = .granted
case Int32(errAEEventNotPermitted):
result = .denied
case Int32(procNotFound):
result = .unknown
default:
result = .unknown
break
}
}
}
return result
} else {
return .granted
}
}
func sendToFileExternalApp(fileURL: URL) -> Bool {
let filePath = fileURL.path
if #available(macOS 10.15, *) {
if let script = NSAppleScript(source: """
tell application "TV" to add (POSIX file "\(filePath)")
""") {
let result = automationConsent(bundleIdentifier: "com.apple.TV", promptIfNeeded: true)
if result == .granted {
var error: NSDictionary?
script.executeAndReturnError(&error)
return error != nil
}
}
} else {
if let script = NSAppleScript(source: """
tell application "iTunes" to add (POSIX file "\(filePath)")
""") {
let result = automationConsent(bundleIdentifier: "com.apple.iTunes", promptIfNeeded: true)
if result == .granted {
var error: NSDictionary?
script.executeAndReturnError(&error)
return error != nil
}
}
}
return false
}
extension String {
// All content is licensed under the terms of the MIT open source license.
// Copyright (c) 2016 Matthijs Hollemans and contributors
func minimumEditDistance(other: String) -> Int {
let m = self.count
let n = other.count
var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
// initialize matrix
for index in 1...m {
// the distance of any first string to an empty second string
matrix[index][0] = index
}
for index in 1...n {
// the distance of any second string to an empty first string
matrix[0][index] = index
}
// compute Levenshtein distance
for (i, selfChar) in self.enumerated() {
for (j, otherChar) in other.enumerated() {
if otherChar == selfChar {
// substitution of equal symbols with cost 0
matrix[i + 1][j + 1] = matrix[i][j]
} else {
// minimum of the cost of insertion, deletion, or substitution
// added to the already computed costs in the corresponding cells
matrix[i + 1][j + 1] = Swift.min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1)
}
}
}
return matrix[m][n]
}
}
extension NSView {
/// Enables or disables every NSControl in this view hierarchy. Used to
/// present the track property views read-only in Matroska (tags-only)
/// documents, where the tracks cannot be modified.
func setAllControlsEnabled(_ enabled: Bool) {
if let control = self as? NSControl {
control.isEnabled = enabled
}
for subview in subviews {
subview.setAllControlsEnabled(enabled)
}
}
}