• Script to Format Selaction in a Text Field

    From Jack Nastyface@21:1/5 to All on Mon May 26 18:58:35 2025
    I want to color some text in a text field using a script but I can’t figure out a way to do it. There doesn’t seem to be an applicable script step. Any ideal?

    Thanks

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Martin =?UTF-8?Q?=CE=A4rautmann?=@21:1/5 to Jack Nastyface on Tue May 27 08:51:07 2025
    On Mon, 26 May 2025 18:58:35 -0400, Jack Nastyface wrote:
    I want to color some text in a text field using a script but I can’t figure out a way to do it. There doesn’t seem to be an applicable script step. Any ideal?

    You want to select some part of the text only?

    I guess the main problem is not to leave focus on the current location.

    I just tried and found with a current FMP version that you stay on your
    current editing location if you go to another file. So create a new file "Formatting" with just one text field "RTF", maybe global storage.
    Script:

    * Cut text
    * Goto File "Formatting"
    * Paste to field "RTF" [select entire contents)

    # check for selected text
    * if ( length(RTF) = 0)
    ...go back to the original file and select the entire text field. If the
    length of the original text field = 0, exit the script there. Otherwise
    it will be an endless loop.


    Text formatting then can be applied on that field RTF, such as
    * TextColor (RTF; RGB())
    * TextFont (RTF; fontname)
    * TextSize (RTF; size)
    * TextStyleAdd (RTF, ...) e.g. italic+doubleunderline

    * cut RTF
    * go back to original file
    * paste

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jack Nastyface@21:1/5 to All on Tue May 27 08:14:05 2025
    On May 27, 2025, Martin Τrautmann wrote
    (in article<slrn103ao6r.4je.t-usenet@ID-685.user.individual.de>):

    On Mon, 26 May 2025 18:58:35 -0400, Jack Nastyface wrote:
    I want to color some text in a text field using a script but I can’t figure
    out a way to do it. There doesn’t seem to be an applicable script step. Any
    ideal?

    You want to select some part of the text only?

    [snip]

    Martin, thank you so much for your reply. I wasn’t sure anyone was still monitoring this group :) Yes, I want to only select a part of the text. While
    I was waiting for a reply, I asked chatGPT to write a script for me. The
    result does exactly what I wanted. However it had an “off by one” error
    in it. LOL Nice to know chatGPT makes those kind of errors too. Here is the script:

    # Assumes the field is named "YourTextField"
    # Make sure to enable "Select entire contents" is OFF in the field settings
    # This only works if the field s active and the user has selected text
    Set Variable [ $start ; Value: Get ( ActiveSelectionStart ) ]
    Set Variable [ $size ;Value: Get ( ActiveSelectionSize ) ]
    Set Variable [ $text ; Value: Get ( ActiveFieldContents ) ]

    If [ $size> 0 ]
    Set Variable [ $before ; Value: Middle ( $text ; 1 ; $start ) ]
    Set Variable [ $selected ; Value: Middle ( $text ; $start + 1 ; $size ) ]
    Set Variable [ $after ; Value: Middle ( $text ; $start + $size + 1 ; Length ( $text ) ) ] Set Variable [ $coloredText ; Value: $before& TextColor (
    $selected ; RGB ( 255 ; 0 ; 0 ) ) & $after ]
    Set Field [ YourTable::YourTextField ; $coloredText ]
    Else
    Show Custom Dialog [ "No Text Selected" ; "Please select some text in the field before running this script." ]
    End If

    Notes: Replace YourTable::YourTextField with the actual table and field name. This script assumes the field is being edited when the script is triggered. For rich text, this formatting will apply and be visible if the field supports styled text (e.g., a Text field on a layout).

    The “off by 1” error is in the first line which should read

    Set Variable [ $start ; Value: Get ( ActiveSelectionStart ) -1 ]
    Jack

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Martin =?UTF-8?Q?=CE=A4rautmann?=@21:1/5 to Jack Nastyface on Tue May 27 15:30:09 2025
    On Tue, 27 May 2025 08:14:05 -0400, Jack Nastyface wrote:
    Martin, thank you so much for your reply. I wasn’t sure anyone was still monitoring this group :) Yes, I want to only select a part of the text. While I was waiting for a reply, I asked chatGPT to write a script for me. The result does exactly what I wanted. However it had an “off by one” error in it. LOL Nice to know chatGPT makes those kind of errors too. Here is the script:

    Nevertheless good to know that chatGPT does some decent work

    # Assumes the field is named "YourTextField"
    # Make sure to enable "Select entire contents" is OFF in the field settings >> # This only works if the field s active and the user has selected text
    Set Variable [ $start ; Value: Get ( ActiveSelectionStart ) ]
    Set Variable [ $size ;Value: Get ( ActiveSelectionSize ) ]
    Set Variable [ $text ; Value: Get ( ActiveFieldContents ) ]

    well chosen and documented

    If [ $size> 0 ]
    Set Variable [ $before ; Value: Middle ( $text ; 1 ; $start ) ]
    Set Variable [ $selected ; Value: Middle ( $text ; $start + 1 ; $size ) ]
    Set Variable [ $after ; Value: Middle ( $text ; $start + $size + 1 ; Length ( $text ) ) ]
    Set Variable [ $coloredText ; Value: $before& TextColor (
    $selected ; RGB ( 255 ; 0 ; 0 ) ) & $after ]
    Set Field [ YourTable::YourTextField ; $coloredText ]

    Here's a certain weakness since it does work on a certain field only.
    The approach which I suggested would work on any field you are currently
    in. But you could script to set the active field name for another
    variable and select the field then again.

    Else
    Show Custom Dialog [ "No Text Selected" ; "Please select some text in the field before running this script." ]
    End If

    One of the options. Instead you might use a dialog to cancel or do the operation on the full text instead.

    Notes: Replace YourTable::YourTextField with the actual table and field name.
    This script assumes the field is being edited when the script is triggered. >> For rich text, this formatting will apply and be visible if the field supports styled text (e.g., a Text field on a layout).

    The “off by 1” error is in the first line which should read

    Set Variable [ $start ; Value: Get ( ActiveSelectionStart ) -1 ]

    a typical and minor mistake, solved easily by debugging. You now might google some of the script steps whether this good solution had been written by
    someone else before. Obviously, this solution here does apply colors
    only, while there are other formatting options, too.

    So thanks for the chatGPT hint - this might simplify future script
    editing if you can copy/paste those scripts directly.

    How well does it work if you tell chatGPT to translate that script for
    German language setup? :-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)