February 17, 20214 yr In a given field there is a body of text: Quote the quick brown fox jumped over the lazy dog. ignoring any formatting if the active selection was the word fox and the active selection contained a leading and or trailing space. _fox or fox_ or _fox_ I would wish to trim the space just to the word fox and reset the active start and end selection before my script continues. Right now I have 3 very verbose IF sections in the script one that tests if there is a leading or trailing or both.
February 17, 20214 yr I don't think there is a truly elegant solution to this. If you can assume that the selection contains at most one leading and one trailing space, then you can use two If [] steps to (a) adjust the selection start if there is a leading space and (b) adjust the selection end if there is a trailing space. If the assumption cannot be made, then you need to loop.
February 17, 20214 yr On second thought: Set Variable [ $selectionStart; Value: Get ( ActiveSelectionStart ) ] Set Variable [ $selection; Value: Middle ( Get ( ActiveFieldContents ) ; $selectionStart ; Get ( ActiveSelectionSize ) ) ] Set Variable [ $text; Value: Trim ( $selection ) ] Set Variable [ $textStart; Value: $selectionStart + Position ( $selection ; $text ; 1 ; 1 ) - 1 ] Set Selection [ Start Position: $textStart; End Position: $textStart + Length ( $text ) - 1 ] Note that this assumes you have selected something besides spaces - otherwise it will move your selection to the last character of the preceding word, if one exists, or after the first space if it doesn't. Edited February 18, 20214 yr by comment
February 17, 20214 yr Author This is what i knocked together - use the json to set the selection. Let ( [ input = Get(ActiveFieldContents) ;start = Get(ActiveSelectionStart) ;size = Get(ActiveSelectionSize) ;text = Middle ( input ; start ; size ) // 0 ; 1 ; 2 ; 3 for space as none prefix suffix both ;sp = Case ( Left ( text ; 1 ) ≠ Char(32) and Right ( text ; 1 ) ≠ Char(32) ; 0 ; Left ( text ; 1 ) = Char(32) and Right ( text ; 1 ) = Char(32) ; 1 ; Left ( text ; 1 ) = Char(32) ; 2 ; Right( text ; 1 ) = Char(32) ; 3 ) ; start = Choose ( sp ; start ; start + 1 ; start + 1 ; start ) ; size = Choose ( sp ; size ; size - 2 ; size - 1 ; size - 1 ) ; end = start + size ] ; JSONSetElement ( "" ; [ "sp" ; sp ; JSONNumber] ;[ "start" ; start ; JSONNumber] ;[ "size" ; size ; JSONNumber] ;[ "end" ; end ; JSONNumber] ;[ "text" ; text ; JSONString] ;[ "selected" ; Trim ( text ) ; JSONString] ) )
Create an account or sign in to comment