How do I dynamically add a text field to a window?

First, a bit of background…

I’m trying to create a text field in the content region of a window. To try to keep it simple, I’m trying to create that field in response to the click of a button in a document-based AppleScript Studio project. That button was created in a window in Interface Builder. It appears properly and functions normally, and I have used it to successfully initiate other trial tasks, using its connection to the “on clicked” handler in the Document.applescript file.

Now, as to the problem I have…

I have tried several different scripts, all of which yield the same error. Here’s a typical scriptlet that characterizes my approach so far…


	-- Since the initiating message comes from the button and the field is to be created in 
	-- the same window as the button, get a reference to the window that contains 
	-- the button that sent the message.
	set theWindow to the window of theObject
	-- Create the new text field and keep a reference to it.
	set theNewField to make new text field at end of the text fields of theWindow with properties {bounds:{100, 100, 180, 132}}
	-- Make sure the user can see the newly-created field.
	update theWindow

There is already another text field in the window. I’m assuming that text field elements in a window are held in an array that is linked to the window object. I’m trying to add a text field object to that array, much like I might add a column to a data source object.

The error I get is, typically,

The error message sort of confirms that I’m trying to add to an array. The problem is that it appears that the array I’m trying to add it to is unmutable. That would say that I would need to do this with Cocoa code.

So… am I making this too complicated? What am I doing wrong?

Thanks very much for whatever you can recommend.

I’m using XCode 1.5, btw. Other system info follows…

Model: Powerbook G4 15" 1 GBy RAM
AppleScript: 1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.7)

To the best of my knowledge, you can’t create new controls in AppleScript alone (menus and menu items are a different matter…). To achieve what you want, you’ll need to write some Obj-C code and then you can call that from AppleScript to create your new control (text fields, buttons, whatever). Here’s some code for creating a text box that can’t be edited or selected and has no bezel or background:

If you added this method to an Obj-C class named “Methods” then you would call it from AppleScript like so:

set the_text_field to (call method "makeTextField:withFrame:" of class "Methods" with parameters {window "main", {5, 10, 20, 30}})

This would make a new text field at position {5, 10} in the window “main” with a width of 20 pixels and a height of 30 pixels. It would return the text field reference to you and then you can use AppleScript to further customize the box or assign AppleScript properties such as “name”.

Jon

Many thanks, jonn8. Haven’t tried your code yet, but everything you say makes sense. I’ll go after it today. In the meantime, have a great weekend.

Gratefully,
Lump

P.S. Gawd, I miss HyperCard.