Radio Buttons User Defaults and Values

Dear All

I confess to being pretty confused by how to get useful values from a radio button matrix when combined with user defaults. Before trying to use Shared User Defaults I had radio buttons that were bound using selected value. That way I could write code that would go something like:


if buttonPressed is equal to "Button one" then
-- do something
else if buttonPressed is equal to "Button two" then
-- do something else
end if

However, when I bind the matrix to Shared User Defaults Controller my script only works when run subsequently if I re-click the buttons. Not the best!

So I tried doing it programmatically as detailed in Shane Stanley’s book. I can get buttons to save to preferences but can’t work out how to get the value of the button in the same way that I can with Cocoa bindings. I can only get the selected row. I tried using this line (full script below:

        tell myDefaults to set theValue to valueForKey_("oneTwoButtonsChoice")

but I still get an integer returned.

Is there a way I can get the values other than programming so that for instance 2 = Buckle?

Thanks for any pointers.


In Xcode I have 5 buttons with values:
One
Two
Buckle
My
Shoe

I then have a button which when clicked runs doTest_()


script AppDelegate
	property parent : class "NSObject"
	property myDefaults : missing value -- contains preferences
	property oneTwoButtons : missing value -- referencing outlet for the radio button matrix
	property theChoice : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		tell current application's NSUserDefaults to set myDefaults to standardUserDefaults()
		tell myDefaults to registerDefaults_({oneTwoButtonsChoice:0}) -- set the defaults
		retrieveDefaults_(me) -- set the intitial button choice
		
		-- Insert code here to initialize your application before any files are opened
	end applicationWillFinishLaunching_
	
	
	on doTest_(sender)
		saveDefaults_(me)
		tell myDefaults to set theValue to valueForKey_("oneTwoButtonsChoice")
		display dialog theValue as text
		
	end doTest_
	
	
	
	on retrieveDefaults_(sender)
		tell myDefaults to set theChoice to objectForKey_("oneTwoButtonsChoice") -- get the row that was selected, either in the defaults or the saved preferences
		tell oneTwoButtons to selectCellAtRow_column_(theChoice, 0)
	end retrieveDefaults_
	
	
	on saveDefaults_(sender)
		set theChoice to oneTwoButtons's selectedRow()
		tell myDefaults to setObject_forKey_(theChoice, "oneTwoButtonsChoice")
	end saveDefaults_
	
	
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

Model: MacBook Pro 2.7 GHz Core i7 16GB RAM
Browser: Safari 537.31
Operating System: Mac OS X (10.8)

I’m not sure exactly what you’re asking, but you can get selectedCell()'s stringValue().

Hi Shane

Thanks for replying. Maybe selectedCell is what I need because I would like to get the value of the string of the radio button selected. I had a look at the documentation and it says selectedCell is for the most recently selected radio button. And in any case I tried coding it in various ways without success!

I tried:


set theValue to current application's selectedCell()'s stringValue()
set theValue to current application's NSMatrix's selectedCell()'s stringValue()
set my theValue to selectedCell()'s stringValue() 

What I want to do ultimately is (by way of radio buttons) set up some initial values for a script to run. These would include one set to decide whether to do all, selected or current document; and another set to decide whether to choose location or file automatically.

Feeling pretty thick right now!

I’m still not following exactly, but if you get the row and/or column, you can then get cellAtRow_Column_, and from that stringValue().

I managed to get something from that, but still only an integer. I had this in the handler activated by clicking a button:

    
on doTest_(sender)        
        tell oneTwoButtons to set theCellSelected to cellAtRow_column_(theChoice, 0)
        log theCellSelected
        set theValue to theCellSelected's stringValue()
        log theValue as text
    end doTest_

The radio button group has it’s selected value bound to the App Delegate.

If I don’t click any of the buttons before activating the handler I get:
2014-02-10 14:33:13.180 dfihudfhiuo[42439:303] <NSButtonCell: 0x10014df50>
2014-02-10 14:33:13.181 dfihudfhiuo[42439:303] 1

If I click a radio button I get:
2014-02-10 14:34:11.883 dfihudfhiuo[42439:303] -[__NSCFString cellAtRow:column:]: unrecognized selector sent to instance 0x100152440
2014-02-10 14:34:11.887 dfihudfhiuo[42439:303] *** -[AppDelegate doTest:]: -[__NSCFString cellAtRow:column:]: unrecognized selector sent to instance 0x100152440 (error -10000)

That’s telling you that oneTwoButtons is a string, and not an outlet to a matrix.

Okay. I took a look and removed the bound values from the Connections inspector, leaving only the referencing outlet. Now (I think this is progress) I don’t get the error message. But.I don’t get a string, just an integer. And its the same for every button:
2014-02-10 14:52:10.333 dfihudfhiuo[42533:303] <NSButtonCell: 0x104b2a890>
2014-02-10 14:52:10.334 dfihudfhiuo[42533:303] 1
2014-02-10 14:52:22.460 dfihudfhiuo[42533:303] <NSButtonCell: 0x101aa4970>
2014-02-10 14:52:22.461 dfihudfhiuo[42533:303] 1
2014-02-10 14:52:43.060 dfihudfhiuo[42533:303] <NSButtonCell: 0x104b2a6b0>
2014-02-10 14:52:43.060 dfihudfhiuo[42533:303] 1
2014-02-10 14:52:45.228 dfihudfhiuo[42533:303] <NSButtonCell: 0x104b2a890>
2014-02-10 14:52:45.228 dfihudfhiuo[42533:303] 1

OK, the docs suggest you should ask an NSButtonCell for its title().

Thank you very much Shane “ it works!

The code for the handler is now this:


on doTest_(sender)
	saveDefaults_(me)
	tell oneTwoButtons to set theCellSelected to cellAtRow_column_(theChoice, 0)
	log theCellSelected
	set theValue to theCellSelected's title()
	log theValue as text
end doTest_