diff options
author | Damiano Galassi <[email protected]> | 2019-06-08 17:46:01 +0200 |
---|---|---|
committer | Damiano Galassi <[email protected]> | 2019-06-08 17:46:01 +0200 |
commit | 727dc9879454c0aea3b2cf4aecbd54d95d8ae45b (patch) | |
tree | dcb787647dde0e6a520e8c834021eceba95999ad | |
parent | fd2c883de4cea7094c08fed7ea22418ec3472281 (diff) |
MacGui: fix queue sleep and shutdown on 10.14 and under sandbox.
-rw-r--r-- | macosx/HBQueueController.m | 17 | ||||
-rw-r--r-- | macosx/HBUtilities.h | 8 | ||||
-rw-r--r-- | macosx/HBUtilities.m | 48 | ||||
-rw-r--r-- | macosx/HandBrake.entitlements | 4 | ||||
-rw-r--r-- | macosx/Info.plist.m4 | 4 |
5 files changed, 79 insertions, 2 deletions
diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m index bd8564ded..87d0ceefe 100644 --- a/macosx/HBQueueController.m +++ b/macosx/HBQueueController.m @@ -1073,7 +1073,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; // Sleep NSDictionary *errorDict; NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource: - @"tell application \"Finder\" to sleep"]; + @"tell application \"System Events\" to sleep"]; [scriptObject executeAndReturnError: &errorDict]; } // If Shutdown has been selected @@ -1081,7 +1081,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { // Shut Down NSDictionary *errorDict; - NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to shut down"]; + NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to shut down"]; [scriptObject executeAndReturnError: &errorDict]; } } @@ -1219,6 +1219,8 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { [self.delegate showPreferencesWindow:nil]; } + + [self promptForAppleEventAuthorization]; } else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"HBAlertWhenDone"] == HBDoneActionShutDown) { @@ -1237,6 +1239,17 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext; { [self.delegate showPreferencesWindow:nil]; } + + [self promptForAppleEventAuthorization]; + } +} + +- (void)promptForAppleEventAuthorization +{ + HBPrivacyConsentState result = [HBUtilities determinePermissionToAutomateTarget:@"com.apple.systemevents" promptIfNeeded:YES]; + if (result != HBPrivacyConsentStateGranted) + { + [HBUtilities writeToActivityLog:"Failed to get permission to automate system events"]; } } diff --git a/macosx/HBUtilities.h b/macosx/HBUtilities.h index edf25ed73..aa87f4f1b 100644 --- a/macosx/HBUtilities.h +++ b/macosx/HBUtilities.h @@ -70,6 +70,14 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)iso6392CodeFor:(NSString *)language; + (NSString *)languageCodeForIso6392Code:(NSString *)language; +typedef NS_ENUM(NSUInteger, HBPrivacyConsentState) { + HBPrivacyConsentStateUnknown, + HBPrivacyConsentStateDenied, + HBPrivacyConsentStateGranted, +}; + ++ (HBPrivacyConsentState)determinePermissionToAutomateTarget:(NSString *)bundleIdentifier promptIfNeeded:(BOOL)promptIfNeeded; + @end NS_ASSUME_NONNULL_END diff --git a/macosx/HBUtilities.m b/macosx/HBUtilities.m index 7e95f007a..9a627bf5a 100644 --- a/macosx/HBUtilities.m +++ b/macosx/HBUtilities.m @@ -383,4 +383,52 @@ static NSDateFormatter *_releaseDateFormatter = nil; return @"Unknown"; } +#if __MAC_OS_X_VERSION_MIN_REQUIRED <= __MAC_10_14 +enum { + errAEEventWouldRequireUserConsent = -1744, +}; +#endif + ++ (HBPrivacyConsentState)determinePermissionToAutomateTarget:(NSString *)bundleIdentifier promptIfNeeded:(BOOL)promptIfNeeded +{ + if (@available(macOS 10.14, *)) + { + const char *identifierCString = bundleIdentifier.UTF8String; + AEAddressDesc addressDesc; + OSErr descResult = AECreateDesc(typeApplicationBundleID, identifierCString, strlen(identifierCString), &addressDesc); + + if (descResult == noErr) + { + OSStatus permission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, promptIfNeeded); + AEDisposeDesc(&addressDesc); + + HBPrivacyConsentState result; + + switch (permission) + { + case errAEEventWouldRequireUserConsent: + [HBUtilities writeToActivityLog:"Request user consent for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateUnknown; + break; + case noErr: + [HBUtilities writeToActivityLog:"Permission granted for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateGranted; + break; + case errAEEventNotPermitted: + [HBUtilities writeToActivityLog:"Permission not granted for %s.", bundleIdentifier.UTF8String]; + result = HBPrivacyConsentStateDenied; + break; + case procNotFound: + default: + [HBUtilities writeToActivityLog:"Permission unknown."]; + result = HBPrivacyConsentStateUnknown; + break; + } + return result; + } + } + + return HBPrivacyConsentStateGranted; +} + @end diff --git a/macosx/HandBrake.entitlements b/macosx/HandBrake.entitlements index e29f683e0..46fc9d34a 100644 --- a/macosx/HandBrake.entitlements +++ b/macosx/HandBrake.entitlements @@ -14,6 +14,10 @@ <true/> <key>com.apple.security.files.user-selected.read-write</key> <true/> + <key>com.apple.security.temporary-exception.apple-events</key> + <array> + <string>com.apple.systemevents</string> + </array> <key>com.apple.security.temporary-exception.files.absolute-path.read-only</key> <array> <string>/usr/local/</string> diff --git a/macosx/Info.plist.m4 b/macosx/Info.plist.m4 index cb647b9bc..aa142fae4 100644 --- a/macosx/Info.plist.m4 +++ b/macosx/Info.plist.m4 @@ -72,5 +72,9 @@ GPLv2 license.</string> <false/> <key>NSUserNotificationAlertStyle</key> <string>alert</string> + <key>NSAppleEventsUsageDescription</key> + <string>HandBrake needs access to shut down or put the computer to sleep</string> + <key>NSAppleScriptEnabled</key> + <true/> </dict> </plist> |