Find Replace certain text in Clipboard with bold text?

I have a clipboard with a lot of text in it.
I can easily find a sentence of text in that clipboard with "Search and Replace Named Clipboard" but I want to replace that found sentence with a bold sentence that is stored in another clipboard.
I see the 'source' clipboard and it is bold
It does replace it, but it is not bold.
What am I missing here?
Id like clipboard 01 to end up as "not bold" (where the word bold is.... bolded)
Sorry about the size of the screenshots.

Keyboard Maestro “Search and Replace Clipboard” Action

Search and Replace Clipboard.kmactions (0.7 KB)

I'm pretty sure that the KM Action works ONLY with plain text.

  • There is one possible KM solution.
    • Split the source text into separate variables so that you have:
      • sourceBefore
      • sourceToBold
      • sourceAfter
    • Then set the System Clipboard to:
      %sourceBefore%%sourceToBold%%sourceAfter

Note that I have applied the bold form in the KM action to:
%sourceToBold%

I just tried this as a proof of concept, and it seems to work:

##example Results

Note that the text "text to bold"is actually in bold style.

##Macro Library   Combine Plain Text with Rich Text


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/2/2a854f8089b9956f2f8a2e0977e1f65570ff8d24.kmmacros">Combine Plain Text with Rich Text.kmmacros</a> (3.5 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

![jmOldOriginal|459x918](upload://iit8EoC1xAAUGz9pt8NKqDRrjd4.png)

The challenge now is to split the source text into its parts.  _Search Clipboard_ with RegEx should be able to do the job.
1 Like

The replacement is plain text, not styled text, so any styling of the named clipboard is lost.

It is probably possible to do it using a For Each action with the Substrings collection, tracking the location of the substrings, splitting it in to its component parts (before and after the match), and then joining them together with the Set Named Clipboard action. But it’s not trivial and today is Saturday and my brain is a bit too foggy to think that entirely through.

It’s not entirely impossible that I could improve the Search & Replace action to allow styled strings in the replacement, but it wont happen for 8.0.

1 Like

Another possible solution is using AppleScript with Tex-Edit Plus:

tell application "Tex-Edit Plus"
  tell front window
    tell selection
      set font to "Times"
      set size to 14
      set style to bold
      set color to black
      set first indent to 6
      set space below to 6
      set bottom border to none
      set justification to left
    end tell
  end tell
end tell

return

tell application "Tex-Edit Plus"
  tell front document
    tell paragraph 2
      set font to "Times"
      set size to 18
      set style to {bold, italic, underline}
    end tell
  end tell
end tell

It looks like TE+ also has RegEx support:

Just confirmed that TE+ fully supports RegEx search using AppleScript:

I tried working with Apple's TextEdit, but it is not working well for me.

Forget about scripting TextEdit, it is a POS, IMO.

It has terrible AppleScript syntax.

1 Like

I would prefer staying ‘native’ in KM and use regex if possible, thanx
Nice, getting there, now-
I tried to find the regex to find ‘string thru string’ but to no avail. How would I use regex to find
A string of text thru the next stated string of text i.e.
Example text is:

There are some words that are unique and I want to find a sub-string of that long string of words.

1- find ‘there are some’- thru and including 'I want to’
2- find ‘sub-string’ thru and including 'of words.'
I would be certain that the string part I’m looking for is completely unique.
So I could set those (1 and 2) as variables.

Super, lol, foggy Saturday, I know the feeling.
A great addition to the ‘hit’ list, stylized replacements, thank you!

If you want help with RegEx, then I need for you to post a range of real-world examples that will cover all possible real data sets, and, if you can, identify any key text that might be used as a delimiter.

Again, need real-world examples, not make up examples just for posting. If it contains sensitive data, then change it as needed without changing the name of the data. For example:

If source data contains a real person's name like:
William J. Clinton

Change it to:
John Q. Smith

Notice how I kept the first name, middle initial with period, and last name.

The For Each action Substrings collection has an option to return the location of the string instead of the string itself.

You have to keep a tally of the last position yourself. It's somewhat tricky, but definitely doable.

1 Like

I really appreciate your help.
I'm going to spend some time on Peter's suggestion and work with the 'substrings collections' but would also like to learn some more Regex with your help.
Here's the body of text as it will always stand.
TEXT START
So nice speaking to you today.
Here are our policies, contact information and a general quote.


Rates are charged per person per hour

All appointments are a XhrminX hour minimum. (Per Staff Member)
The One-time appt. rate is $Xrate3X/hour/person.
The Move-in/out rate is $Xrate4X/hour/person.
The Post-Construction rate is $Xrate5X/hour/person.
We can then set up a weekly or bi-weekly schedule if you desire.
The Bi-Weekly rate is $Xrate2X/hour/person, Weekly rate is $Xrate1X/hour/person.
The same cleaning associate will always clean your home. (barring unforeseen circumstances)

An estimate for a 'spring', first time cleaning of a home that size would be Xstaff_initialX staff member(s) Xhours_initialX hours. Then recurring service either bi-weekly or weekly would be 1 staff member for the amount of time you requested with XhrminX hours being the minimum.

Initial Deep Cleaning:
Xstaff_initialX staff Xhours_initial_2X hours, tax included = $XinitialhighX
Xstaff_initialX staff Xhours_initial_1X hours, tax included = $XinitiallowX
If less time is needed, you only pay for the time that is used.

Bi-Weekly:
1 staff Xhrminrecur1X hours, tax included = $XbwhighX
1 staff XhrminrecurX hours, tax included = $XbwlowX

Weekly:
1 staff Xhrminrecur1X hours, tax included = $XwhighX
1 staff XhrminrecurX hours, tax included = $XwlowX

For your appointment please provide:
· A vacuum with bag emptied, if applicable.
· A mop & bucket.
· Floor care 'stick' (eg.) Bona Microfiber Floor Mop with washable pads or similar
(Bona Hardwood Floor Care System at Home Depot or online)
· Swiffer stick with dry pads optional, good for getting animal hair.
· Broom, toilet brush and paper towels.
· Stainless Cleaner if needed.
· Small step stool if needed.
· Please self-clean your oven the night before service. (if requested to be cleaned)
· If the oven is non self-cleaning then please provide a can of fume-free oven cleaner.
(there is a surcharge if we supply the equipment)

We provide all cleaning products & solvents (unless you'd rather supply them)
Microfiber Cloths, Scrub Brushes and a LOT of elbow grease!

All appointments are a XhrminX hour minimum per staff member.
We ask that air-conditioning is on during the summer and that heat is provided in the winter.

Our Cancellation Policy:
If you cancel after 2:00 PM the day prior to your scheduled
appointment a $50 fee will be assessed. For service canceled
less than 3 hours in advance 60% of the estimated cost will be charged.
80% of the estimated cost will be charged for client no-shows on the day of service.

Billing:
If you are paying with a credit card you will receive a
receipt from Authorize.net when you are billed.
A 2.5% convenience fee is applied to CC bills.
Or we can email you an invoice and you can mail a check.
Invoices not paid by the due date incur LATE FEES.
IF NOT PAID 15 DAYS AFTER THE DUE DATE, WE WILL
AUTOMATICALLY CHARGE THE CREDIT CARD ON FILE.
Please - NO Cash payment is accepted in the field.
Tips are graciously accepted by staff members though not mandatory.

END OF TEXT

I would like to italicize this line
If less time is needed, you only pay for the time that is used.

And I would like to bold this text
LATE FEES.
IF NOT PAID 15 DAYS AFTER THE DUE DATE, WE WILL
AUTOMATICALLY CHARGE THE CREDIT CARD ON FILE.
Please - NO Cash payment is accepted in the field.
Tips are graciously accepted by staff members though not mandatory.
END OF TEXT

So the goal as I understand it, would be to have regex find the beginning of the text up to the beginning of the italics line - I could then set that as a variable.
then I could have a variable that is in italics which is the italics line
then find from the end of the italics line to the beginning of the bold text, making that into a variable.
Then a variable would be the bold text
then lastly from the end of the bold to the end of the message would be the last variable.
I could then combine those variables to make the complete and stylized text.
whew......

Here is the text in a KM action if that is easier,,,,, a post thought...

Keyboard Maestro “Set Variable to Text” Action

Set Variable to Text.kmactions (3.5 KB)

This Macro seems to do the job. Please review and test, and let us know if this works for you.

The RegEx looks for all text between the 4 asterisks, inclusive of the asterisks:
(\*{4}.+\*{4})

The RegEx looks for "LATE FEES" thru the end of the text:
(LATE FEES.*)

For full RegEx explanation, see: regex101: build, test, and debug regex

Please make sure that all of your source quotes will fit this pattern.

##Macro Library   Add Rich Text to Parts of Text @Example


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/0/087ca2d03942594caefd9937e12fa2d4502b4d2c.kmmacros">Add Rich Text to Parts of Text @Example.kmmacros</a> (7.8 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###Use Case

* Split Source Text into Parts, and apply rich text formatting to selected parts.

---


<img src="/uploads/default/original/2X/0/01743226944dd37927184536ee8920ddfd80f878.png" width="619" height="802">
1 Like

you’re a maniac dude… I can’t take a lot of time until later to look at this , but thank you in advance.
The last sentence "Please - NO Cash payment is accepted in the field.
Tips are graciously accepted by staff members though not mandatory."
Should not be bolded, is that possible?
BTW, I have been working although slowly, with Peter’s suggestion about for each approach. I’ve made good headway and will post once I have it ‘smoothed’ out.
Again, thanx for your expertise and time, it is greatly appreciated.
For my edification could you break down the regex that you used for me?
(?s)(.)(*{4}.+*{4})(.)(LATE FEES.*)
Cheers

Yes, but that's not what you asked for above.
Use this RegEx:
(?s)(.*)(\*{4}.+\*{4})(.*)(LATE FEES.*)(Please \- .*)

and add z_PT3 to the search and to the Set Clipboard.

I already did:

Revised RegEx: regex101: build, test, and debug regex

1 Like

you of course are absolutely right, my apologies.
and again, thanx

  • after looking at the code, - all working and
    it’s a thing of beauty! really =)
1 Like

New comment: just 'found' the 'position' option which could be used instead of 'range'.

Stylized Text from 1 master variable - user defined 'delimiters'
Using the 'for each' substring 'as range' to get start and end points of text, set a variable as that text and then be able to stylize it in a 'composite' clipboard

Keyboard Maestro Actions.kmactions (23 KB)

1 Like