Reply to topic  [ 7 posts ] 
Macro: format text and italicize 
Author Message

Joined: 2011-01-12 05:32:38
Posts: 256
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:
$$ 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:
$$ 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 202 times
2013-10-15 23:24:13
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Yeah, I wasn't careful enough when I wrote the macro. I wrote

Code:
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:
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


2013-10-16 01:40:30
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
phspaelti wrote:
Well, this shows why all that beta testing is necessary…

There's always some forgotten little detail lurking! 8)


2013-10-16 10:16:55
Profile WWW

Joined: 2011-01-12 05:32:38
Posts: 256
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:
$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:
$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.



2013-10-16 19:41:39
Profile

Joined: 2011-01-12 05:32:38
Posts: 256
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! :)


2013-10-16 20:01:30
Profile

Joined: 2011-01-12 05:32:38
Posts: 256
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.


2013-10-16 20:04:54
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
$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


2013-10-16 23:30:43
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software