I've written a ruby script that I am now looking at wrapping up in a preference pane for the purpose of configuring my ruby script. Now I was hoping that there would be a way for me to run some arbitrary commands when this preference pane gets installed in order to install necessary ruby gems etc, however I can't seem to find any documen开发者_如何学编程tation on this sort of functionality anywhere.
Any ideas would be appreciated.
Cheers Nick
You could just call a ruby script that performs any necessary configurations for you. If you need it to install gems, you'll probably need to run it with elevated privileges. This is how I've done it in the past:
// Get the path to the helper script from the main app bundle
NSBundle *appBundle = [NSBundle mainBundle];
NSString *scriptPath = [appBundle pathForResource: @"helperScript"
ofType: @"rb"];
// Construct the applescript command to run to helper script with elevated privileges
NSString *scriptCommand = [NSString stringWithFormat: @"do shell script \"ruby '%@'\" with administrator privileges", scriptPath];
// Execute the helper script via Applescript, log any errors
NSAppleScript *executeRubyScript = [[NSAppleScript alloc] initWithSource: scriptCommand];
NSDictionary *error = [[NSDictionary alloc] init];
[executeRubyScript executeAndReturnError: &error];
[executeRubyScript release];
精彩评论