Skip to content

Commit

Permalink
Check for accessibility permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
dteoh committed Jun 16, 2018
1 parent d3e35ac commit 2488436
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SlowQuitApps/SQAAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ - (id)init {
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
SQADialogs *dialogs = [[SQADialogs alloc] init];

if (!hasAccessibility()) {
[dialogs informAccessibilityRequirement];
// If we terminate now, the special accesibility alert/dialog
// from the framework/OS will dissappear immediately.
return;
}

if ([self registerGlobalHotkey]) {
[dialogs askAboutAutoStart];

Expand Down Expand Up @@ -90,6 +97,11 @@ - (void)cmdQPressed {
return NULL;
}

BOOL hasAccessibility() {
NSDictionary *options = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES};
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
}

BOOL shouldHandleCmdQ() {
NSRunningApplication *activeApp = findActiveApp();
if (activeApp == NULL) {
Expand Down
1 change: 1 addition & 0 deletions SlowQuitApps/SQADialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

- (void)askAboutAutoStart;
- (void)informHotkeyRegistrationFailure;
- (void)informAccessibilityRequirement;

@end
9 changes: 9 additions & 0 deletions SlowQuitApps/SQADialogs.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ - (void)informHotkeyRegistrationFailure {
[alert runModal];
}

- (void)informAccessibilityRequirement {
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleInformational;
alert.messageText = NSLocalizedString(@"SlowQuitApps requires permissions to control your computer", nil);
alert.informativeText = NSLocalizedString(@"SlowQuitApps needs accessibility permissions to handle ⌘Q.\r\rAfter adding SlowQuitApps to System Preferences -> Security & Privacy -> Privacy -> Accessibility, please restart the app.", nil);
[alert addButtonWithTitle:NSLocalizedString(@"OK", nil)];
[alert runModal];
}

@end

0 comments on commit 2488436

Please sign in to comment.