Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.filemaker > #3541 > unrolled thread

Script to Format Selaction in a Text Field

Started byJack Nastyface <nowhere@noplace.com>
First post2025-05-26 18:58 -0400
Last post2025-05-27 15:30 +0200
Articles 4 — 2 participants

Back to article view | Back to comp.databases.filemaker


Contents

  Script to Format Selaction in a Text Field Jack Nastyface <nowhere@noplace.com> - 2025-05-26 18:58 -0400
    Re: Script to Format Selaction in a Text Field Martin Τrautmann <t-usenet@gmx.net> - 2025-05-27 08:51 +0200
      Re: Script to Format Selaction in a Text Field Jack Nastyface <nowhere@noplace.com> - 2025-05-27 08:14 -0400
        Re: Script to Format Selaction in a Text Field Martin Τrautmann <t-usenet@gmx.net> - 2025-05-27 15:30 +0200

#3541 — Script to Format Selaction in a Text Field

FromJack Nastyface <nowhere@noplace.com>
Date2025-05-26 18:58 -0400
SubjectScript to Format Selaction in a Text Field
Message-ID<0001HW.2DE5299B01544BBC309C3E38F@news.astraweb.com>
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

[toc] | [next] | [standalone]


#3542

FromMartin Τrautmann <t-usenet@gmx.net>
Date2025-05-27 08:51 +0200
Message-ID<slrn103ao6r.4je.t-usenet@ID-685.user.individual.de>
In reply to#3541
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

[toc] | [prev] | [next] | [standalone]


#3543

FromJack Nastyface <nowhere@noplace.com>
Date2025-05-27 08:14 -0400
Message-ID<0001HW.2DE5E40D017FFE57306CF638F@news.astraweb.com>
In reply to#3542
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

[toc] | [prev] | [next] | [standalone]


#3544

FromMartin Τrautmann <t-usenet@gmx.net>
Date2025-05-27 15:30 +0200
Message-ID<slrn103bfj2.4je.t-usenet@ID-685.user.individual.de>
In reply to#3543
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? :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.databases.filemaker


csiph-web