Selected text in Editor gadGet

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Selected text in Editor gadGet

Post by spacebuddy »

I am trying to get the selected text in the Editor Gadget, I have tried this but failed :cry:

Selected=CocoaMessage(0,GadgetID(0),"selectedRange")
S=CocoaMessage(0,GadgetID(0),"substringWithRange:",Selected)

Any help would be appreciated :D
WilliamL
Addict
Addict
Posts: 1255
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Selected text in Editor gadGet

Post 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]) ?
MacBook Pro-M1 (2021), Tahoe 26.1, PB 6.31b2
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Selected text in Editor gadGet

Post by spacebuddy »

problems is getting the range, my code has errors :oops:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Selected text in Editor gadGet

Post 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()
Windows (x64)
Raspberry Pi OS (Arm64)
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Selected text in Editor gadGet

Post by spacebuddy »

Thanks Wilbert, I will try to add this to my editor. I start my editor over cause to buggy, so I re-start :D :D :D
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Selected text in Editor gadGet

Post 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
WilliamL
Addict
Addict
Posts: 1255
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Selected text in Editor gadGet

Post 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.
MacBook Pro-M1 (2021), Tahoe 26.1, PB 6.31b2
spacebuddy
Enthusiast
Enthusiast
Posts: 364
Joined: Thu Jul 02, 2009 5:42 am

Re: Selected text in Editor gadGet

Post 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. :P

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 :)
Post Reply