GUI limitations?

Question #1

Is there any way to create GUI elements (like radio buttons or tabs) on the fly at run time using xcode or Interface Builder?

My searches on this forum suggest that there isn’t but that seems like a huge oversight on apple’s part so it’s kind of hard to believe. Also, none of the books I read about xcode or studio say that you can’t and you’d think that they would have.

Question #2

Is there another way to use apple’s GUI without using xcode or IB that would give me the freedom to create buttons (for instance) at run time?

I know I can create GUI during run time using Java and Java GUI elements and I know that I can use Java with xcode and IB but it seems like the only way to use apple’s aqua is through interface builder where everything has to defined ahead of time.

I’m writing a program that will read our models and their options from a database and then present them to the user. Imagine a car dealership. You log on. Choose a model (from a changing list of models) and you are presented with options for that model (from a changing or evolving list of options). I could use drop down lists (and I might) because their contents can be defined at run time but I’d definitley prefer radio buttons because it would be less painful to use for large lists.

Any ideas would be greatly appreciated.

Tom (btw I’m new to this stuff so go easy)

First, let me start by saying that Xcode can do anything. Granted, it can’t make toast or take over the world… but if you as a developer can dream something up, it has everything you need to write the software to support and facilitate your dream. That said, it may be that you’re simply in the wrong place looking for answers. While macscripter has some fine applescript developers, there are few of us here that can truly open the door on all that xcode has to offer. Applescript is not really meant for things such as those you request, so you’ll have to look deeper, or try another language. While applescript can get you halfway there, you’ll need obj-c to do much of the dirty work.

I posted some code some time back, probably mid-'04 that does exactly what you’re asking for. It may have been lost to time somehow, but it is certainly possible. Without making any assumptions as to you abilities or experience, I’ll say right off that the technique in ANY language is going to be complex and require a good deal of scripting or coding savvy. Any time you try to manage completely dynamic data sets, things can get quickly out of hand. That being said, below is the general idea for creating buttons on the fly and hooking them to applescript code. How you keep track of the buttons you create, managing the data that references them and their states, and all of the other glue that will hold your app together is completely up to you. This sounds like a daunting task, one that a simple post will not be able to answer for you. I’ll post the meat of what I came up with in my test project back then, and can answer questions you may have if you decide that you want to continue down this road.

Place this subscript at the top of your main AS script. It will catch all of the dynamic buttons that are clicked. Edit this to execute all of your actions related to them and their changes…

script catchButtons
	on clicked theObject
		display dialog ((name of theObject & ": " & ((state of theObject) as boolean)) as string)
	end clicked
end script

Use this code to create the new button in applescript, using a call to your custom “DYNobjects” class…

set DYNobjects to call method "alloc" of class "DYNobjects"

set contentView to content view of window "Main"
set newButton to call method "createButtonIncontentView:frame:state:continuous:" of DYNobjects with parameters {contentView, {20, 42, 120, 142}, 0, true}
set name of newButton to "Test"
set title of newButton to "My Button"
set script of newButton to catchButtons

This is the method that creates the dynamic buttons. You can place this all in one file (DYNobjects.m) or split it into two (.h & .m) if you wish…

Note that the code above is really rough, and is just cut&pasted from a working app. The process is pretty straightforward, but does require some pretty significant understanding of app development not only accomplish, but to then manage. I didn’t go crazy making everything look pretty and explain everything, because in order to make this all work, you’ll have to have the skills necessary to clean up the code and make sense of it yourself, anyways. I just put in the important bits so you could begin to get an idea of the seriousness of the undertaking.

The reason that you don’t see a lot of people doing this is because it get’s pretty complex to manage. If you only have a few options, you may find this useful. The more options you add, the more I’d recommend going in another direction. In fact, I’d pretty much just recommend that you look at other options, regardless. Popup buttons, fixed-length matrices with unused items disabled or invisible, or table views with a button cell column all come to mind, and are much easier to manage.


Thanks, Jobu. You’re good.

It’ll take me a while to absorb all of that code but I have some time on my hands. I’ve already started building some parts of my program in Java just to test the concept and I might wind up keeping it that way but I love learning new languages and systems so I’m going to stick with this for a little longer.