I’m trying to figure out how to retrieve the text from a NSTextView and use it as a string. I’m using a text view because I need to have the vertical scroll bar to enter more text than what can be displayed for the room I have.
I’m can put text into the text view with setString_(“This is texts”) however when I use string(sender) as string to retrieve what’s in the text view I get “Can’t make string («class ocid» id «data kptr00000000808E560002000000») of «class ocid» id «data kptr000000006085550002000000» into type string. (error -1700)” Obviously I can’t turn this class into a string directly… How do I retrieve text from a text view and be able to use it as a string??
Hi,
the element containing the text is a NSTextStorage object which is a subClass of NSMutableAttributedString
In Objective-C you can get the string value with
[[textView textStorage] string]
textView represents the NSTextView outlet
I don’t know Obj-C yet… I did find out before your post that I was able to log to the console with the textStorage(sender). I have a simple window with a text view and a button. textView is the outlet for the text view and the Clicked_(sender) is connected to the button.
on Clicked_(sender)
textView’s setString_(“This is texts”) --this places text into the text view
set x to textView’s textStorage(sender)
log x – this will log the text from the text view
display dialog (x as string) – this will error
end Clicked_
I don’t know ASOC, but try something like
set x to textView's textStorage's string()
Edit: after doing some research, this should do it
set textStorage to textView's textStorage()
set theText to textStorage's |string|()
This works…
on Clicked_(sender)
textView’s setString_(“This is text”)
set textStorage to textView’s textStorage()
set theText to textStorage’s |string|()
display dialog (theText as string)
end Clicked_
What does the pipes around |string| indicate? That it is an AS keyword?
Thanks,
Indeed it is
Is there anyway to do the same thing with bindings. I’ve tried things similar to this code but can figure it out. I have the textView property bound to the Text View Bindings > Attributed String.
Thanks,
script Info_from_text_viewAppDelegate
property parent : class “NSObject”
property textView : missing value --bound property
on btnClicked_(sender)
set textStorage to (my textView's textStorage())
set theText to textStorage's |string|()
display dialog (theText as string)
end btnClicked_
end script
FYI, a multi-line text field will also scroll, although without scroll bars.
You can get the length of the bound attributed string.