Text to text field after text already in it.

I need to add some text to a field but instead of overwriting the old text in it I want it to be add to the end of it. I tried using something like

set the contents of text field “blah” of window 1 to previous text of text field “blah” of window 1 & theVariable

I know it might be in the forums already but when searching for something as general as ‘text’ i’d have to look through tons of pages. Any help would be appreciated.

The contents haven’t changed yet, so contents is what you want:

set contents of text field "blah" of window 1 to contents of text field "blah" of window 1 & theVariable


tell text field "blah" of window 1 to set contents to contents & theVariable

Side note: You may want to use a name for the window instead of an index; If you have a preferences window or an “about” window in front, your script won’t find the right window.


tell text field "blah" of window 1 to set content to (content & theVariable)

*** EDIT ***
Bruce beat me to it!

Actually, I find it better practice to use the “content” property instead. There are some objects that return a reference to themselves, rather than a reference to the actual content (i.e. the string value) of the object when using the “contents” property. While there are few objects that actually exhibit this behavior, it can be afrustrating to troubleshoot the problem when you’re used to using “contents” and then you get a wierd error when you do encounter this conflict. “Content” always returns what you’d expect, so I’d recommend using that instead of ‘contents’.

Bruce, I thought of this too… and almost said it… but then I thought about why one WOULD want to use this approach. If you’re working with a document-based app then using “window 1” is appropriate… although I would recommend encapsulating it all in a “try” block, in case some other window happens to be in front. If you’re not in a doc-based window, then yes, naming your window is much better form.

*** END EDIT ***


I usually do this too. My main point was that all the values/variables/objects are evaluated first, then the object (whatever it is) is set.

To be honest, I’ve never made a document-based app, and I’ve hardly seen any Studio-based ones around.

I actually prefer to use the window and super view properties; I would think these work well in a document-based app too.

Yeah, I do that. I only put “window 1” to save some typing time.