views:

151

answers:

3

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

+2  A: 

No brainer:

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

And to choose a Pane to open:

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

Find the right file in /System/Library/PreferencePanes/ and open it.

I'm sure there's a more cocoa way to do it, still... works with every language.

ZJR
Sidenote: you may want also to check `~/Library/PreferencePanes/` and `/Library/PreferencePanes` that's where **third parties** install their panes.
ZJR
A: 

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"];
0xced
I have the exact same Scripting Bridge code as you (well, minus the exception code) - I took mine from the Apple sample. The weird thing is that if I double-click on the prefpane file already inside the PreferencePanes folder, System Preferences opens and loads the pane instantly.
K_T
+1  A: 

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

Peter Hosey
Indeed: `[[NSWorkspace sharedWorkspace] openFile:@"/Path/To/Foo.prefPane"];`
Max Howell
I'm not usually keen on this though as you have to figure out where your Third Party prefpane has been installed. Alas, using bundle identifiers with the appropriate NSWorkspace call doesn't seem to work.
Max Howell
There are only two places it could be, and you can use `NSSearchPathForDirectoriesInDomains` to look in both of them.
Peter Hosey