osx- opening Privacy>Accessibility window programmatically
Asked Answered
D

5

8

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.

Dinothere answered 9/1, 2015 at 8:18 Comment(0)
C
9

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"]];
Conventionalize answered 16/8, 2018 at 23:26 Comment(0)
D
2

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];
}
Dinothere answered 12/1, 2015 at 4:48 Comment(0)
I
2

Swift 4 compatible version, adopted from the accepted answer:

static func openAccessibilityPreferences() {
    let macOS10version = ProcessInfo.processInfo.operatingSystemVersion.minorVersion

    let script = macOS10version < 9
        ? "tell application \"System Preferences\" \n set the current pane to pane id \"com.apple.preference.universalaccess\" \n activate \n end tell"
        : "tell application \"System Preferences\" \n reveal anchor \"Privacy_Accessibility\" of pane id \"com.apple.preference.security\" \n activate \n end tell"

    NSAppleScript(source: script)?.executeAndReturnError(nil)
}
Imbrue answered 5/12, 2018 at 21:20 Comment(0)
M
1

Found simpler solution:

NSURL *URL = [NSURL URLWithString:@"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility"];
[[NSWorkspace sharedWorkspace] openURL:URL];

In case you just need to open prefPane

NSURL *URL = [NSURL URLWithString:@"/System/Library/PreferencePanes/Security.prefPane"];
[[NSWorkspace sharedWorkspace] openFile:[URL relativePath]];
Maiduguri answered 7/1, 2018 at 17:47 Comment(1)
The accepted answer didn't work for me, but this one did. Thanks!Rann
B
0

A way from shell command is
/bin/sh -c open -b com.apple.systempreferences /System/Library/PreferencePanes/Security.prefPane

If you know how to open it from command-line, you can customize your programmatical way

Bijection answered 21/9, 2020 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.