Macro: format text and italicize

Get help using and writing Nisus Writer Pro macros.
Post Reply
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Macro: format text and italicize

Post by NisusUser »

The attached macro (thanks, Philip!) does what I need in one case, but not in another.

It is supposed to apply a Character Style "EC-Titles and acrostic letters" (which is to have a font size of 10) to text in <<double arrows>> and then delete the arrows themselves.

It is also supposed to change all text in [brackets] to italics and delete the brackets.

If there is indeed some text in <<double arrows>>, then the macro seems to work correctly, whether or not the text in double arrows comes first in the file or not.

However, if there is no text at all in double arrows, the macro applies the Character Style to the text that was in italics.

I've studied the macro and tried to figure it out, but I can't tell why it's doing that.

Text that is processed properly (note "[test]" and "<<Test.>>"):

Code: Select all

$$ Ro 1:8
Ziiwizxziz zz xzxxix zzxx Zizxxi xzi Jzzx Ziizix xz ixz xizxz, xzx {„zxiz ixzx iixziiwz zxzixizwz“ – zixz „ixzx iixziiwzz zxzixizwzz“}[test] ixzx iixziiwz zxzixizwz xizzwz xzzzxixiz. 
$$ Ro 1:9
Zzz <<Test.>> Zizxzz, xxiizw zz izixzxix zzxx xxzziz ix Zxxzxz zxzxxziiixiz, xiz wzxx iixxxixizz, xzix xz xziixxxz wixix ixz zzxx wzixxzz xizxxwzi 
Text that is not processed properly:

Code: Select all

$$ Ro 1:8
Ziiwizxziz zz xzxxix zzxx Zizxxi xzi Jzzx Ziizix xz ixz xizxz, xzx {„zxiz ixzx iixziiwz zxzixizwz“ – zixz „ixzx iixziiwzz zxzixizwzz“}[test] ixzx iixziiwz zxzixizwz xizzwz xzzzxixiz. 
$$ Ro 1:9
Zzz Zizxzz, xxiizw zz izixzxix zzxx xxzziz ix Zxxzxz zxzxxziiixiz, xiz wzxx iixxxixizz, xzix xz xziixxxz wixix ixz zzxx wzixxzz xizxxwzi 
Attachments
1. 2013-10-14 Italicize and Remove Brackets.nwm
(16.47 KiB) Downloaded 712 times
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro: format text and italicize

Post by phspaelti »

Yeah, I wasn't careful enough when I wrote the macro. I wrote

Code: Select all

Find and Replace '\[([^\[\]]+)\]', '\1', 'Ea'
Format:Font Face:Italic
which is supposed to find text in brackets, remove the brackets, and then leave the found stuff selected. Then it applies the Italic.
And that is what it does, as you found out.

But the problem comes when it doesn't find any text in brackets. The instruction to apply Italic is still carried out. But it will now affect whatever happens to be selected. And the same is true for the second part of the macro that works on text enclosed in << … >>.

The solution is to add an if clause. You can do that like this:

Code: Select all

if Find and Replace '\[([^\[\]]+)\]', '\1', 'Ea'
Format:Font Face:Italic
end
Now if the Find and Replace does not find anything it will skip the Italic instruction. Do the same for the other Find as well.

Well, this shows why all that beta testing is necessary…
philip
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro: format text and italicize

Post by martin »

phspaelti wrote:Well, this shows why all that beta testing is necessary…
There's always some forgotten little detail lurking! 8)
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Macro: format text and italicize

Post by NisusUser »

If I understood you correctly, this macro is the one that should work (added the "if"), but if there is no text in <<double arrows>>, it still shrinks the text in [brackets].

Code: Select all

$doc = Document.active

if Find and Replace '\[([^\[\]]+)\]', '\1', 'Ea'
Format:Font Face:Italic
end

# Add the style, if necessary 
$doc.addStyles ‘EC-Titles and acrostic letters’
Find and Replace '<<(.+?)>>', '\1', 'Ea'
Character Style:EC-Titles and acrostic letters
I even tried adding an "if" in the "Find and Replace" part looking for <<>>, but the macro hung then on line 9.

So I added "end" and got this:

Code: Select all

$doc = Document.active

if Find and Replace '\[([^\[\]]+)\]', '\1', 'Ea'
Format:Font Face:Italic
end

# Add the style, if necessary 
$doc.addStyles ‘EC-Titles and acrostic letters’
if Find and Replace '<<(.+?)>>', '\1', 'Ea'
Character Style:EC-Titles and acrostic letters
end
Now it works!

Thanks, Philip!
Last edited by NisusUser on 2013-10-16 20:02:17, edited 1 time in total.
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Macro: format text and italicize

Post by NisusUser »

phspaelti wrote:Do the same for the other Find as well.
Obviously this is what I missed. I now recall seeing it yesterday, but not today! :)
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Macro: format text and italicize

Post by NisusUser »

One other thing, Philip. The macro adds the new character style, but does the macro tell NWP to make that style have a font size of 10? It works perfectly that way, but I don't see that specified anywhere. So where's the 10-pt size coming from? Of course I wanted the <<>> stuff to be 2 points smaller than the other text, so assuming the other is 12, that's fine. I just don't see how NWP knew to do that.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro: format text and italicize

Post by phspaelti »

NisusUser wrote:One other thing, Philip. The macro adds the new character style, but does the macro tell NWP to make that style have a font size of 10? It works perfectly that way, but I don't see that specified anywhere. So where's the 10-pt size coming from? Of course I wanted the <<>> stuff to be 2 points smaller than the other text, so assuming the other is 12, that's fine. I just don't see how NWP knew to do that.
The macro adds the style from the macro file itself, which is applied to the text ‘EC-Titles and acrostic letters’ after the .addStyles command. Note incidentally what's important with that command are the styles applied to the text, not the text itself. So if you want the style added by the macro to look different, you can open the macro file, switch to the style sheet view and change the styles attributes as you like. Obviously this will only affect future files. Note also that such style changes will not affect files which already have a/the ‘EC-Titles and acrostic letters’ style. If you wanted that behavior you should change the line to:

Code: Select all

$doc.addStyles ‘EC-Titles and acrostic letters’, 'replace'
This will cause the macro to replace any such pre-existing style.

***Correction***
An earlier version of the code above had 'replace' with a capital 'r'. However the code only works if 'replace' is spelled all in lower case.
philip
Post Reply