Fastest way to open prefpane programmatically?

AppleScript was too slow, so I tried ScriptingBridge to open System Preferences.app and set the current panel, which is also too slow. Is there a faster way to do this? correctly

+7
objective-c cocoa macos
source share
4 answers

Use Launch Services or NSWorkspace to open the prefpane package. This is the software version of the open (1) command.

+5
source share

A more direct method than using the file system path is to use the corresponding resource URL for the preference panel using NSWorkspace, as shown below:

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

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

+8
source share

No problems:

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

And choose which panel to open:

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

If you find that with a little trial and error, the correct file is in /System/Library/PreferencePanes/ first.

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

Also: you can check these paths

 /Library/PreferencePanes/ ~/Library/PreferencePanes/ 

... like the one where third-party applications install their *.prefPane files

+5
source share

How exactly did you use the script bridge?

I tried with this code and I think it works quite 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 entertainment option that Cocoa has but is not documented at all (and only works with system preference panels). You can use it to compare characteristics, but do not use it in production code.

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

All Articles