Text View woes

Hi Everyone,

I’ve gone over so many examples that look exactly like my code here I simply can’t figure out what I’m doing wrong.

I have a single window with one button and one NSTextView. The button is called “test”, the textview is called “view” and the window is called “main” in interface builder. I connected the button to the only script which is this:


on clicked theObject

	if the name of theObject is "test" then
		log "test button pressed."
		set the contents of text view "view" of scroll view "view" of window "main" to "Check it out!"
		log "set view code executed"
	end if
	
end clicked

I get no errors when the program builds and no errors when I hit the “test” button … although nothing happens at all when I hit the button other than it flashing.

Checking the run tab for my log statements, I see both of them in there for every click

So what gives? I have a blind eye to the problem … help!

Anyone?

Anyone?

Bueller?

I thought it was something like this when i first came across the problem, but gave up on it.

I didn’t name the text view, only the scroll view. I didn’t know how to get to the attributes of the text view, and had only named the scroll view.

I hadn’t seen those two little buttons on IB’s project window, the list view helps me see everything much better.


on clicked theObject

	if the name of theObject is "test" then
		log "test button pressed."
		set the contents of text view "view" of scroll view "view" of window "main" to "Check it out!"
		log "set view code executed"
	end if
	
end clicked

Did you have any luck with this as I am having the exact same problem.

Also, is there an Apple doco detailing how to reference the various Cocoa GUI components from AppleScript within AppleScript Studio?

Regards

If you haven’t named both the text view and the scroll view (in IB, click once on the item to set the name of the scroll view via the AppleScript pane on the info palette, then double-click on the item to set the name of the text view), you can still reference the text view by its index:

set the contents of text view 1 of scroll view "view" of window "main" to "Check it out!" 

Jon

I just post to help me learn, not to answer sorry :-).

So why you doesn’t get the window of the button directely and ask directely to the window the named component? Like (I don’t adapt the code to your case in order to not add many errors):


on clicked theObject
	log "clicked:" & name of theObject
	if name of theObject is "OK" then
		tell window of theObject
			try
				set myVar to contents of text field "textFieldName" as text
			on error error_message number error_number
				set this_error to "Error: " & error_number & ". " & error_message & return
				display dialog "error in on clicked button OK: " & this_error
			end try
		end tell
		doStuff()
		close panel (window of theObject)
	else
		log "not handle clicked for " & name of theObject
	end if
end clicked

although the documentation states this syntax:

set contents of yourTextView to "foo"

I have never had luck with that, but with this it works:

set content of yourTextView to "foo"

content instead of contents

See also: Text View Suite