Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Trimming active selection

Featured Replies

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.

 

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.

 

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 by comment

  • 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] 

)

)

 

Not sure where you're going with this.

 

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.