diff --git a/Classes/DocumentWindowController.swift b/Classes/DocumentWindowController.swift index cb3131e..15581d2 100644 --- a/Classes/DocumentWindowController.swift +++ b/Classes/DocumentWindowController.swift @@ -264,9 +264,16 @@ class DocumentWindowController: NSWindowController, TracksViewControllerDelegate private var sheetController: NSWindowController? @IBAction func sendToExternalApp(_ sender: Any) { - let workspace = NSWorkspace.shared - if let filePath = doc.fileURL?.path, let appPath = workspace.fullPath(forApplication: "iTunes") { - workspace.openFile(filePath, withApplication: appPath) + + if let filePath = doc.fileURL?.path, 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) + } } } diff --git a/Classes/MP42FileAdditions.swift b/Classes/MP42FileAdditions.swift index 0f46935..ed14ab5 100644 --- a/Classes/MP42FileAdditions.swift +++ b/Classes/MP42FileAdditions.swift @@ -16,16 +16,6 @@ private extension String { } } -extension NSPasteboard.PasteboardType { - static let backwardsCompatibleFileURL: NSPasteboard.PasteboardType = { - if #available(OSX 10.13, *) { - return NSPasteboard.PasteboardType.fileURL - } else { - return NSPasteboard.PasteboardType(kUTTypeFileURL as String) - } - } () -} - extension MP42File { enum TrackHDType : Int { diff --git a/Classes/QueueAction.swift b/Classes/QueueAction.swift index 5bb9d65..5bed8a6 100644 --- a/Classes/QueueAction.swift +++ b/Classes/QueueAction.swift @@ -451,12 +451,30 @@ class QueueClearTrackNameAction : NSObject, QueueActionProtocol { } enum QueueColorSpaceActionTag: UInt16 { - case SBQueueColorSpaceActionTagNone = 1 - case SBQueueColorSpaceActionTagRec601PAL - case SBQueueColorSpaceActionTagRec601SMPTEC - case SBQueueColorSpaceActionTagRec709 - case SBQueueColorSpaceActionTagRec2020 - case SBQueueColorSpaceActionTagRec2100PQ + case None = 1 + case Rec601PAL + case Rec601SMPTEC + case Rec709 + case Rec2020 + case Rec2100PQ + + func tagValues() -> (colorPrimaries: UInt16, transferCharacteristics: UInt16, matrixCoefficients: UInt16) { + switch self { + case .None: + return (0, 0, 0) + case .Rec601PAL: + return (5, 1, 6) + case .Rec601SMPTEC: + return (6, 1, 6) + case .Rec709: + return (1, 1, 1) + case .Rec2020: + return (9, 1, 9) + case .Rec2100PQ: + return (9, 16, 9) + } + } + } /// An action that set the video track color space. @@ -466,37 +484,10 @@ class QueueColorSpaceAction : NSObject, QueueActionProtocol { var localizedDescription: String { return NSLocalizedString("Setting color space", comment: "Set track color space action local description") } override var description: String { return NSLocalizedString("Set color space", comment: "Set track color space action description") } - let colorPrimaries: UInt16; - let transferCharacteristics: UInt16; - let matrixCoefficients: UInt16; + let tag: QueueColorSpaceActionTag; init(tag: QueueColorSpaceActionTag) { - switch tag { - case .SBQueueColorSpaceActionTagNone: - self.colorPrimaries = 0 - self.transferCharacteristics = 0 - self.matrixCoefficients = 0 - case .SBQueueColorSpaceActionTagRec601PAL: - self.colorPrimaries = 5 - self.transferCharacteristics = 1 - self.matrixCoefficients = 6 - case .SBQueueColorSpaceActionTagRec601SMPTEC: - self.colorPrimaries = 6 - self.transferCharacteristics = 1 - self.matrixCoefficients = 6 - case .SBQueueColorSpaceActionTagRec709: - self.colorPrimaries = 1 - self.transferCharacteristics = 1 - self.matrixCoefficients = 1 - case .SBQueueColorSpaceActionTagRec2020: - self.colorPrimaries = 9 - self.transferCharacteristics = 1 - self.matrixCoefficients = 9 - case .SBQueueColorSpaceActionTagRec2100PQ: - self.colorPrimaries = 9 - self.transferCharacteristics = 16 - self.matrixCoefficients = 9 - } + self.tag = tag } func runAction(_ item: QueueItem) -> Bool { @@ -506,9 +497,10 @@ class QueueColorSpaceAction : NSObject, QueueActionProtocol { track.format == kMP42VideoCodecType_HEVC || track.format == kMP42VideoCodecType_HEVC_PSinBitstream || track.format == kMP42VideoCodecType_MPEG4Video { - track.colorPrimaries = colorPrimaries; - track.transferCharacteristics = transferCharacteristics; - track.matrixCoefficients = matrixCoefficients; + let values = tag.tagValues() + track.colorPrimaries = values.colorPrimaries; + track.transferCharacteristics = values.transferCharacteristics; + track.matrixCoefficients = values.matrixCoefficients; } } return true @@ -517,15 +509,11 @@ class QueueColorSpaceAction : NSObject, QueueActionProtocol { } func encode(with aCoder: NSCoder) { - aCoder.encode(Int32(colorPrimaries), forKey: "SBQueueColorSpaceActionColorPrimaries") - aCoder.encode(Int32(transferCharacteristics), forKey: "SBQueueColorSpaceActionTransferCharacteristics") - aCoder.encode(Int32(matrixCoefficients), forKey: "SBQueueColorSpaceActionMatrixCoefficients") + aCoder.encode(Int32(tag.rawValue), forKey: "SBQueueColorSpaceActionTag") } required init?(coder aDecoder: NSCoder) { - self.colorPrimaries = UInt16(aDecoder.decodeInt32(forKey: "SBQueueColorSpaceActionColorPrimaries")) - self.transferCharacteristics = UInt16(aDecoder.decodeInt32(forKey: "SBQueueColorSpaceActionTransferCharacteristics")) - self.matrixCoefficients = UInt16(aDecoder.decodeInt32(forKey: "SBQueueColorSpaceActionMatrixCoefficients")) + self.tag = QueueColorSpaceActionTag(rawValue: UInt16(aDecoder.decodeInt32(forKey: "SBQueueColorSpaceActionTag"))) ?? .Rec709 } static var supportsSecureCoding: Bool { return true } @@ -558,9 +546,17 @@ class QueueSendToiTunesAction: NSObject, QueueActionProtocol { override init() {} func runAction(_ item: QueueItem) -> Bool { - let workspace = NSWorkspace.shared - if let appPath = workspace.fullPath(forApplication: "iTunes") { - return workspace.openFile(item.destURL.path, withApplication: appPath) + + if let script = NSAppleScript(source: """ + tell application "iTunes" to add (POSIX file "\(item.destURL.path)") + """) { + + let result = automationConsent(bundleIdentifier: "com.apple.iTunes", promptIfNeeded: true) + if result == .granted { + var error: NSDictionary? + script.executeAndReturnError(&error) + return true + } } return false diff --git a/Classes/QueueController.swift b/Classes/QueueController.swift index 47a48fc..d917471 100644 --- a/Classes/QueueController.swift +++ b/Classes/QueueController.swift @@ -46,6 +46,7 @@ class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, fatalError("Invalid queue url") } super.init(window: nil) + _ = window } required init?(coder: NSCoder) { @@ -264,8 +265,8 @@ class QueueController : NSWindowController, NSWindowDelegate, NSPopoverDelegate, if prefs.fixTrackLanguage { item.addAction(QueueSetLanguageAction(language: prefs.fixTrackLanguageValue)) } - if prefs.applyColorSpace { - item.addAction(QueueColorSpaceAction(tag: QueueColorSpaceActionTag(rawValue: UInt16(prefs.applyColorSpaceValue))!)) + if prefs.applyColorSpace, let tag = QueueColorSpaceActionTag(rawValue: UInt16(prefs.applyColorSpaceValue)) { + item.addAction(QueueColorSpaceAction(tag: tag)) } if let set = prefs.metadataSet { item.addAction(QueueSetAction(preset: set)) diff --git a/Classes/QueuePreset.swift b/Classes/QueuePreset.swift index 435ddbe..6e21454 100644 --- a/Classes/QueuePreset.swift +++ b/Classes/QueuePreset.swift @@ -10,7 +10,7 @@ import Foundation class QueuePreset: Preset { var title: String - let options: [String:Any] + let options: [String : Any] let version: Int var changed: Bool @@ -19,7 +19,7 @@ class QueuePreset: Preset { return "sbqueuepreset" } - init(title: String, options: [String:Any]) { + init(title: String, options: [String : Any]) { self.title = title self.options = options self.changed = true diff --git a/Classes/Utilities.swift b/Classes/Utilities.swift new file mode 100644 index 0000000..67629d6 --- /dev/null +++ b/Classes/Utilities.swift @@ -0,0 +1,54 @@ +// +// Utilities.swift +// Subler +// +// Created by Damiano Galassi on 31/01/2019. +// + +import Cocoa + +extension NSPasteboard.PasteboardType { + static let backwardsCompatibleFileURL: NSPasteboard.PasteboardType = { + if #available(OSX 10.13, *) { + return NSPasteboard.PasteboardType.fileURL + } else { + return NSPasteboard.PasteboardType(kUTTypeFileURL as String) + } + } () +} + +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 + 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: //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 + } +} diff --git a/Info.plist b/Info.plist index 42d3639..b864668 100644 --- a/Info.plist +++ b/Info.plist @@ -2,6 +2,8 @@ + NSAppleEventsUsageDescription + Subler needs access to add movies to iTunes CFBundleDevelopmentRegion English CFBundleDocumentTypes diff --git a/Subler.entitlements b/Subler.entitlements index 4d1438c..447295b 100755 --- a/Subler.entitlements +++ b/Subler.entitlements @@ -4,6 +4,8 @@ com.apple.security.assets.movies.read-write + com.apple.security.automation.apple-events + com.apple.security.files.bookmarks.app-scope com.apple.security.files.user-selected.read-write diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index ba3f39e..e2c2a37 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -98,6 +98,7 @@ A9BA1AEE1DBF7DD700FD0B38 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = A9BA1AF01DBF7DD700FD0B38 /* InfoPlist.strings */; }; A9BA1AF31DBF7DD800FD0B38 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = A9BA1AF51DBF7DD800FD0B38 /* Localizable.strings */; }; A9BC410818D9ABC000F8C92C /* QueueItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9BC410618D9ABC000F8C92C /* QueueItem.xib */; }; + A9BE561B220307F100FF6BE3 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9BE561A220307F100FF6BE3 /* Utilities.swift */; }; A9C09C7C1F99F460007939EE /* ExceptionAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C09C7B1F99F460007939EE /* ExceptionAlertController.swift */; }; A9C09C981F99F971007939EE /* ExpandedTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C09C971F99F971007939EE /* ExpandedTableView.swift */; }; A9C09C9A1F9A0808007939EE /* EmptyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C09C991F9A0808007939EE /* EmptyViewController.swift */; }; @@ -321,6 +322,7 @@ A9BA1AE61DBF773700FD0B38 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/InfoPlist.strings; sourceTree = ""; }; A9BA1AEF1DBF7DD700FD0B38 /* it */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/InfoPlist.strings; sourceTree = ""; }; A9BA1AF41DBF7DD800FD0B38 /* it */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = it; path = it.lproj/Localizable.strings; sourceTree = ""; }; + A9BE561A220307F100FF6BE3 /* Utilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Utilities.swift; path = Classes/Utilities.swift; sourceTree = ""; }; A9C09C7B1F99F460007939EE /* ExceptionAlertController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ExceptionAlertController.swift; path = Classes/ExceptionAlertController.swift; sourceTree = ""; }; A9C09C971F99F971007939EE /* ExpandedTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ExpandedTableView.swift; path = Classes/ExpandedTableView.swift; sourceTree = ""; }; A9C09C991F9A0808007939EE /* EmptyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = EmptyViewController.swift; path = Classes/PropertyView/EmptyViewController.swift; sourceTree = ""; }; @@ -424,6 +426,7 @@ A9FAC5FE1BAC3BAC00525E42 /* Document */, A9AF6A7818D0537F00FAAB08 /* Queue */, A9915ABB1F4B26A5003C57D1 /* Presets */, + A9BE561A220307F100FF6BE3 /* Utilities.swift */, A9395E341F8B9ACA004C402B /* MP42FileAdditions.swift */, A91EB968205ABAF400392E1C /* NSWindowAdditions.swift */, A98D440F2036F661005CB092 /* AppDelegate.swift */, @@ -714,6 +717,9 @@ LastSwiftMigration = 1020; ProvisioningStyle = Automatic; SystemCapabilities = { + com.apple.HardenedRuntime = { + enabled = 1; + }; com.apple.Sandbox = { enabled = 0; }; @@ -909,6 +915,7 @@ A92AD0161F3482350082892E /* NetworkUtilities.swift in Sources */, A944E5921F498FC4002E43F6 /* PresetPrefsViewController.swift in Sources */, A9E97F141F274F1100DBA586 /* iTunesStore.swift in Sources */, + A9BE561B220307F100FF6BE3 /* Utilities.swift in Sources */, A9433A8D1F2F5C3600BB38CA /* ChapterResult.swift in Sources */, A9C73536206B8F9900794673 /* QueuePreferences.swift in Sources */, A9C09C9C1F9A08A9007939EE /* MultiSelectViewController.swift in Sources */, @@ -1370,6 +1377,7 @@ CODE_SIGN_STYLE = Automatic; COPY_PHASE_STRIP = NO; DEVELOPMENT_TEAM = 5X9DE89KYV; + ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)\"", @@ -1408,6 +1416,7 @@ CODE_SIGN_IDENTITY = "Mac Developer"; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 5X9DE89KYV; + ENABLE_HARDENED_RUNTIME = YES; FRAMEWORK_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)\"", @@ -1470,7 +1479,6 @@ COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; - ENABLE_HARDENED_RUNTIME = YES; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = c11; @@ -1541,7 +1549,6 @@ CODE_SIGN_IDENTITY = ""; COMBINE_HIDPI_IMAGES = YES; DEFINES_MODULE = YES; - ENABLE_HARDENED_RUNTIME = YES; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = c11; GCC_NO_COMMON_BLOCKS = YES;