In a macro, remove formatting from a text variable

Everything related to our flagship word processor.
Post Reply
allenwatson
Posts: 34
Joined: 2005-10-05 15:05:51
Location: Portland, OR
Contact:

In a macro, remove formatting from a text variable

Post by allenwatson »

I have a macro that builds a text string, copying parts from a formatted document. I want to remove all formatting except styles from the variable, not from a selection within the document. The menu command would affect the entire document which is not at all desirable.

This is probably very simple, but the last time I worked on a macro was about 2 years ago and I’ve forgotten most of what I knew then.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: In a macro, remove formatting from a text variable

Post by phspaelti »

allenwatson wrote: 2023-06-15 14:50:27 I want to remove all formatting except styles from the variable
(emphasis mine :wink: )
allenwatson wrote: 2023-06-15 14:50:27 This is probably very simple
:P

In principle you might think this is the place to use the Push Target Selection command, but as the Macro Reference says explicitly, Menu commands don't affect such targets, and instead continue to affect the document text.

So here is what I suggest:
  1. Copy the original
  2. Remove the formatting using the menu command
  3. Copy the modified bit
  4. Swap it for the original
A bit roundabout, but it works.

Code: Select all

$sel = TextSelection.active
$original = $sel.subtext
Remove Formatting Except Styles
$modified = $sel.subtext
$sel.text.replaceInRange $sel.range, $original

# Show result
Select Document End
Type Attributed Text $modified
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: In a macro, remove formatting from a text variable

Post by phspaelti »

Now having reread what you are actually looking for, I should say that, if your macro is collecting lots of pieces from the original document and then gathering them for output elsewhere, sure you could use my little swap each time you collect a piece. But I suspect it might be much better to approach things differently. Basically I would collect unattributed strings, but also the info about what is where, and then apply clean formatting to the output at the end. But for that one would have to know more about what you are trying to do in the first place.
philip
allenwatson
Posts: 34
Joined: 2005-10-05 15:05:51
Location: Portland, OR
Contact:

Re: In a macro, remove formatting from a text variable

Post by allenwatson »

I’m working with a document much like a Bible, with Chapter numbers, Paragraph numbers, and sentence numbers. I’m taking a selection of one or more sentences and using searches to extract a reference that ends up looking like this:" T-3.IV.4:2–7”. The problem I am trying to solve is that portions of that reference are in bold, others in superscript. I want to reduce the entire thing to plain text before I paste it into another document with “use existing styles." I suppose I could paste it as is, then select the reference and remove formatting and styles. But if it is being pasted into text that has a style applied, I would be losing that style.

What I thought might be done is some way to convert the contents of a variable within the macro to plain text, or some way to i. the macro convert the contents of a clipboard to plain text.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: In a macro, remove formatting from a text variable

Post by phspaelti »

Hello Allen,
Okay. Now I understand better what you are trying to achieve.
It might be good to keep some terms clear. One of the advantages of Nisus Writer is that it can work with Attributed Text, text with styling attributes. I prefer to reserve the term Styles for Character/Paragraph styles, which are one kind of attribute that can be used to define combinations of styling attibutes. Text without styling attributes ("plain text") can be called Strings.

Now it seems what you want to do is the exact reverse of what I understood originally: you want to keep the attributes, and get rid of the styles, so that the pasted text takes on the styles of the target document, but nevertheless keeps the extra added salient attributes (e.g., bolding, etc.). This is incidentally a general problem, that arises in any copy/paste operation. Judging by your mentioning "use existing styles" you are pasting text which has a style with the same name, but different attributes, where Nisus throws up a style conflict dialog.

But here is the amazing thing: if you transfer a bit of text from Document A with attributes and styles to Document B which has styles with the same name but different attributes on those styles, the transferred text will take on the new style with its attendant attributes. Here is what the macro code will look like:

Code: Select all

$sourceDoc = Document.withDisplayName 'Document A.rtf'
$targetDoc = Document.withDisplayName 'Document B.rtf'

$sourceSel = $sourceDoc.textSelection
$sourceText = $sourceSel.subtext

$targetSel = $targetDoc.textSelection
$targetDoc.text.replaceInRange $targetSel.range, $sourceText
How you build this into your macro will depend a bit. How are you transferring the text? Is the macro just supposed to select one bit, or many? If it's the latter, how does it know which bit will go where?

If you still have questions, and need help, just let me know.
philip
Post Reply