Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

osx- opening Privacy>Accessibility window programmatically

I am working on the app which needs to be enabled from System Preferences > Security and Privacy > Privacy > Accessibility.

Right now, I am using the following code to open window shown in the screenshot below:

-(IBAction)enableAccessibility
{
NSString *script = @"tell application \"System Preferences\" \n reveal anchor \"Privacy\" of pane id \"com.apple.preference.security\" \n activate \n end tell";

NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
[scriptObject executeAndReturnError:nil];
}

But not necessary that it will open "Accessibility" tab. Instead, it opens previously opened tab.

So Please suggest me the way to modify this code which will open specifically "Accessibility" Tab from the side menu of this window.

enter image description here

Thanks in advance.

like image 658
Piyush Mathur Avatar asked Jan 09 '15 08:01

Piyush Mathur


2 Answers

https://macosxautomation.com/system-prefs-links.html has a page of links to many, but not all of, the various preference panes. With a little bit of guesswork, I was able to verify that these calls work under macOS Mojave beta 7. I'm using these calls to guide the user to the proper pane when they've denied access to a device that the app can't run without.

// open last Privacy subpane viewed:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy"]];

// specify a particular subpange
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Camera"]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Microphone"]];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Automation"]];
like image 94
Hal Mueller Avatar answered Nov 05 '22 10:11

Hal Mueller


While searching for the solution, I found generated the following code from some hints in this question which worked for me.

This is what I wanted to implement.

Thanks @duskwuff for supporting with your comment.

NSString *script;
if ([[[[NSProcessInfo processInfo] operatingSystemVersionString] substringToIndex:12] isEqualToString:@"Version 10.7"] || [[[[NSProcessInfo processInfo] operatingSystemVersionString] substringToIndex:12] isEqualToString:@"Version 10.8"])
{ //>> For OSX 10.7 and 10.8
     script = @"tell application \"System Preferences\" \n set the current pane to pane id \"com.apple.preference.universalaccess\" \n activate \n end tell";

     NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
     [scriptObject executeAndReturnError:nil];

}
else
{ //>> For OSX 10.9 and 10.10
    script = @"tell application \"System Preferences\" \n reveal anchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\" \n activate \n end tell";

    NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:script];
    [scriptObject executeAndReturnError:nil];
}
like image 20
Piyush Mathur Avatar answered Nov 05 '22 11:11

Piyush Mathur