Saturday, December 16, 2017

#1 2009-11-01 09:55:33 pm

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

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:

Applescript:

{
   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)

Offline

 

#2 2009-11-01 10:20:06 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: Need help returning values and setting popup button index

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?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2009-11-01 10:27:00 pm

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

Re: Need help returning values and setting popup button index

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.

Offline

 

#4 2009-11-01 10:31:07 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Need help returning values and setting popup button index

Try this.

Applescript:

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

Offline

 

#5 2009-11-01 11:12:05 pm

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

Re: Need help returning values and setting popup button index

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.

Offline

 

#6 2009-11-01 11:39:55 pm

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

Re: Need help returning values and setting popup button index

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.

Applescript:

- (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?

Offline

 

#7 2009-11-01 11:50:14 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: Need help returning values and setting popup button index

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.

Offline

 

#8 2009-11-02 01:21:21 am

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

Re: Need help returning values and setting popup button index

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.

Offline

 

#9 2009-11-02 01:27:34 am

Megabyte Computing
Member
Registered: 2009-11-02
Posts: 7

Re: Need help returning values and setting popup button index

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)