I’m trying to make a simple app (just for practice) that reads aloud some custom text.
However I can’t work out how to get the contents of a textview into a variable.
After some searching around and some guess work this is as far as i’ve gotten
on clicked theObject
set varname to the contents of text view “saybox” of scroll view “view” of tab view “tabview” of window “main”
display dialog varname
The code compiles but when i click the button that i would like to display a dialog repeating to me the contents of the textview i get dialog with the following error;
NSReceiverEvaluationScriptError: 4 (1)
I have no idea what that means or for that matter how far off my code is.
For all i know i could have just messed up with identifying the textview. is there a way to skip out the long list of "of"s as it seems an easy point for making mistakes and i don’t know how to check whether i’ve gotten it right.
i just got done writing 2 programs the delt with tabs… i think that your text view “saybox” should be text view item.
here is some sample code that worked for me in my programs.
set the contents of text field "toAdd_Count" of tab view item "Counter_Tab" of tab view "tabView1" of window "mainWindow" to setNumber
tell tab view item "Notes_Tab" of tab view "tabView1" of window "mainWindow"
set theExport to the contents of text view 1 of scroll view "Notes"
thanks, but i don’t seem to be any closer to getting the content of the text box into a variable. It compiles but when i try execute it i get an error “NSReceiverEvaluationScriptError: 4 (1)”… whatever that means
here’s what i have now
on clicked theObject
set Alert to the contents of text view “sayBox” of tab view item “sayTab” of tab view “tabView” of window “mainWindow” (this line is the problem)
display dialog Alert
also here is a screenshot with UI element inspector’s take on the text box i’m working with.
A “NSReceiverEvaluationScriptError: 4 (1)” error means that you are referencing an item incorrectly… i.e. you have not provided the correct name(s) or an object doesn’t exist at the reference you’ve provided.
A text view is automatically placed inside of a scroll view when dragged from the palette. If you click once on the ‘text view’, you have actually selected the scroll view that contains the text view. By then double-clicking on the scroll view, you will select the text view itself. Make sure BOTH are named with an AS name, and referenced correctly.
dmccoy26 was probably close in his posted solution, but you may have missed the scroll view part. If you intend to write it all in one line, you’ll have to use something like this…
set Alert to the contents of text view "sayBox" of scroll view "sayScroll" of tab view item "sayTab" of tab view "tabView" of window "mainWindow"
…where ‘sayScroll’ is whatever the name of the scroll view is. You may also need to set ‘Alert’ to the content of the text view rather than the ‘contents’ to get the actual text out of it.
Also, you do not need to predefine/declare variables in applescript (if that’s what your doing with the “local Alert” statement) as you do in other languages. The variable is automatically considered local within the scope of the handler, and is released after the handler is finished executing.