Need help returning values and setting popup button index

Hi guys,

A couple months back I created a preference pane for QuickTime X. Everything works as it should, but I have been unable to get the popup button menu to show a correct initial value (It defaults back to “Enabled” on everything once System Preferences is closed). I promised an update to users, but I can’t seem to get this problem fixed so I can finally complete the app.

Basically, I would like to run an AppleScript for the purpose of reading the QuickTimeX defaults, then return that value for the GUI to read and apply to its menus. I already know how to run the AppleScript, but from there I am completely lost.

This is the code I use to run scripts:

{
	NSDictionary* errorDict; NSAppleEventDescriptor* returnDescriptor = NULL;
	NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: @"\
								   do shell script \"defaults read com.apple.QuickTimePlayerX MGPlayMovieOnOpen\""];
	returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; [scriptObject release];
	if (returnDescriptor != NULL) {
	}
}

I need that value for the purpose of applying it to the bindings in Interface Builder. I’d like to set “Enabled” (Menu Index 1) or “Disabled” (Menu Index 2) to the initial item shown when the preference pane is opened. If you haven’t seen my preference pane yet, a screen shot can be seen at http://megabytecomp.com/apps.aspx. You are also welcome to download it as well from that same page.

Whomever can help me get this working (or at least point me in the right direction) will be given credit on the preference pane as a thank you. Thanks for taking time out to help me with this, I really appreciate it!

Model: MacBook Pro 2.4 GHz, 4GB DDR3 RAM
AppleScript: 2.3
Browser: Firefox 3.5.4
Operating System: Mac OS X (10.6)

Because your app requires 10.6, there’s no need to muck around with NSAppleScript. You can add an AppleScript class, write an AppleScript handler, and call it from your Objective-C. But if all your script is doing is calling “do shell script”, can’t you just do it without AS as an NSTask?

Hi Shane, thanks for the reply. I didn’t think about NSTask, that’s a good point. How would I return the value from that? I haven’t done much work outside of AppleScript, and I’m still learning about coding in general.

Try this.

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/com.apple.QuickTimePlayerX.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *value = [dict objectForKey:@"MGPlayMovieOnOpen"];

Thanks Craig! I get a warning in Xcode “Control reaches end of non-void function” when I compile it. What kind of -()name:(id) does this code have to go under for me to be able to bind it with menu items in Interface Builder? I understand what the code does, I just don’t know how to tie it all together.

After playing around a bit, I actually got something to show up in Interface Builder, which means I did something right when setting the function.

- (void)setValue:(NSValueTransformer *)autoPlay
{
	NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/com.apple.QuickTimePlayerX.plist"];
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
	NSString *value = [dict objectForKey:@"MGPlayMovieOnOpen"];
}

Using that code autofills “autoPlay” into the “Model Key Path” box for the PopUp Button Cell’s bindings. What else needs to be done?

I am not understanding what you are doing. What are you binding to the popup?
Are you using an NSArray? That is what should be modified with the pref value.

Bind the array to the popup and then when the array is modified, the values
in the popup reflect the changes.

I just want the initial menu option in the preference pane to reflect what the user preferences for QuickTime say. So, if Autoplay is enabled in the preference file, I want the “Enabled” menu option to populate the cell. The code you gave me will return either a 1 or a 0, so how do I take that number and assign a menu item to each one? I need 1 to be Enabled and 0 to be Disabled, so whichever one it is will get returned and immediately read by the popup button.

This is all just to set the values of the menus when the preference pane opens. I hope I’m explaining this well enough, cause I’m not sure how else to say it.

I just saw an example of an NSArray, so I see what you’re saying now. I’m going to give that a quick try and see what happens.