开发者

Fastest way to programmatically open prefpane?

AppleScript was too slow, so I tried ScriptingBridge to open System Preferences.app and set the current pane, which is also too slow. Is there a faster way to do开发者_如何学运维 it? properly


A more direct method than using the file system path is to use the appropriate resource URL for the preference pane with NSWorkspace as shown:

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

where the urlString was taken from a list of some of the possible URL strings https://macosxautomation.com/system-prefs-links.html


Use Launch Services or NSWorkspace to open the prefpane bundle. That's the programmatic version of the open(1) command.


No brainer:

system("open -a System\\ Preferences");

And to choose which Pane to open:

open /System/Library/PreferencePanes/Internet.prefPane
open /System/Library/PreferencePanes/DateAndTime.prefPane
...

Provided you found, with a little trial and error, the right file in /System/Library/PreferencePanes/ first.

I'm sure there's a more cocoa way to do this last trick, still... this one works with every language.

Also: you may want to check these paths

/Library/PreferencePanes/
~/Library/PreferencePanes/

...as that's where third party apps install their *.prefPane files


How exactly did you use the Scripting Bridge?

I tried with this code and I think it performs reasonably well:

SystemPreferencesApplication *SystemPreferences = [SBApplication applicationWithBundleIdentifier:@"com.apple.systempreferences"];
@try {
    [SystemPreferences activate];
    SystemPreferences.currentPane = [SystemPreferences.panes objectWithID:@"com.apple.preference.security"];
} @catch (NSException *exception) {
    NSLog(@"%@", [exception description]);
}

Here is another option just for fun which is Cocoa, but not documented at all (and only works with system preference panes). You may use it to compare performances, but don't use it in production code.

id bezelServicesTask = [NSConnection rootProxyForConnectionWithRegisteredName:@"com.apple.BezelServices" host:nil];
[bezelServicesTask performSelector:@selector(launchSystemPreferences:) withObject:@"Security.prefPane"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜