Replace first line of selected text with same text but styled font 18 color blue

Macro attached

When I create a Scrivener document (could be the body of an Evernote note) from a block of selected text, I often want to give the first line some style, for example font size 18, color blue.

Thanks to @JMichaelTX 's regex .+, I can easily extract the first line of selected text with a regex which I can then copy to a named clipboard → change style of named clipboard.

I have 2 problems with the macro:
1- I can't figure out why at the end it is the styled blue font 18 variable itself (!) %Variable%First_Liine_of_Selection% that is displayed rather than styled first line of selected text.

2- the second problem is that I obviously have no clue how to replace the first line of selected text with the styled variant. I can't understand why KBM does not have a simple search / replace regex like BBEdit, in which case I could replace .+ with the styled named clipboard.

Thanks very much.

The macro:

:hammer_and_wrench: Replace 1st line of selection with styled text blue font 18.kmmacros|attachment (29.0 KB)

The end result: display named clipboard

You are missing a critical action at the top of your macro: Copy

Once I add that, it works fine:

image

KM has a powerful Search and Replace action, but it only works with PLAIN text.

So, you wil have to combine the rich text first line, with the plain text in one action, like:

image

image

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So I would write your macro a lot different.
BTW, the RegEx I use:
(.+)\R(?s)(.+)
is designed to capture all remaining lines to the body.
The key is the (?s).
To understand this, setup this Regex in Regex101.com

Sample Input

image

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Set Clipboard to Styled First Line Plus Body [Example]

-~~~ VER: 1.0    2020-09-04 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Set Clipboard to Styled First Line Plus Body [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


BTW, I would avoid the use of Named Clipboards and Global Variables as much as possible. They just add bulk to the data KM keeps track of, and requires you manually maintain (delete) from time to time.

1 Like

superb !! exactly what I was looking for. thanks a lot !!