I need a macro to convert indications of italics in txt files (e.g. "_any text_") to true italics in an RTF file.
It seems like a simple thing, but none of the same macros I've seen come close to what I need: find text, change format
Myron
"_text_" to italic
Re: "_text_" to italic
…and presumably remove the underlines?
Actually for this kind of thing you don't even need a macro. You can use Find/Replace: Note that the replace expression has the Italics applied.
You can of course macroize the above, but you'll need to be careful about the interactions with styles in the macro. So I prefer doing it in two steps: (1) Find/Replace (2) apply the Italics. To be fail safe you should test before applying the italics.
Code: Select all
if Find and Replace '_(.+?)_', '\1', 'Ea'
Italic
end
Code: Select all
$doc = Document.active
$bits = $doc.text.findAndReplace '_(.+?)_', '\1', 'Ea'
if $bits.count
$doc.setSelection $bits
Menu.activateAtPath 'Italic'
end
philip
-
- Posts: 8
- Joined: 2009-08-12 02:59:46
Re: "_text_" to italic
Good Grief! The Find/Replace routine works! I had no idea it was possible.
I'll have to study the Find/Replace documentation... It's not at all obvious why you wouldn't use "AnyText" as the wildcard. [...and at the moment I have no idea what "1+shortest" is/does. Time for some reading.]
The macro version seems to do exactly what I want. (Although I've learned always to preserve a path back to the starting position)
Thanks for the help!!
For what it is worth, learning macros in NWP seems needlessly difficult because the Macro Reference does not start with a clear specification of the nature of a stored macro (an ASCII file that is interpreted), or the syntax requirements. Can multiple commands be linked on the same line of code, or is one-thing-per-line absolute? Is white space ignored? Is there a way to end the code part of a line short of a Return/LF? How are comments marked out?

I'll have to study the Find/Replace documentation... It's not at all obvious why you wouldn't use "AnyText" as the wildcard. [...and at the moment I have no idea what "1+shortest" is/does. Time for some reading.]
The macro version seems to do exactly what I want. (Although I've learned always to preserve a path back to the starting position)
Thanks for the help!!
For what it is worth, learning macros in NWP seems needlessly difficult because the Macro Reference does not start with a clear specification of the nature of a stored macro (an ASCII file that is interpreted), or the syntax requirements. Can multiple commands be linked on the same line of code, or is one-thing-per-line absolute? Is white space ignored? Is there a way to end the code part of a line short of a Return/LF? How are comments marked out?
Re: "_text_" to italic
Indeed it is not.MyronGochnauer wrote: ↑2019-12-05 10:16:59It's not at all obvious why you wouldn't use "AnyText" as the wildcard.
Consider the following case:
Code: Select all
_Nisus_ Writer is a great _document_ processor.
An algorithm that prefers the second answer is called "greedy". Powerfind is greedy. Using "shortest" is the way to get Powerfind to stop being greedy.
If you look at the Powerfind Pro version of AnyText you will see that it has the greedy repetition built in, which is why I chose to roll my own "Any Text".
philip
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
Re: "_text_" to italic
If you're willing to do a good amount of reading then you might check out information on regular expressions (aka: regex), which Nisus Writer supports in our PowerFind Pro search mode. Learning regex can change your life if you work with text! Though regex is certainly a very complicated topic.MyronGochnauer wrote: ↑2019-12-05 10:16:59 It's not at all obvious why you wouldn't use "AnyText" as the wildcard. [...and at the moment I have no idea what "1+shortest" is/does. Time for some reading.]
I'm sorry about that. The macro references is indeed more of a reference. It's not that friendly in introducing all the concepts you need to know. You might best be served just looking at a few macros from the Macro menu for example code.learning macros in NWP seems needlessly difficult because the Macro Reference does not start with a clear specification of the nature of a stored macro (an ASCII file that is interpreted), or the syntax requirements.
I'll try to answer your questions, but please let us know if you have more.
Any text file can be a macro. It can be a plain text file, or more likely in Nisus Writer, an RTF file with the .nwm extension. Generally speaking you should have one command per line. You must use returns/newlines to terminate commands; there's no terminal character like semicolons in C++. Comments are marked by prefixing the line with #, or you can mark multi-line comments using /* */. Whitespace is mostly ignored as in other programming languages. But there are a few exceptions to that, like invoking menu commands. Consider these lines of macro code:
Code: Select all
Font Face:Bold
Menu ":Format:Font Face:Bold"
Font Face:Bold
Menu ":Format:Font Face:Bold"