Created
April 19, 2016 17:07
-
-
Save pilotmoon/a80d4be461c3e173f2f0901707e0d704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DCUniversalAccessHelper.m | |
// dc | |
// | |
// Created by Work on 22/10/2010. | |
// Copyright 2010 Nicholas Moore. All rights reserved. | |
// | |
#import "NMUniversalAccessHelper.h" | |
#import "NMAppUtils.h" | |
// for dlsym & weak linking | |
#import <dlfcn.h> | |
static Boolean (*_weak_AXIsProcessTrustedWithOptions)(CFDictionaryRef); | |
static CFStringRef *_weak_kAXTrustedCheckOptionPrompt; | |
static NMUniversalAccessHelper *_sharedInstance; | |
NSString *NMUniversalAccessStateChangedNotification=@"NMUniversalAccessStateChangedNotification"; | |
@implementation NMUniversalAccessHelper | |
+ (NMUniversalAccessHelper *)sharedInstance | |
{ | |
if (!_sharedInstance) { | |
_sharedInstance=[[NMUniversalAccessHelper alloc] init]; | |
_weak_AXIsProcessTrustedWithOptions=dlsym(RTLD_DEFAULT, "AXIsProcessTrustedWithOptions"); | |
_weak_kAXTrustedCheckOptionPrompt=dlsym(RTLD_DEFAULT, "kAXTrustedCheckOptionPrompt"); | |
} | |
return _sharedInstance; | |
} | |
// if available, use the new Check with prompt method | |
+ (BOOL)checkAxTrustedWithPrompt:(BOOL)prompt | |
{ | |
if (_weak_AXIsProcessTrustedWithOptions!=0&&_weak_kAXTrustedCheckOptionPrompt!=0) { | |
return (_weak_AXIsProcessTrustedWithOptions)((__bridge CFDictionaryRef)@{(__bridge NSString*)(*_weak_kAXTrustedCheckOptionPrompt): @(prompt)}); | |
} | |
else { | |
return AXIsProcessTrusted(); | |
} | |
} | |
- (id)init | |
{ | |
self=[super init]; | |
if (self) { | |
[self refreshState]; | |
} | |
return self; | |
} | |
- (BOOL)isAxEnabled | |
{ | |
return AXAPIEnabled()||[[self class] checkAxTrustedWithPrompt:NO]; | |
} | |
- (void)refreshState | |
{ | |
BOOL is=[self isAxEnabled]; | |
if (is!=axWasEnabled) { | |
[self willChangeValueForKey:@"axEnabled"]; | |
[self didChangeValueForKey:@"axEnabled"]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:NMUniversalAccessStateChangedNotification object:self userInfo:@{@"axEnabled": @(is)}]; | |
} | |
axWasEnabled=is; | |
} | |
+ (void)openUniversalAccessPane | |
{ | |
[[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/UniversalAccessPref.prefPane"]; | |
} | |
+ (BOOL)enableUniversalAccess | |
{ | |
if (NMOSVersionCheckMountainLionOrBelow()) { | |
BOOL good=NO; | |
NSString *s = [NSString stringWithFormat: | |
@"tell application \"System Events\"\n" | |
@"\tif UI elements enabled is false then\n" | |
@"\t\tactivate\n" | |
@"\t\tset UI elements enabled to true\n" | |
@"\tend\n" | |
@"\treturn UI elements enabled\n" | |
@"end tell"]; | |
NSAppleScript *scriptObj = [[NSAppleScript alloc] initWithSource:s]; | |
if (scriptObj) { | |
NSDictionary *error = nil; | |
NSAppleEventDescriptor *desc = [scriptObj executeAndReturnError:&error]; | |
if(error) { | |
//NMLogFine(@"%@", error); | |
} | |
else if(desc) { | |
good = [desc booleanValue]; | |
} | |
} | |
return good; | |
} | |
else { | |
return [self checkAxTrustedWithPrompt:YES]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment