3rd button state?

Hey everyone, is there a way to give a button a third state? I’m trying to make a repeat button like in iTunes but that has three states, and in IB you can only have 2.

Ok, well now I know I have to subclass NSButton…but once again thats Objective-C that I don’t know how to do…it shudnt be very long if all I am doing is drawing an extra state…anyone know the solution for this?

Hi Hendo,

Have a look at the documentation for NSButton in Xcode. You should be able to set three states: NSOnState, NSOffState, and NSMixedState. Apparently the mixed state must be enabled first in order to be used; do so by calling the method setAllowsMixedState:, passing true.
To get and set the button’s state, call the methods setState: and state.

Hope that helps as a starting point.

will be be able to do this through call methods? or will i actually have to sublcass the button? I can’t actually find where you set the image for the mixed state either :confused:


as far as I know, there is no extra image for the mixed state …
before subclassing … maybe an AppleScript solution like this is sufficient?

global buttonstate, buttonimgs

on awake from nib theObject
	set buttonimgs to {load image "1.png", load image "2.png", load image "3.png"}
	set buttonstate to 1
end awake from nib

on clicked theObject
	set buttonstate to buttonstate + 1
	if buttonstate > 3 then set buttonstate to 1
	set image of theObject to item buttonstate of buttonimgs
end clicked

Chosse a ‘momentary’ button behaviour and set 1.png as initial icon in Interface Builder.


This doesnt work…I set the button to momentary change…and its called “repeat” and i have it checked off as on clicked, and on awake from nib. On launch i immediately get an error saying no result was returned from some part of this expression…and then when i press the button it says the variable buttonstate is not defined…

hmm - try this - maybe I misunderstood sth …?


Thanks, I’ll take a look when I get home:cool:

Dominik, this was exactly what I was looking for. I found my problem, I had named by image wrong in the code. It all works now. Thanks :smiley:

so now that I got it to go through tme, I need to be able to sync it with the state of the repeat button in iTunes. When I did it for shuffle it was alot easier because the state of shuffle can be turned into an integer, but looking at the iTunes dictionary, song repeat can not.

Here’s the integer thing working:

tell application "iTunes"
	if (player state is stopped) then
		set repeatMode to null
		set repeatMode to current playlist's song repeat
	end if
	if (repeatMode is off) then
		set repeatModeIndex to 1
	else if (repeatMode is all) then
		set repeatModeIndex to 2
	else if (repeatMode is one) then
		set repeatModeIndex to 3
		set repeatModeIndex to 0
	end if
end tell

Note that I’ve set the index to 0 if iTunes is stopped, so that means that the button should be disabled.

Worked great :smiley: Thanks so much!