Page 1 of 1
Selected text in Editor gadGet
Posted: Tue Aug 05, 2014 10:40 pm
by spacebuddy
I am trying to get the selected text in the Editor Gadget, I have tried this but failed
Selected=CocoaMessage(0,GadgetID(0),"selectedRange")
S=CocoaMessage(0,GadgetID(0),"substringWithRange:",Selected)
Any help would be appreciated

Re: Selected text in Editor gadGet
Posted: Tue Aug 05, 2014 11:40 pm
by WilliamL
I would be interested in an answer too!
But if you know the range then couldn't you just GetGadgetText() and extract it with Result$ = Mid(String$, StartPosition [, Length]) ?
Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 12:22 am
by spacebuddy
problems is getting the range, my code has errors

Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 6:15 am
by wilbert
Getting the range
Code: Select all
CocoaMessage(@Range.NSRange, GadgetID(0), "selectedRange")
Debug Mid(GetGadgetText(0), Range\location + 1, Range\length)
An alternative might be to copy the selected text to the clipboard and get it from there
Code: Select all
CocoaMessage(0, GadgetID(0), "copy:", 0)
Debug GetClipboardText()
Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 6:46 am
by spacebuddy
Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 8:46 am
by Danilo
Code: Select all
Procedure.s GetSelectedText(editorGadget)
Protected Range.NSRange, result.s, nsString
CocoaMessage(@Range.NSRange, GadgetID(editorGadget), "selectedRange")
nsString=CocoaMessage(0,CocoaMessage(0,GadgetID(editorGadget),"string"),"substringWithRange:@",@Range)
If nsString
result = PeekS(CocoaMessage(0, nsString, "UTF8String"), -1, #PB_UTF8)
EndIf
ProcedureReturn result
EndProcedure
Procedure.s GetSelectedTextRTF(editorGadget)
Protected Range.NSRange, result.s, length, theData, nsData
CocoaMessage(@Range.NSRange, GadgetID(editorGadget), "selectedRange")
CocoaMessage(@nsData,GadgetID(editorGadget),"RTFFromRange:@",@Range)
If nsData
length = CocoaMessage(0,nsData,"length")
theData = CocoaMessage(0,nsData,"bytes")
result = PeekS(theData,length,#PB_UTF8)
EndIf
ProcedureReturn result
EndProcedure
Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 4:52 pm
by WilliamL
That's very interesting! (and useful)
I understand now that you were trying to get the text that was selected and not selecting the text
I will add this to my collection of snippets.
Re: Selected text in Editor gadGet
Posted: Wed Aug 06, 2014 6:27 pm
by spacebuddy
Wilbert,
I got it working, I put the code into a Thread so it can calculate the number of words the user has selected. So, in the Text Editor, whenever they highlight any words
it does a count of those words in realtime.
Here is the code
Procedure Monitor(Parameter)
Repeat
CocoaMessage(@Range.NSRange, GadgetID(Notes\Editors[CurrentIndex] ), "selectedRange")
If Range\Length>0
a$=Mid(GetGadgetText(Notes\Editors[CurrentIndex]), Range\location + 1, Range\length)
NumWords=CountWords(@a$)
a$=""
StatusBarText(0,4,"Words: "+Str(NumWords))
Else
StatusBarText(0,4,"Words: 0")
EndIf
Delay(1000)
ForEver
EndProcedure
Not sure if I should have a Delay in there, but it has not crashed yet
